Click here to Skip to main content
16,018,904 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is my code
C#
public void bindLead()
{
    SqlConnection con = new SqlConnection(cn.ConnectionStrings);
    con.Open();
    SqlCommand cmd = new SqlCommand("SELECT [LedId],[FirstName],[Company],[ProductInterest],[PotentialRevenue],[Date],[Status],[Owner]  FROM [WebCrm].[dbo].[tbl_Lead]", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    da.Fill(ds);
    con.Close();
    if (ds.Tables[0].Rows.Count > 0)
    {
        grdLead.DataSource = ds;
        grdLead.DataBind();
    }
    else
    {
        ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
        grdLead.DataSource = ds;
        grdLead.DataBind();
        int columncount = grdLead.Rows[0].Cells.Count;
        grdLead.Rows[0].Cells.Clear();
        grdLead.Rows[0].Cells.Add(new TableCell());
        grdLead.Rows[0].Cells[0].ColumnSpan = columncount;
        grdLead.Rows[0].Cells[0].Text = "No Records Found";
    }
}
protected void grdLead_RowEditing(object sender, GridViewEditEventArgs e)
{
    grdLead.EditIndex = e.NewEditIndex;
    bindLead();
}

protected void grdLead_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    grdLead.EditIndex = -1;
    bindLead();
}

protected void grdLead_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    grdLead.PageIndex = e.NewPageIndex;
    bindLead();
}

protected void grdLead_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    SqlConnection con = new SqlConnection(cn.ConnectionStrings);
    GridViewRow row = (GridViewRow)grdLead.Rows[e.RowIndex];
    Label lblLedId = (Label)row.FindControl("lblLedId");
    con.Open();
    SqlCommand cmd = new SqlCommand("DELETE FROM [WebCrm].[dbo].[tbl_Lead] WHERE LedId='" + Convert.ToInt32(grdLead.DataKeys[e.RowIndex].Value.ToString()) + "'", con);
    cmd.ExecuteNonQuery();
    con.Close();
    bindLead();
}
protected void grdLead_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    int LedId = Convert.ToInt32(grdLead.DataKeys[e.RowIndex].Value.ToString());
    GridViewRow row = (GridViewRow)grdLead.Rows[e.RowIndex];
    Label lblLedId = (Label)row.FindControl("lblLedId");
    //TextBox txtname=(TextBox)gr.cell[].control[];
    TextBox textLead = (TextBox)row.Cells[0].Controls[0];
    TextBox textCompany = (TextBox)row.Cells[1].Controls[0];
    TextBox textProduct = (TextBox)row.Cells[2].Controls[0];
    TextBox textRevenue = (TextBox)row.Cells[3].Controls[0];
    TextBox textDate = (TextBox)row.Cells[4].Controls[0];
    TextBox textStatus = (TextBox)row.Cells[5].Controls[0];
    TextBox textOwner = (TextBox)row.Cells[6].Controls[0];
    //TextBox textadd = (TextBox)row.FindControl("txtadd");
    //TextBox textc = (TextBox)row.FindControl("txtc");
    grdLead.EditIndex = -1;
    con.Open();
    //SqlCommand cmd = new SqlCommand("SELECT * FROM detail", conn);
    SqlCommand cmd = new SqlCommand("update detail set FirstName='" + textLead.Text + "',Company='" + textCompany.Text + "',ProductInterest='" +textProduct.Text +"',PotentialRevenue='" + textRevenue.Text+ "',Date='" + textDate.Text + "',Status='" + textStatus.Text+ "',Owner='"+textOwner.Text+"'", con);
    cmd.ExecuteNonQuery();
    con.Close();
    bindLead();
}
Posted
Updated 15-Oct-15 20:35pm
v2
Comments
Tomas Takac 16-Oct-15 2:35am    
What line throws the exception?

1 solution

Try to enclose your row count clause in a table count clause.
Something like:
C#
if (ds.Tables.Count > 0)
{
   if (ds.Tables[0].Rows.Count > 0)
   {
      // ...
   }
   else
   {
      // ...
   }
}

and continue from there and see if you still have an IndexOutOfRangeException.
 
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