Click here to Skip to main content
15,888,102 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to show 2 columns in a DropDownList Menu. To accomplish that,

when I execute the following SQL query in SSMS,

SQL
SELECT (convert(varchar,Cost_ID) +' , '+ Item_Description) FROM Cost_ID;


It brings the desired output. But when I add it in Microsoft Visual Studio 2012 Data-Source, i.e.,

ASP.NET
<asp:SqlDataSource ID="SqlDataSource12" runat="server" ConnectionString="Data Source=MEHDI-PC\SQLEXPRESS;Initial Catalog=PIMS;Integrated Security=True" ProviderName="System.Data.SqlClient" SelectCommand="SELECT (convert(varchar,CostID) +'   ,   '+ Item_Description) FROM Cost_ID"></asp:SqlDataSource>


it gives the following error when I run the program:

HTML
DataBinding: System.Data.DataRowView does not contain a property with the name Cost_ID.


Any idea where I am going wrong?

Thank You.
Posted

1 solution

Not sure if i understand you right,you are trying to show multiple columns in dropdownlist that comes from a database.Is it?
If that then see how it can be done below:

C#
SqlCommand cmd;
SqlConnection con;
SqlDataAdapter da;
DataSet ds;

private void button1_Click(object sender, EventArgs e)
{
    con = new SqlConnection("your_database_connection_sting");
    cmd =new SqlCommand ("select * from emp", con);
    da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);

    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
        comboBox1.Items.Add(ds.Tables[0].Rows[i][0] + " " + ds.Tables[0].Rows[i][1] + " " + ds.Tables[0].Rows[i][2]);
    }
}


Output:
C#
1021 shuvro 4875
1022 ridoy 3675
1023 trena 5575
 
Share this answer
 
v2
Comments
[no name] 28-Jul-13 14:51pm    
Thanks for replying @ridoy.

I got another solution from somewhere else. which is:

SQL statement doesn't provides a column name for CONVERT operation:

SelectCommand="SELECT (convert(varchar,CostID) +' , '+ Item_Description) FROM Cost_ID"

Change it to:

SelectCommand="SELECT (convert(varchar,CostID) +' , '+ Item_Description) Cost_ID FROM Cost_ID"

I was basically trying to show 2 columns as 1 column in DropDownList menu. Thanks

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