I want to access my SQL Server database and retrieve the value of some columns with specific id. I get the id as a print the database in a dataGridView table, so the first cell of the row that the user has selected is the id. Here is the code
con3.Open();
if (typeBOX.SelectedIndex == 0)
{
pictureBox1.Image = Organizer.Properties.Resources.b_height_years;
ChartForm.ImageSet = 1;
pictureBox1.Invalidate();
SqlCommand cmd1 = new SqlCommand("select Height,Age from data where Id = '" + dataGridView1.SelectedCells[0].Value.ToString() + "'", con3);
// SqlCommand cmd2 = new SqlCommand("select Age from data where Id = '" + dataGridView1.SelectedCells[0].Value.ToString() + "'", con3);
SqlDataReader reader1 = cmd1.ExecuteReader();
bool flag = false;
while (reader1.Read() && flag == false)
{
string tempHeight = reader1["Height"].ToString();
ChartForm.Height = int.Parse(tempHeight);
string tempAge = reader1["Age"].ToString();
ChartForm.Age = int.Parse(tempAge);
flag = true;
}
}
But when I am trying to run the code I get the error:
System.Data.SqlClient.SqlException: 'Conversion failed when converting the varchar value 'zxxv' to data type int.
'zxxv' is a saved FirstName
in the database but I do not for it in my command cmd1
. I am only accessing the height
and age
which are both integers. I do not know why this is happening.