Click here to Skip to main content
15,896,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have created a form in which there are 4 labels teacher id ,teacher name and department and description along with respective textboxes.i have inserted the values in the database through ADD button (using insert query).I have taken datagrid view to display the data .The problem is now i want that one i select one row of datagridview then it should display in respective text boxes. i have tried following code but not able to get desired write properly.

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 teacher2", con);
            SqlDataReader dr = com.ExecuteReader();
            DataTable dt = new DataTable();

            dt.Load(dr);
            dr.Close();
            dataGridView1.DataSource = dt;

            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();
}


Please help
me
regards
shivani
Posted
Comments
Ankit Rajput 2-May-11 1:02am    
I think you forget to put where clause in your query.
Please check it once.

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
 
Share this answer
 
v6
Comments
shivani 2013 2-May-11 2:14am    
can u explain in detail
Ankit Rajput 2-May-11 2:20am    
What to explain in it?
"select * from teacher2" its your query. It will select all the data from your table. If you need to pick specific data then you need provide some condition. Like
"select * from teacher2 Where teacherID=1".
shivani 2013 2-May-11 3:05am    
the coding which have given me nw.i have alredy dne this.........my question is wat to write in this ??????? in following code
select * from teacher2 Where teacherID=????"
as i am selecting a particular row of dat grid view
shivani 2013 2-May-11 2:34am    
i want is on loading form whole data should get displayed in data grid view and i am getting this..............but wen i click one particular row of dat grid view then i should get data for respective selected row in respective text boxes......nw wat coding should i give
m mean select * from teacher where teacher_id=???????????as i m selecting particular row from dat grid view.......
Ankit Rajput 2-May-11 2:50am    
First Let me know whether its a Windows Application or Web Application?
If its a Windows Application, Then load all the data for Form Load event.

Do coding for specific value in this event.
Use Array Concept It will Work.

ArrayList ar = new ArrayList();
ar.Add(Session[id]);
ar.Add(Session[ Name]);
ar.Add(Session[dept]);
ar.Add(Session[desc;]);

grd.DataSource = ar;
grd.DataBind();
txtName.Text = Convert.ToString(Session[Name]);

txtdesc.Text = Convert.ToString(Session[desc]);
txtdept.Text = Convert.ToString(Session[dept]);
 
Share this answer
 
v2
Comments
shivani 2013 29-Apr-11 7:46am    
can you explain it briefly
shivani 2013 2-May-11 1:25am    
where to write this code????????????
Hi,

I noticed that you are closing data reader before you read it. And there is no need to bind data to grid view again.

you can modify your code as below

SqlCommand com = new SqlCommand("select * from teacher2", con);
SqlDataReader dr = com.ExecuteReader();
 if (dr.HasRows)
{
while(dr.Read())
{
 txtteacherid.Text = dr[0].ToString();
 txtteachername.Text = dr[1].ToString();
 txtdepartment.Text = dr[2].ToString();
 txtdescription.Text = dr[3].ToString();
}
dr.Close();
}
 
Share this answer
 
v3
Comments
shivani 2013 29-Apr-11 7:43am    
thanx for ur effort but it always display data of last row in text boxes
RaviRanjanKr 29-Apr-11 16:49pm    
Always Wrap your Code in "pre" tags :)
I think below code will work..:):)
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 teacher2", con);
            SqlDataReader dr = com.ExecuteReader();
            DataTable dt = new DataTable();
 
            dt.Load(dr);
            dr.Close();
            dataGridView1.DataSource = dt;
 
            if (dr.HasRows)
 {
                dr.Read();
            txtteacherid.Text = dr[0]["id"].ToString();
            txtteachername.Text = dr[0]["name"].ToString();
            txtdepartment.Text = dr[0]["dept"].ToString();
            txtdescription.Text = dr[0]["description"].ToString();
}
}
 
Share this answer
 
Comments
shivani 2013 29-Apr-11 7:43am    
no,its not working
Simple writing dr.Read() won't help you in reading the values from the Database.
You have to loop through each record using a while statement. Also you are closing the dr before doing the below operation, which is wrong.

Use Ankit's SqlCommand query too.
C#
if (dr.HasRows)
           {
            while(dr.Read())
            {
             txtteacherid.Text = dr[0].ToString();
             txtteachername.Text = dr[1].ToString();
             txtdepartment.Text = dr[2].ToString();
             txtdescription.Text = dr[3].ToString();
            }
           }
           dr.Close();
 
Share this answer
 
v2
Comments
Kalaivanan.E 19-Jul-12 8:01am    
how to access the database?
u can use javascript for this

Its very easy

on page load write this

C#
foreach (GridViewRow row in GridView1.Rows)
{
               Button bk = (Button)row.FindControl(Button2);// Your Edit button
               bk.Attributes.Add(onclick,return getmew(+row.RowIndex+));
}


then in javascript write this

C#
function getmew(dd)
    {
     var b=document.getElementById(GridView1);
     var c=document.getElementById(TextBox1);
     var d=document.getElementById(TextBox2);
     dd=dd+1;
     c.value=document.getElementById(GridView1).rows[dd].cells[2].innerHTML;
     d.value=document.getElementById(GridView1).rows[dd].cells[3].innerHTML;
}


u can use rowdatabound also...do it in your way
good luck....
 
Share this answer
 
v2
Comments
shivani 2013 2-May-11 1:26am    
can u tell how to write javascript code......
Ankit Rajput 2-May-11 2:59am    
Downvoting: Its a windows application. How to use javascript?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900