开发者

Bind combobox with data from database

开发者 https://www.devze.com 2023-02-13 22:16 出处:网络
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 .

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

精彩评论

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