DataSet for DropDownList problem

I

Iain Kirk

Being a bit of a novice any help or pointers would be appreciated.

I am building a web page that populates a DropDowList from a table in SQL,
for testing purposes i have a simple page that displays a datagrid with the
same data, now this does work so i know the data is there but the
dropdownlist is empty. If anyone could show me where i'm going wrong i would
be very thankful.

Here is the code i am using:

<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
Dim ConnectionString As String =
"server=(local);database=commerce;trusted_connection=true"
Dim CommandText as String = "SELECT CategoryID, CategoryName FROM
Categories"

Sub Page_Load(Sender As Object, E As EventArgs)
Dim myConnection As New SqlConnection(ConnectionString)
Dim myCommand As New SqlCommand(CommandText, myConnection)

myConnection.Open()

DataGrid1.DataSource =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
DataGrid1.DataBind()
End Sub

Function GetCategories() as DataSet
Dim ddlDataSet as DataSet = New DataSet()
Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(CommandText,
ConnectionString)

myDataAdapter.Fill(ddlDataSet, "Categories")

Return ddlDataSet
End Function

</script>

<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
<form runat="server">
<p>
<asp:datagrid id="DataGrid1" runat="server"
EnableViewState="False" ForeColor="Black" BackColor="White" CellPadding="3"
GridLines="None" CellSpacing="1">
<HeaderStyle font-bold="True" forecolor="white"
backcolor="#4A3C8C"></HeaderStyle>
<ItemStyle backcolor="#DEDFDE"></ItemStyle>
</asp:datagrid>
</p>
<p>
<asp:DropDownList id="DropDownList1" runat="server"
DataSource="<%# GetCategories %>" width="200"></asp:DropDownList>
</p>
</form>
</body>
</html>


TIA Iain
 
I

Iain Kirk

Thanks for prompt reply, I have already tried that before posting previously
and tried it once more just to confirm but it still doesn't work, any more
ideas would be great.

Cheers, Iain
 
A

Alvin Bruney [MVP]

your complete code should look like this
dropdown.datatextfield = "CategoryID"
dropdown.datavaluefield="CategoryName"
then bind assuming you have tested the dataset to see if it is not null and
it has at least one row.

if this does not work for you, post your code
 
I

Iain Kirk

Alvin thanks for replying, i tried what you said and i then got an error in
IE that said 'The server tag is not well formed.'. I think i may be missing
the binding bit, how do i test to make sure it is bound and there is data in
the DataSet?

Here is my code to save space i have only included what i believe to be
relevent if you need it all i can post it.

<%@ Page Language="VB" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
Dim ConnectionString As String =
"server=(local);database=commerce;trusted_connection=true"
Dim SQLCmd As String = "select CategoryID as [Cat ID], CategoryName as
[Cat Name] from CMRC_Categories"
Dim SQLDDL as String = "SELECT CategoryID, CategoryName FROM
CMRC_Categories"

Sub Page_Load(Sender As Object, E As EventArgs)
Dim myConnection As New SqlConnection(ConnectionString)
Dim myCommand As New SqlCommand(SQLCmd, myConnection)

myConnection.Open()

DataGrid1.DataSource =
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
DataGrid1.DataBind()
End Sub

Function GetCategories() as DataSet
Dim ddlDataSet as DataSet = New DataSet()
Dim myDataAdapter as SqlDataAdapter = New SqlDataAdapter(SQLDDL,
ConnectionString)

myDataAdapter.Fill(ddlDataSet, "Categories")

Return ddlDataSet
End Function

</script>


<html>
<head>
</head>
<body style="FONT-FAMILY: arial">
<form runat="server">
<p>
<asp:datagrid id="DataGrid1" runat="server"
EnableViewState="False" ForeColor="Black" BackColor="White" CellPadding="3"
GridLines="None" CellSpacing="1">
<HeaderStyle font-bold="True" forecolor="white"
backcolor="#4A3C8C"></HeaderStyle>
<ItemStyle backcolor="#DEDFDE"></ItemStyle>
</asp:datagrid>
</p>
<p>
<asp:DropDownList id="DropDownList1" runat="server"
DataSource="<%# GetCategories() %>" DataTextField="CategoryName"
DataValueField="CategoryID" width="200"></asp:DropDownList>
</p>
</form>
</body>
</html>


Alvin Bruney said:
your complete code should look like this
dropdown.datatextfield = "CategoryID"
dropdown.datavaluefield="CategoryName"
then bind assuming you have tested the dataset to see if it is not null and
it has at least one row.

if this does not work for you, post your code

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
Iain Kirk said:
Thanks for prompt reply, I have already tried that before posting previously
and tried it once more just to confirm but it still doesn't work, any more
ideas would be great.

Cheers, Iain


in
SQL,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top