ASP.NET DropDownList by VB.NET

This article describes how to bind DropDownlist with database and also display binding data in GridView.
  • 2198
 

I have a DropDownList, I can bind it to a table and populate its choice items in GridView control. we select item in the DropDownList and match with database table which record match with database. it will display in the GridView control.

Creating Table

create table info

(

username varchar(50),

Email varchar(40)

)

go

insert into info values('andrew','[email protected]')

go

insert into info values('Rahul','[email protected]')

go

insert into info values('Rohatash','[email protected]')

go

insert into info values('yardi','[email protected]')

go

select * from info;

table1.gif
 

Table1
 

For example

 

Drag and Drop one GridView control, one Button control, one label control and one DropDownList control on the form.

 

ASPX code

 

<div >

        <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"

            GridLines="None">

            <AlternatingRowStyle BackColor="White" />

            <EditRowStyle BackColor="#2461BF" />

            <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

            <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />

            <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />

            <RowStyle BackColor="#EFF3FB" />

            <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />

            <SortedAscendingCellStyle BackColor="#F5F7FB" />

            <SortedAscendingHeaderStyle BackColor="#6D95E1" />

            <SortedDescendingCellStyle BackColor="#E9EBEF" />

            <SortedDescendingHeaderStyle BackColor="#4870BE" />

        </asp:GridView>           

        <br />

        <br /> 

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

            <asp:ListItem>rohatash</asp:ListItem>

            <asp:ListItem>andrew</asp:ListItem>

            <asp:ListItem>yardi</asp:ListItem>

            <asp:ListItem>Rahul</asp:ListItem>

            <asp:ListItem>monu</asp:ListItem>

    </asp:DropDownList>

    &nbsp;&nbsp;&nbsp;<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

    &nbsp;&nbsp;&nbsp;&nbsp;

    <br />

    <br />

    <br />

    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="ShowRecord"

        Height="37px" />

    </div>

 

Now double click on the Button Control and add the following code.

 

Protected Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgsHandles Button1.Click

        Dim str As String = "Data Source=.;uid=sa;pwd=Password$2;database=dropdowndatabase"

        Dim con As New SqlConnection(str)

        con.Open()

        Dim cmd As New SqlCommand("select * from info where username = '" + DropDownList1.SelectedItem.Text + "'", con)

        'Dim cmd As New SqlCommand("select * from info")

        Dim Adpt As New SqlDataAdapter(cmd)

        Dim ds As DataSet = New DataSet()

        If (Adpt.Fill(ds, "info")) Then

            GridView1.DataSource = ds.Tables(0)

            GridView1.DataBind()

            Label1.Text = "Found"

            GridView1.Visible = True

        Else

            Label1.Text = "Not found"

            GridView1.Visible = False

        End If

    End Sub

 

Now run the application.


 

table2.gif
 

 

Figure1.

 

Now select username from dropdownlist and click on the ShowRecord Button.


 

table3.gif
 

 

Figure2

 

Now select an item from the DropDownList which is not in database. GridView will be invisible for that selected item. 
 


table4.gif
 

Figure3

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.