Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All,

I am binding combobox at runtime with table named tblcty (which has records) but not showing in combobox.

SqlDataAdapter adp = new SqlDataAdapter("Select * from tblcty", con);
DataSet ds = new DataSet();
adp.Fill(ds);
cbfrom.DataSource = ds.Tables["tblcty"];
cbfrom.DisplayMember= "CtyName";
cbfrom.ValueMember = "CtyID";     


Regards!
Aman
Posted

See your code here.

MIDL
adp.Fill(ds);
cbfrom.DataSource = ds.Tables["tblcty"];


dataset is not setting table name itself based on table name in db you're retrieving.

You need to define it explicitly, In other way if you aren't defining it then you simply fetch table from dataset using indexing.

So Update your snippet with.

MIDL
adp.Fill(ds);
cbfrom.DataSource = ds.Tables[0];


OR

MIDL
adp.Fill(ds,"tblcty");
cbfrom.DataSource = ds.Tables["tblcty"];



Hope it helps.
 
Share this answer
 
v2
Comments
Rajesh Anuhya 19-Jan-11 7:49am    
It's a Good answer, Don't know how it's down voted??
Rajesh Anuhya 19-Jan-11 7:49am    
Take my +5
Hiren solanki 19-Jan-11 7:51am    
Thanks rajesh, I don't know but many of my answer getting downvoted today for no reason. even accepted like this too and I've requested for that in site/bugs.
Try these.It may help

1.Check if ds is empty or not.
2.try ds.Tables[0]; instead of ds.Tables["tblcty"];
3.Check DisplayMember name & Valuemember name is the same column name in dataset.
 
Share this answer
 

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