text in ddl before binding with dataset.

Rani

Well-known member
Joined
Nov 19, 2005
Messages
71
Programming Experience
Beginner
I have two ddl countries and states. I populate using the dataset and bind them to the ddl's.
what i want to do is before i bind i want to show in my ddl text like
"---Please select Country------" or "----Please select a state-----"
how would i get this done?
Thanks:confused:
 
Eother you're using a DropDownList, in which case this should have been posted in the WebForms forum, or else you're using a ComboBox, in which case you should have said so and posted this in the WinForms forum. Please post your questions in the most appropriate forum. After 46 posts you should know this. Please make the effort.

If your control is bound to data then it will display only that data. If you want an extra entry then you have to add it to your data source.
 
I am using a dropdown list control and webservice to populate countries.
Please see below. After this line "ds = myservice.getcountries"
In order to show the first line in the dropdownlist the text like "Please select a country" then follows all the countries? How would i do the text part?
VB.NET:
Dim ds AsNew DataSet
'create an instance of a proxy class
Dim myservice AsNew picture.PictureWebService
ds = myservice.getcountries
Me.ddlcountry.DataSource = ds
Me.ddlcountry.DataTextField = "country"
Me.ddlcountry.DataValueField = "country_id"
Me.ddlcountry.DataBind()
ds = Nothing
 
Thanks.
 
Last edited by a moderator:
Hello!

If i know how to, I would not be posting requests for help.
can you show me an example as to how it can be done.

I want to write the text before this line Me.ddlcountry.DataSource = ds

I tried to the best of my knowledge. It did not work.

Thanks
 
Here these are couple things i tried. I am getting object not set to an instance of an object

VB.NET:
Dim myservice AsNew picture.PictureWebService
ds = myservice.getcountries
Dim drcountry As DataRow
Me.ddlcountry.Items.Add(drcountry("--Please select a country---"))
also tried
Me.ddlcountry.Items.Add(new listitem(drcountry("--Please select a country---")))
also tried
Me.ddlcountry.Items.Add(text(drcountry("--Please select a country---")))
 
Me.ddlcountry.DataSource = ds
none of the above works.
 
Last edited by a moderator:
You didn't do what was adviced. You seemingly try to add it to the controls items collection, you were told to add it to the datasource of the control.

You add a row to DataTable by using its NewRow method which returns an empty DataRow, this you fill in values and Add to DataTable.Rows collection. In this case it would be:
VB.NET:
Dim dr As Data.DataRow = ds.Tables("country").NewRow
dr.Item("country_id") = "--Please select a country--"
ds.Tables("country").Rows.InsertAt(dr, 0)

And please put code parts of your posting in code boxes will you? See the forum FAQ on how to use it, you can also explore the advanced posting editor, try out the editor buttons (tooltip help display when you hover them), edit/preview is possible before you post.

Thread moved to ASP.Net section, Data Access forum.
 
Object reference not set to an instance of an object.

This is the error i get "Object reference not set to an instance of an object. "
when i try the pc of code below. I get the error at this line"Dim dr As Data.DataRow = ds.Tables("countries").NewRow"

VB.NET:
Dim con AsNew SqlConnection(Application("connection"))
Dim sql AsString = "select * from countries"
Dim ds AsNew DataSet
Dim da AsNew SqlDataAdapter(sql, con)
da.Fill(ds)
Dim test AsString = ds.GetXml
[COLOR=red]Dim dr As Data.DataRow = ds.Tables("countries").NewRow[/COLOR]
[COLOR=red]dr.Item("country") = "---Please select a country---"[/COLOR]
[COLOR=red]ds.Tables("countriesy").Rows.InsertAt(dr, 0)[/COLOR]
Me.ddlcountry.DataSource = ds
Me.ddlcountry.DataTextField = "country"
Me.ddlcountry.DataValueField = "country_id"
Me.ddlcountry.DataBind()
ds = Nothing
Thanks for your help
 
Hey, you got the code box working, nice dude! :) I marked in red the three code lines you copied wrong, you have written the datatable name and datacolumn name wrong three places in those three lines of code. Correct them as the sample I gave.
 
Object reference not set to an instance of an object.

This line i am stll getting error Object reference not set to an instance of an object..
Dim dr As Data.DataRow = ds.Tables("countries").NewRow
 
Please disregard my request.

The answer to this is:
when filling da adapter specify the name of the table da.Fill(ds, "countries")

TG helped me out. It worked.

Thanks for your time.


 
ok, I thought your table was "country", sorry I mixed because I set up the datasource with datamember and datavaluefield, you did datatextfield and datavaluefield without stating datamember (which you don't have to when there is only one datatable in the dataset) so I just misread it. Glad you found out your table name and got the sample sorted out.
 
Hi

Another alternative would be to use the Insert method of the Items collection to insert your additional value. This would be done after you have called the DataBind method on your DropDownList:

VB.NET:
DropDownList1.Items.Insert(0, New ListItem("--Please select a country--", "0"))

The advantage to using this method is that you will not have to modify your DataSource which could conceivably be used elsewhere in your routine.


HTH
 
Is it possible to add individual items to a bound WebForms control? I know that it's not with WinForms but I guess WebForms could well be different. I've no experience with WebForms so I don't know, but I'd like to have this conformed or denied.
 
Hi

It is possible with the DropDownList and ListBox controls (I haven't looked into others) to add content as in my reply above. However, when binding to a DataSource such as an SQLDataSource, the code above will not cause an exception, but the item will not be added either, instead you first have to set the AppendDataBoundItems property of the control to True. This is not required when setting the DataSource to a DataTable/DataView etc.

I took a quick look at the ComboBox in Windows Forms (it's been a while since I have worked with Windows Applications) and it appears that the control has no method for adding new content when it is bound to a DataSource. So in that instance you would either have to modify your data source (as was mentioned by yourself) or create an object that could store your Display Value and Data Member value and then iterate the rows of your DataSource adding a new instance of your custom object to the ComboBox, in this instance you would also need to supply a ToString method in order for the combo box to display the correct value when that object is added.


HTH
 
Back
Top