?
Solved

Populate Dropdownlist with data from database and add blank row

Posted on 2008-10-10
6
Medium Priority
?
804 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 4

Accepted Solution

by:
sijishJohn earned 375 total points
ID: 22685604
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
ID: 22685627
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 375 total points
ID: 22685634
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 7

Assisted Solution

by:ajolly
ajolly earned 375 total points
ID: 22685635
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 375 total points
ID: 22685638
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
ID: 31504949
I've chosen to put only a blank row and mantain the control bound to the datasource.
Thanks annyway.

jppinto
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

762 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