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>
<aspropDownList id="DropDownList1" runat="server"
DataSource="<%# GetCategories %>" width="200"></aspropDownList>
</p>
</form>
</body>
</html>
TIA Iain
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>
<aspropDownList id="DropDownList1" runat="server"
DataSource="<%# GetCategories %>" width="200"></aspropDownList>
</p>
</form>
</body>
</html>
TIA Iain