Abdel Jelil Zaghouani Abdel Jelil Zaghouani -4 years ago 120
C# Question

cannot convert from string to int

i want to convert what i selected from the combobox so i can edit it or delete it, but the messege "cannot convert from string to int" keep showing

if (sqlCon.State == ConnectionState.Closed)
sqlCon.Open();
string Query = "select * from tbl_article where NameArticle='"+comboBoxArt.Text+"'";
SqlCommand cmd = new SqlCommand(Query, sqlCon);
SqlDataReader myReader;
try
{
myReader = cmd.ExecuteReader();
while (myReader.Read())
{
txtName.Text = myReader.GetString("NameArticle");
txtPrice.Text = myReader.GetInt32("PriceArticle").ToString();
}
}


and also when i run it, the selected item changes to his id "IdArticle".
how can i fix this ??

Answer Source

Why not just do this?

txtName.Text = myReader["NameArticle"].ToString();
txtPrice.Text = myReader["PriceArticle"].ToString();

It should get whatever value from database whether its int or DateTime etc then convert it to string?

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download