开发者

Bind combobox with data from database

I had windows form and I added combobox which bin data from database I added my code but this error apeared (invalid column name Category) altought the name was right .

public Category()
{
    InitializeComponent();
    CategoryParent();
}

private void CategoryParent()
{
    using (SqlConnection Con = GetConnection())
    {
        SqlDataAdapter da = new SqlDataAdapter("Select Category.Category,Category.Id from Category", Con);
        DataTable dt = new DataTable();
        da.Fill(dt);
        CBParent.DataSource = dt;
        CBParent.DisplayMember开发者_StackOverflow社区 = "Category";
        CBParent.ValueMember = "Id";
    }
}


Change your query like this,

   Select Category.Category as CatName ,Category.Id from Category

i-e use an alias like "CatName" for your column and set Display member like this,

      CBParent.DisplayMember = "CatName";

Hope it shall help.


I checked the connection and I found the connectionstring was wrong

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜