Click here to Skip to main content
15,896,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
private void BindGrid()
    {
        int selectedRowIndex = GridView1.SelectedIndex;
        int pId = (int)GridView1.DataKeys[selectedRowIndex].Value;//I get error in this line.
        SqlConnection conn;
        SqlCommand comm;
        SqlDataReader reader;
        string connectionString =
        ConfigurationManager.ConnectionStrings[
        "ConnectionString"].ConnectionString;
        conn = new SqlConnection(connectionString);
        comm = new SqlCommand("SELECT ProductId, ProductName, Price FROM Product " +
        "WHERE ProductId=@ProductId", conn);
        comm.Parameters.Add("ProductId", SqlDbType.Int);
        comm.Parameters["ProductId"].Value = pId;
        try
        {
            conn.Open();
            reader = comm.ExecuteReader();
            employeeDetails.DataSource = reader;
            employeeDetails.DataKeyNames = new string[] { "ProductId" };
            employeeDetails.DataBind();
            reader.Close();
        }
        finally
        {
            conn.Close();
        }
    }
Posted
Updated 21-Jan-14 21:23pm
v2

From the documentation[^]:
The zero-based index of the selected row in a GridView control. The default is -1, which indicates that no row is currently selected.


You could modify your code this way:
C#
private void BindGrid()
    {
        int selectedRowIndex = GridView1.SelectedIndex;
        if ( selectedRowIndex < 0) return;
        //...
 
Share this answer
 
Comments
Dhaliwal1 22-Jan-14 4:37am    
thank you for giving me a solution.Please elaborate it because i'm beginner in c# programming and can't rectify my error.
CPallini 22-Jan-14 4:45am    
Well, I gave you a detailed answer. If no index is currently selected in the GridView then the GridView1.SelectedIndex property is -1, this is an error condition for the remaining code. Adding the line
if (selectedRowIndex < 0) return;
prevents the execution of the remaining code.
Dhaliwal1 23-Jan-14 6:38am    
Thank you so much..finally it works..
CPallini 23-Jan-14 6:44am    
You are welcome.
GridView1.SelectedIndex = -1 because no index is selected in the gridview

try the below code...

private void BindGrid()
{
int selectedRowIndex = GridView1.SelectedIndex;
if(selectedRowIndex>0)
{
int pId = (int)GridView1.DataKeys[selectedRowIndex].Value;
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
string connectionString =
ConfigurationManager.ConnectionStrings[
"ConnectionString"].ConnectionString;
conn = new SqlConnection(connectionString);
comm = new SqlCommand("SELECT ProductId, ProductName, Price FROM Product " +
"WHERE ProductId=@ProductId", conn);
comm.Parameters.Add("ProductId", SqlDbType.Int);
comm.Parameters["ProductId"].Value = pId;
try
{
conn.Open();
reader = comm.ExecuteReader();
employeeDetails.DataSource = reader;
employeeDetails.DataKeyNames = new string[] { "ProductId" };
employeeDetails.DataBind();
reader.Close();
}
finally
{
conn.Close();
}
}
C#


}
 
Share this answer
 
v2
Comments
Dhaliwal1 23-Jan-14 6:39am    
Thank you..

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