Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Please see my code:
public static DropDownList dropDown(string col1,string col2,string tblName,DropDownList ddl)
   {
       SqlConnection con = Database.GetConnection();
       SqlCommand cmd = new SqlCommand("select "+col1.Trim()+","+col2.Trim()+" from " + tblName.Trim() + "", con);
       SqlDataAdapter da = new SqlDataAdapter(cmd);
       DataSet ds = new DataSet();
       da.Fill(ds, tblName);
       ddl.DataSource = ds;
       ddl.DataTextField = col1;
       ddl.DataValueField = col2;
       ddl.DataBind();
       return ddl;
   }


In this sql command i have to declare columns manually. If i need one more columns then this function is not working. How can i solve this?

Thanks in Advance.
Posted
Updated 17-May-11 0:13am
v2
Comments
Sunasara Imdadhusen 17-May-11 6:27am    
Is there any error?
rahul dev123 17-May-11 6:54am    
No
BobJanova 17-May-11 8:36am    
I hope you are not calling this function with user-entered column names or table name, by the way (it is SQL injection vulnerable with unsanitised arguments).

1 solution

Think I know what you mean. The drop down list only really displays 1 column (DataTextField) and uses the other as the value (DataValueField). If you want to display more than 1 column you'll either need to concatenate 1 to n columns as the DataTextField column or use a custom control that will display more than 1 column. You can either create one yourself or download one that someone else has written, either as freeware, shareware or pay for one.
 
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