Solved

Populate Dropdownlist with data from database and add blank row

Posted on 2008-10-10
6
788 Views
Last Modified: 2010-04-21
Hello,

I'm new to ASP so I need help to do the following:

I need to populate a Dropdownlist with data from an Access database table. I can bind the control to a DataSource in design mode but I want to fill the dropdownlist with data using code so that I can add on the first row a blank row or some kind of message like "Choose your Color...". So far I've writen the code below, then I got stucked...

Can you please help me do this?

Thanks,

jppinto
Dim strSql As String = ""

strSql = "SELECT [Descricao] FROM [Leis] ORDER BY [Descricao]"

Dim Conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Programação\AEDemo\db\manpower.mdb")

Dim Cmd As New OleDbCommand(strSql, Conn)

Conn.Open()

Open in new window

0
Comment
Question by:jppinto
6 Comments
 
LVL 4

Accepted Solution

by:
sijishJohn earned 125 total points
Comment Utility
try this..
Dim strSql As String = ""

        strSql = "SELECT [Descricao] FROM [Leis] ORDER BY [Descricao]"

        Dim Conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Programação\AEDemo\db\manpower.mdb")

        Dim Cmd As New OleDbCommand(strSql, Conn)

        Conn.Open()

        DropDownList1.DataSource = Cmd.ExecuteNonQuery()

        DropDownList1.DataBind()

        DropDownList1.Items.Add(New ListItem("Choose your Color...", "-1"))

        Conn.Close()

Open in new window

0
 
LVL 20

Expert Comment

by:informaniac
Comment Utility
Sorry dude have c# code only...
OleDbDataAdapter da = new OleDbDataAdapter();

da.SelectCommand = cmd;
 

DataSet ds = new DataSet();
 

da.Fill(ds);
 

ddl.DataSource = ds.Tables[0];

ddl.DataTextField = "Descricao";

ddl.DataValueField="Descricao";

ddl.DataBind();
 

ListItem li = new ListItem("Select avalue","0");

ddl.Items.Insert(0,li);

Open in new window

0
 
LVL 14

Assisted Solution

by:Binuth
Binuth earned 125 total points
Comment Utility
try this


        Dim strSql As String = ""

        strSql = "SELECT [Descricao] FROM [Leis] ORDER BY [Descricao]"

        Dim Conn As New Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Programação\AEDemo\db\manpower.mdb")

        Dim Cmd As New Data.OleDb.OleDbCommand(strSql, Conn)

        Dim adp As New Data.OleDb.OleDbDataAdapter(Cmd)
 

        Dim dt As New Data.DataTable

        adp.Fill(dt)
 

        DropDownList1.DataSource = dt

        DropDownList1.DataTextField = "Descricao"

        DropDownList1.DataValueField = "Descricao"

        DropDownList1.DataBind()
 

        DropDownList1.Items.Insert(0, "Choose your Color...")

Open in new window

0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 7

Assisted Solution

by:ajolly
ajolly earned 125 total points
Comment Utility
You can add this item at design time, Choose your Color...
and specify AppendDataBoundItems property of the dropdownlist as true.


<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="true">

    <asp:ListItem Value="" Text="Choose your Color...">

    </asp:ListItem>

</asp:DropDownList>

Open in new window

0
 
LVL 20

Assisted Solution

by:informaniac
informaniac earned 125 total points
Comment Utility
Here's the entire code
Dim strSql As String = ""

strSql = "SELECT [Descricao] FROM [Leis] ORDER BY [Descricao]"

Dim Conn As New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;" & "Data Source=C:\Programação\AEDemo\db\manpower.mdb")

Dim Cmd As New OleDbCommand(strSql, Conn)

Conn.Open()
 

Dim da As OleDbDataAdapter =  New OleDbDataAdapter() 

da.SelectCommand = cmd

 

Dim ds As DataSet =  New DataSet() 

 

da.Fill(ds)

 

ddl.DataSource = ds.Tables(0)

ddl.DataTextField = "Descricao"

ddl.DataValueField="Descricao"

ddl.DataBind()

 

Dim li As ListItem =  New ListItem("Select a value","0") 

ddl.Items.Insert(0,li)

Open in new window

0
 
LVL 33

Author Closing Comment

by:jppinto
Comment Utility
I've chosen to put only a blank row and mantain the control bound to the datasource.
Thanks annyway.

jppinto
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now