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
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="VB" runat="server">
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("SELECT CName,FDate FROM Company",objConn)
objDS=new DataSet()
objDapter.Fill(objDS,"Comp"
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?
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.
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
cname.DataSource=objDS.Tables("Comp".DefaultView
cname.DataBind()
End Sub
<form runat="server">
<aspropDownList id="cname" DataTextField="CName" runat="server">
</aspropDownList>
</form>
Now when I had a look at the source code from the browser, it showed the following HTML:
<select.......>
<option value="Company1-C1">Company1-C1</option>
<option value="Company2-C2">Company2-C2</option>
<option value="Company3-C3">Company3-C3</option>
<option value="Company4-C4">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 <aspropDownList.....> 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 "Select Your Company Name" & then only the company names be listed. What do I do to ensure this?
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 "Select your company" 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).
I am really thankful to you for helping me out so much. Yeah, even I was under the impression that "Select Your Company" 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?
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 "None Selected". that way, when our client drop down list loads, "None Selected" always appears and is a valid selection. Otherwise, we'd have to manually add a "None Selected" 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)
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.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.