473,395 Members | 1,474 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

DataSet for DropDownList problem

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_connecti on=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.CloseConne ction)
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
Nov 18 '05 #1
4 1532
Set DataTextField and DataValueField
"Iain Kirk" <ia***********@z-uk.com> wrote in message
news:uI****************@TK2MSFTNGP11.phx.gbl...
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_connecti on=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.CloseConne ction)
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

Nov 18 '05 #2
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
"Anatoly" <an*****@hotmail.com> wrote in message
news:u1****************@TK2MSFTNGP09.phx.gbl...
Set DataTextField and DataValueField
"Iain Kirk" <ia***********@z-uk.com> wrote in message
news:uI****************@TK2MSFTNGP11.phx.gbl...
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_connecti on=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.CloseConne ction)
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


Nov 18 '05 #3
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" <ia***********@z-uk.com> wrote in message
news:ed****************@TK2MSFTNGP11.phx.gbl...
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
"Anatoly" <an*****@hotmail.com> wrote in message
news:u1****************@TK2MSFTNGP09.phx.gbl...
Set DataTextField and DataValueField
"Iain Kirk" <ia***********@z-uk.com> wrote in message
news:uI****************@TK2MSFTNGP11.phx.gbl...
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_connecti on=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.CloseConne ction)
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



Nov 18 '05 #4
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_connecti on=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.CloseConne ction)
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 [MVP]" <vapor at steaming post office> wrote in message
news:e5****************@TK2MSFTNGP09.phx.gbl...
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" <ia***********@z-uk.com> wrote in message
news:ed****************@TK2MSFTNGP11.phx.gbl...
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
"Anatoly" <an*****@hotmail.com> wrote in message
news:u1****************@TK2MSFTNGP09.phx.gbl...
Set DataTextField and DataValueField
"Iain Kirk" <ia***********@z-uk.com> wrote in message
news:uI****************@TK2MSFTNGP11.phx.gbl...
> 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_connecti on=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.CloseConne ction)
> 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
>
>



Nov 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Chris Kennedy | last post by:
I am having big problem manipulating XML in a dataset. I am reading in a schema based on the following XML document. The minute I try to e.g. create a new row I get 'Object reference not set to an...
0
by: TonyM | last post by:
Hi All, Please bear with me on this, I'm not 100% sure I know how to ask this clearly. I have a Web Application that I'm trying to keep the items you can select in a DropDownList in a...
3
by: Bill | last post by:
I have a seating chart web form that has over 50 entry field controls (tables/booths) where I use a DropDownList box to select a single company name from a single large list of organizations (200...
0
by: TK | last post by:
Hi, I need help on datagrid. I have a dataset exposed as a webservice. I do have a web application that is supposed to consume the service on a datagrid.
15
by: ruca | last post by:
Hi, Can I read a .TXT File to a DataSet? How can I do that? I want to read his lines to a DropDownList. This lines are the names of employees that I export from an application that I have. I...
4
by: Marty U. | last post by:
I have an assembly that contains a function that returns a dataset. I want to bind that dataset to a dropdownlist. If I do it as such: DataSet ds; ds = myobject.GetDataSet();...
2
by: jdsharp | last post by:
I understand how to bind the Dropdownlist control to a table so that it shows the list of values, but how do I bind it to my main data entry dataset? Thanks for the help! Jason
12
by: Bishoy George | last post by:
I have a dataset called ds1 filled with 2 tables Employees and Customers from Northwind database. I have dropdownList called ddLastName with the following properties: ddLastName.DataSource =...
1
by: Probi | last post by:
HI, I have a DropDownList populated from database I use the following code OracleDataAdapter ad2 = new OracleDataAdapter(cmd2, connection); DataSet ds2 = new DataSet(); ad2.Fill(ds2); ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.