HELP!! DropDownList not displaying first record
-
I was about to publish my site live until I discovered that a DDL is not displaying the first record from the results set! My code is as follows: if (objRdr.Read()) { ddlModelTypes.DataSource = objRdr; ddlModelTypes.DataValueField = "TypeId"; ddlModelTypes.DataTextField = "ModelType"; ddlModelTypes.DataBind(); } I thought the following line was replacing the first value: ddlModelTypes.Items.Insert(0, "Other"); However, I took this out and it's still not displaying the first record value so any help will be much appreciated. Thanks Lorna
-
I was about to publish my site live until I discovered that a DDL is not displaying the first record from the results set! My code is as follows: if (objRdr.Read()) { ddlModelTypes.DataSource = objRdr; ddlModelTypes.DataValueField = "TypeId"; ddlModelTypes.DataTextField = "ModelType"; ddlModelTypes.DataBind(); } I thought the following line was replacing the first value: ddlModelTypes.Items.Insert(0, "Other"); However, I took this out and it's still not displaying the first record value so any help will be much appreciated. Thanks Lorna
You do not need to call objRdr.Read(). Try the following. connection.open ddlModelTypes.DataSource = objRdr; ddlModelTypes.DataValueField = "TypeId"; ddlModelTypes.DataTextField = "ModelType"; ddlModelTypes.DataBind(); connection.dispose();
I didn't get any requirements for the signature
-
I was about to publish my site live until I discovered that a DDL is not displaying the first record from the results set! My code is as follows: if (objRdr.Read()) { ddlModelTypes.DataSource = objRdr; ddlModelTypes.DataValueField = "TypeId"; ddlModelTypes.DataTextField = "ModelType"; ddlModelTypes.DataBind(); } I thought the following line was replacing the first value: ddlModelTypes.Items.Insert(0, "Other"); However, I took this out and it's still not displaying the first record value so any help will be much appreciated. Thanks Lorna
Do Like this
Member 3402886 wrote:
ddlModelTypes.DataSource = objRdr; ddlModelTypes.DataValueField = "TypeId"; ddlModelTypes.DataTextField = "ModelType"; ddlModelTypes.DataBind();
No need to Call
Member 3402886 wrote:
if (objRdr.Read())
No need to Call , otherwise everything else looks ok , it should work
-
You do not need to call objRdr.Read(). Try the following. connection.open ddlModelTypes.DataSource = objRdr; ddlModelTypes.DataValueField = "TypeId"; ddlModelTypes.DataTextField = "ModelType"; ddlModelTypes.DataBind(); connection.dispose();
I didn't get any requirements for the signature
Thanks but I'm using the if (objRdr.Read()) to check that records have been returned as sometimes there will be no records - is there another way to check this before using DataBind?
-
Thanks but I'm using the if (objRdr.Read()) to check that records have been returned as sometimes there will be no records - is there another way to check this before using DataBind?
It doesn't matter if there's no records, the binding won't fail. You can always check post binding (i.e. count the items in the list) if you need to do something in that case. Calling dr.Read moves the reader onto the second record...