Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populate A DropDownList 1

Status
Not open for further replies.

arpan

Programmer
Oct 16, 2002
336
IN
How do I populate a DropDownList with the records existing in a SQL Server database table? As such, I know how to retrieve records from a backend.

Thanks,

Arpan
 
With DropDownList
.DataSource = 'your datasource i.e.datatable,dataset,etc
.DataTextField = 'the column in your source that holds
'the value to be displayed in the ddl
.DataValueField = 'same as above, but holds the value
.DataBind 'binds the data to the drop down list
End With

hth

D'Arcy
 
Hi,

Thanks for your response. I could follow what you have suggested but could you please tell me what is wrong with this code snippet (to populate a DropDownList with DB records):

<script language=&quot;VB&quot; runat=&quot;server&quot;>
Sub Page_Load(obj As Object,ea As EventArgs)
Dim objDS As DataSet
Dim objConn As SQLConnection
Dim objDapter As SQLDataAdapter

objConn=New SQLConnection(............)
objDapter=New SQLDataAdapter(&quot;SELECT CName,FDate FROM Company&quot;,objConn)
objDS=new DataSet()
objDapter.Fill(objDS,&quot;Comp&quot;)

cname.DataSource=objDS.Tables(&quot;Comp&quot;).DefaultView
cname.DataBind()
End Sub
</script>
<body>
<form runat=&quot;server&quot;>
<asp:DropDownList id=&quot;cname&quot; runat=&quot;server&quot;>
<asp:ListItem>
<%# Container.DataItem(&quot;CName&quot;) & &quot; &quot; & Container.DataItem(&quot;FDate&quot;) %>
</asp:ListItem>
</asp:DropDownList>
</form>

When I execute the above I get an error saying Code blocks are not supported in this context which points to the <%# Container.DataItem.....%> line. Where am I erring here?

Regards,

Arpan
 
well my guess would be that instead of putting those tags between the <asp:ListItem></asp:ListItem?>, you're supposed to assign the values as properties to the initial list item tag.
i.e.
<asp:ListItem text = <yourCName container> value = <yourvalue>></asp:ListItem>

and that you'll need some loop to happen so that it can add each item in your dataset's datatable.

I use VS.NET, so I never code this way with the actual tags in an html view, so this is just a best guess. Some others here are pretty knowledgeable with the spaghetti stuff, so they might be able to help more.

hth...somehow
:)

D'Arcy
 
Hi,

Thanks once again for your advice. This is what I have finally done which is working as well:

Sub Page_Load(obj As Object,ea As EventArgs)
Dim objDS As DataSet
Dim objConn As SQLConnection
Dim objDapter As SQLDataAdapter

objConn=New SQLConnection(&quot;Server=(local);........&quot;)
objDapter=New SQLDataAdapter(&quot;SELECT CName=CName + '-' + CCode FROM Company&quot;,objConn)

objDS=new DataSet()
objDapter.Fill(objDS,&quot;Comp&quot;)

cname.DataSource=objDS.Tables(&quot;Comp&quot;).DefaultView
cname.DataBind()
End Sub
<form runat=&quot;server&quot;>
<asp:DropDownList id=&quot;cname&quot; DataTextField=&quot;CName&quot; runat=&quot;server&quot;>
</asp:DropDownList>
</form>

Now when I had a look at the source code from the browser, it showed the following HTML:

<select.......>
<option value=&quot;Company1-C1&quot;>Company1-C1</option>
<option value=&quot;Company2-C2&quot;>Company2-C2</option>
<option value=&quot;Company3-C3&quot;>Company3-C3</option>
<option value=&quot;Company4-C4&quot;>Company4-C4</option>
</select>

What I want is that though I have the columns CName & CCode in the SELECT statement, I want that only the CName should be displayed as the options in the DropDownList & not the CCode. How do I do this? Also the value attribute within the <option> should be equal to the CCode only & not the CName-CCode as what can be seen from the HTML source code (which I have shown in green color). How do I do this? Also the ASP.NET <asp:DropDownList.....> would show all the company names in the DropDownList but I want that the first option that should be displayed in the DropDownList should be something like &quot;Select Your Company Name&quot; & then only the company names be listed. What do I do to ensure this?

Thanks once again for your help & co-opeartion,

Regards,

Arpan
 
well, if you want the values split up you have to split them in your sql statement instead of joining them there.

You'll have to manually add the &quot;Select your company&quot; item to the drop down list, or have it as the first record in the database (but this is ONLY advised if you aren't using Null values in your database).

D
 
I am really thankful to you for helping me out so much. Yeah, even I was under the impression that &quot;Select Your Company&quot; has to be done manually but the question is where do I include the code to do so or how do I do it? Also why is it advisable to do so only if there aren't any NULL values in the database?

Regards,

Arpan
 
Well, there's two trains of thought with null values in a database: either you have them, or you don't.

We don't. Instead, we ensure that any item that potentially won't have a set value is associated with a record representing that.

i.e. in our Client table, the row with an ID of 0 has a description value of &quot;None Selected&quot;. that way, when our client drop down list loads, &quot;None Selected&quot; always appears and is a valid selection. Otherwise, we'd have to manually add a &quot;None Selected&quot; and then have code that would check for it, and insert a null value when we inserted/updated into the database. Way too much management, and we've got a better fix (in our eyes anyway)

And thanks for the star, I'm glad I could help!
:)

D
 
One thing extra....

As HTTP is stateless, the extra overhead of using ADO.NET in disconnected mode can be a little bit cumbersome. Have a think about using the DataReaders instead.

Craig
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top