Click here to Skip to main content
15,898,374 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I am trying to Populating One Dropdownlist with selection of other DropDownlist control .
can't understand what is goin wrong..coz nothing is happening , just first is working well, i.e 1st Dropdown is binding data sucessfully but 2nd part isn't working...plz help...
C#
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            cn.Open()
            boundDp()
            cn.Close()
        End If
End Sub

C#
Public Sub boundDp()
        cmd.Connection = cn
        cmd.CommandText = "Select * From Acct_Sum"
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable
        da.Fill(dt)
        DPA1.DataSource = dt
        DPA1.DataTextField = "Loan_code"
        DPA1.DataValueField = "Loan_code"
        DPA1.DataBind()
        DPA1.Items.Insert(0, "Select Item Here")
End Sub

C#
Public Sub Shw_data()
        cmd.Connection = cn
        cmd.CommandText = "Select * From Acct_Sum Where Loan_code=@Loan_code"
        cmd.Parameters.AddWithValue("@Loan_code", DPA1.SelectedItem.Value)
        Dim dr As SqlDataReader
        dr = cmd.ExecuteReader
        If dr.HasRows Then
            dr.Read()
            TextBox1.Text = dr.Item("Bor_Name").ToString
            TextBox2.Text = dr.Item("coBor_Name").ToString
        End If
End Sub

C#
Public Sub shw_data2()
        cmd.Connection = cn
        cmd.CommandText = "Select Loan_code From Acct_Sum2 Where Loan_code=@Loan_code"
        cmd.Parameters.AddWithValue("@Loan_code", DPA2.SelectedItem.Value)
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable
        da.Fill(dt)
        DPA2.DataSource = dt
        'DPA2.DataTextField = "chk_Num"
        DPA2.DataValueField = "Loan_code"
        DPA2.DataBind()
        DPA2.Items.Insert(0, "Select Sub Item")
End Sub

C#
Private Sub DPA2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DPA2.SelectedIndexChanged
        cn.Open()
       
        shw_data2()
        cn.Close()
End Sub

Note...I have two tables...Acct_Sum and Acct_Sum2 with Unique ID=Loan_code

Thnx....
Posted
Updated 22-Jul-13 1:24am
v2

1 solution

Follow the code below i have made some modifications to make it easier and work. I didnt verified connections etc.

1) Define two dropdown list like below
C#
<asp:dropdownlist id="drp1" runat="server" onselectedindexchanged="drp1_SelectedIndexChanged" autopostback="true" datavaluefield="xyz" datatextfield="xyz" xmlns:asp="#unknown">
</asp:dropdownlist>
<asp:dropdownlist id="drp2" runat="server" datavaluefield="loan_code" datatextfield="loan_code" xmlns:asp="#unknown">    
</asp:dropdownlist>

2)
VB
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            cn.Open()
            boundDp()
            cn.Close()
        End If
End Sub

3)
VB
Public Sub boundDp()
        cmd.Connection = cn
        cmd.CommandText = "Select xyz From Acct_Sum"
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable
        da.Fill(dt)            
        Dim dr As DataRow = dt.NewRow()
        dr("xyz") = "Select Item Here"
        dt.Rows.InsertAt(dr, 0)
        drp1.DataSource = dt 
        drp1.DataBind()        
End Sub

4)
C#
Private Sub drp1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DPA2.SelectedIndexChanged
        cn.Open()       
        cmd.Connection = cn
        cmd.CommandText = "Select loan_code From Acct_Sum2 Where loan_code=@Loan_code"
        cmd.Parameters.AddWithValue("@Loan_code", drp1.SelectedValue)
        Dim da As New SqlDataAdapter(cmd)
        Dim dt As New DataTable
        da.Fill(dt)
        Dim dr As DataRow = dt.NewRow()
        dr("xyz") = "Select Sub Item Here"
        dt.Rows.InsertAt(dr, 0)
        drp2.DataSource = dt        
        drp2.DataBind()
        cn.Close() 
End Sub

Regards..:laugh:
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900