Hi,
Please add the Where Clause in your query with Key Column on which basis you want to fetch data.
[EDIT]
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=SW-PC-20;Integrated security =SSPI;Initial catalog=institute");
con.Open();
SqlCommand com = new SqlCommand("select * from teacher2", con);
SqlDataReader dr = com.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
dataGridView1.DataSource = dt;
dr.Close();
con.Close();
}
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=SW-PC-20;Integrated security =SSPI;Initial catalog=institute");
con.Open();
SqlCommand com = new SqlCommand("select * from Media_Method where Media_method_ID ='" + dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString() +"'", con);
SqlDataReader dr = com.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
txtteacherid.Text = dr[0].ToString();
txtteachername.Text = dr[1].ToString();
txtdepartment.Text = dr[2].ToString();
txtdescription.Text = dr[3].ToString();
}
dr.Close();
con.Close();
}
You can directly fetch the data from gridview as well no need to go to database for filling the textboxes every time.
Change your code like this and then try whether your goal achieved or not?
I think now you can understand what I want to say?
regards
AR