all groups > asp.net > february 2004 >
You're in the

asp.net

group:

Re: DataSet for DropDownList problem



Re: DataSet for DropDownList problem Iain Kirk
2/23/2004 11:16:15 PM
asp.net: 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>


[quoted text, click to view]


Re: DataSet for DropDownList problem Alvin Bruney [MVP]
2/25/2004 8:36:36 PM
do this
DataGrid1.DataSource = GetCategories()
DataGrid1.DataBind();

get categories already returns a dataset so just bind the result to the
grid. if it is empty, there will be no data, otherwise there will be a grid
with data in it.
--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
[quoted text, click to view]

AddThis Social Bookmark Button