Click here to Skip to main content
15,888,195 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
part 1) I want to connect DropDownlist box control to SqlDb..tried below code but not working

'cmd.Connection = cn
'cmd.CommandText = "Select * from Acct_Sum"
'dr = cmd.ExecuteReader()
'DPList.DataSource = ds.Tables("Acct_Sum")
'DPList.DataValueField = "coBor_Name"
'DPList.DataTextField = "coBor_Name"
'cn.Close()


part 2)Also I want to change the content of 2 texboxes based on selection of item from dropdownlist.


I found it done the first part using : Choose data source option of DropDownlist,

but not able to do both.i.e part 2


Plz help thnx...
Posted
Updated 5-Jul-13 2:37am
v2

1 solution

Try this...:)


C#
private void FillDropDownList()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);

        SqlCommand cmd = new SqlCommand("Select * from Acct_Sum", con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);

        DataSet ds = new DataSet();
        da.Fill(ds);
       
        DropDownList1.DataTextField = ds.Tables[0].Columns["coBor_Name"].ToString();
        DropDownList1.DataValueField = ds.Tables[0].Columns["coBor_Name"].ToString();

        DropDownList1.DataSource = ds.Tables[0];
        DropDownList1.DataBind();
    }
 
Share this answer
 
Comments
Aman.Jen 5-Jul-13 9:06am    
hi..

I have tried this with vb.net as below....but not working

cn.Open()
cmd.Connection = cn
cmd.CommandText = "Select * from Acct_Sum"
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim ds As DataSet = New DataSet()
da.Fill(ds)
DPList.DataTextField = ds.Tables(0).Columns["coBor_Name"].ToString ----Error : (value type cnnot be coverte to string)
DPList.DataValueField = ds.Tables(0).Columns["coBor_Name"].ToString ----Error : (value type cnnot be coverte to string)
DPList.DataSource = ds.Tables(0)
DPList.DataBind()
Nirav Prabtani 5-Jul-13 9:09am    
you need both of them in to string?????
Aman.Jen 5-Jul-13 9:22am    
yes
Nirav Prabtani 5-Jul-13 9:24am    
can you give me one coBor_Name .
Aman.Jen 5-Jul-13 10:44am    
Hi,
I have solved the first part..with code.

cn.Open()
cmd.Connection = cn
cmd.CommandText = "Select * From Acct_Sum"
dr = cmd.ExecuteReader()
If dr.HasRows Then
While dr.Read()
If Not IsDBNull(dr.Item("coBor_Name")) Then
DPList.Items.Add(dr.Item("coBor_Name"))
End If
End While
End If


now stuck with part2..i.e

I want change value of 2 textboxes on selectedIndex of DropDownlist control...


Plz help
Thnx

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