Click here to Skip to main content
15,907,281 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
on gridview selectedindexchanged i have folllowing code...but is not working
plz improve it..

C#
String qr = "select * from stusub where StuID='" + gv1.SelectedRow.Cells[1].Text + "' ";
        SqlCommand cm = new SqlCommand(qr, conn);
        conn.Open();
        SqlDataReader dr1 = cm.ExecuteReader();

        DataTable dt1 = new DataTable();
       
        dt1.Load(dr1);

        for (int i = 0; i < dt1.Rows.Count; i++)
        {
            foreach (ListItem item in lbSubject.Items)
            {
                if(dr1.HasRows)
                {
                    item.Selected = true;
                    lbSubject.SelectedValue = Convert.ToString(dt1.Rows[i]["SubID"]);
                }
            }
        }
        conn.Close();
Posted
Updated 6-Sep-12 6:30am
v2
Comments
[no name] 6-Sep-12 12:38pm    
What's the error?
Sergey Alexandrovich Kryukov 6-Sep-12 14:07pm    
No one is going to improve something when you don't even explain your goal and your problem. Use "Improve question" first.
--SA

1 solution

dt1.Load(dr1) is closing the reader after it reads the data in.

Change the line of code from
VB
if(dr1.HasRows)

to
C#
if(dt1.Rows.Count > 0)


See if that solves the problem.
 
Share this answer
 

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