Click here to Skip to main content
15,908,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want short list rows in gridView by selecting values from dropdown list...problem arises when i have a vsalue in dropDown but not in gridview than it Give me error "
C#
index 0 is either negative or above rows count

"

What I have tried:

C#
protected void ddlAllEngineerSolver_SelectedIndexChanged(object sender, EventArgs e)
    {
        DataView productsTable = (DataView)
               SqlDataSourceAssignedComplains.Select(DataSourceSelectArguments.Empty);
        productsTable.RowFilter = string.Format("EngName = '{0}'",
            ddlAllEngineerSolver.SelectedItem);
        DataRowView row1 = null;
        if ((row1 = (DataRowView)productsTable[0]).ToString()!="0")
        {
             DataTable dataTable = row1.DataView.ToTable();
            GridView1.DataSource = dataTable;
            GridView1.DataBind();
        }
      
    }
Posted
Updated 16-Jun-16 6:30am

Before using productsTable[0] check if there are rows in product table.

C#
if (productsTable != null && productsTable.Rows.Count > 0 && (row1 = (DataRowView)productsTable[0]).ToString()!="0")
 
Share this answer
 
I would agree that checking the Row Count can prevent the issue you are getting. You could actually rewrite your code to this:

C#
protected void ddlAllEngineerSolver_SelectedIndexChanged(object sender, EventArgs e){
        DataView productsTable = (DataView)
        SqlDataSourceAssignedComplains.Select(DataSourceSelectArguments.Empty);
        productsTable.RowFilter = string.Format("EngName = '{0}'",
            ddlAllEngineerSolver.SelectedItem);

 	    DataTable dtResult = productsTable.ToTable();
        if (dtResult.Rows.Count > 0)
        {
            GridView1.DataSource = dtResult
            GridView1.DataBind();
        }
	    else{
		         //display no result found
	    }   
}
 
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