This line:
textBox4.Text = dr1[].ToString();
Don't have a column index.
Edit:
Why do you convert to Int32 and use the value as a string?
commandstring = "SELECT * FROM productT WHERE spiceID='" + Convert.ToInt32(textBox1.Text) + "'";
Try this:
commandstring = string.Format("SELECT * FROM productT WHERE spiceID='{0}'", textBox1.Text.Trim());
Just did a "heal" on your ccde, see this:
private void button5_Click(object sender, EventArgs e)
{
if (textBox1.Text.Trim() == string.Empty) return;
int id = 0;
if (int.TryParse(textBox1.Text.Trim(), out id))
{
String commandstring;
SqlDataReader dr1;
SqlCommand com2;
commandstring = string.Format("SELECT * FROM productT WHERE spiceID='{0}'", id);
com2 = new SqlCommand(commandstring, conn1);
try
{
conn1.Open();
}
catch
{
MessageBox.Show("error in seaching", "message", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
}
try
{
dr1 = com2.ExecuteReader();
if (dr1.Read())
{
comboBox1.Text = dr1[1].ToString();
dateTimePicker1.Value = (DateTime)dr1[2];
dateTimePicker2.Value = (DateTime)dr1[3];
textBox2.Text = dr1[4].ToString();
textBox3.Text = dr1[5].ToString();
textBox4.Text = dr1[].ToString();
}
else
{
MessageBox.Show("Record can not found");
}
}
catch
{
if (!dr1.IsClosed) dr1.Close();
conn1.Close();
}
}
}