Click here to Skip to main content
15,881,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am getting this error on first line of GridView2_RowDataBound which is


string str = " Category :" +........



aspx code
ASP.NET
<asp:GridView ID="GridView2" runat="server" GridLines="None"
               onrowdatabound="GridView2_RowDataBound" AllowPaging="true" align="center"
        onpageindexchanging="GridView2_PageIndexChanging"  PageSize="4" >
               <AlternatingRowStyle BackColor="White" />
               <RowStyle BackColor="#CCCCCC" />
           </asp:GridView>



.cs code

C#
protected void Page_Load(object sender, EventArgs e)
        {
 if (!IsPostBack)
            {
            Recentinternship();
}
}
 
 protected void Recentinternship()
        {
            SqlConnection con = new SqlConnection("Data Source=jayraj-pc\\sqlexpress;Initial Catalog=Internship;Integrated Security=True;Pooling=False");
            con.Open();
            string str = "select B.CompanyLogo as ' ',A.Title,A.InternshipStartDate,A.ApplicationDeadline,A.Duration,A.InternshipCity,A.Category,A.Stipend,A.InternshipID,A.Degree,A.Branch,A.CompanyID,A.Title,A.CompanyName from Internship AS A INNER JOIN  Companies AS B ON A.CompanyID=B.CompanyID ";
            SqlCommand cmd = new SqlCommand(str, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            SqlDataReader dr = cmd.ExecuteReader();
 
            GridView2.DataSource = dr;
            GridView2.DataBind();
            //GridView2.BottomPagerRow.Visible = true;

 
            dr.Close();
 
            con.Close();
 
        }
 

 protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            string str = " Category :" + e.Row.Cells[6].Text + "<br/>Duration :" + e.Row.Cells[4].Text + "<br/>InternshipCity :" + e.Row.Cells[5].Text + "<br/>Stipend :" + e.Row.Cells[7].Text + "<br/>InternshipStartDate :" + e.Row.Cells[2].Text + "<br/>ApplicationDeadline :" + e.Row.Cells[3].Text + "<br/>Required Degree :" + e.Row.Cells[9].Text + "<br/>Required Branch :" + e.Row.Cells[10].Text;
            e.Row.Cells[1].Text = str;
 
            //e.Row.Cells[1].Visible = false;
            //e.Row.Cells[2].Visible = false;
            e.Row.Cells[3].Visible = false;
            e.Row.Cells[4].Visible = false;
            e.Row.Cells[5].Visible = false;
            e.Row.Cells[6].Visible = false;
            e.Row.Cells[7].Visible = false;
            e.Row.Cells[8].Visible = false;
            e.Row.Cells[9].Visible = false;
            e.Row.Cells[10].Visible = false;
            e.Row.Cells[11].Visible = false;
            e.Row.Cells[12].Visible = false;
            e.Row.Cells[13].Visible = false;
 

            if (GridView2.Rows.Count > 0)
            {
                GridView2.HeaderRow.Cells[0].Text = " ";
                GridView2.HeaderRow.Cells[1].Text = " ";
                GridView2.HeaderRow.Cells[2].Text = " ";
 

 

            }
            Image img = new Image();
            img.ID = "abc";
            img.ImageUrl = e.Row.Cells[0].Text;
            e.Row.Cells[0].Controls.Add(img);
 
            Button btn = new Button();
            //btn.Text = "preview";
            e.Row.Cells[2].Controls.Add(btn);
            btn.CausesValidation = false;
            btn.Text = "View and Apply";
            if (Session.Count > 0)
            {
 

                btn.PostBackUrl = "Apply_internship.aspx?InternshipID=" + e.Row.Cells[8].Text + "&CompanyID=" + e.Row.Cells[11].Text + "&Title=" + e.Row.Cells[12].Text + "&CompanyName=" + e.Row.Cells[13].Text + "";
            }
 
            else
            {
 
                btn.PostBackUrl = "Apply_login.aspx";
            }
 
        }
 
  protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            Recentinternship();
        }
Posted
Comments
King Fisher 5-May-14 1:28am    
Check you have valid rowindex Value at RowDatabound.
[no name] 7-May-14 12:45pm    
Have you debugge it. Can you specify exact line where error is coming..then we can determine it. Because code looks fine.
jayraj86 7-May-14 13:27pm    
yes i debugged..

I am getting this error on first line of GridView2_RowDataBound which is


string str = " Category :" +........

C#
string str = " Category :" + e.Row.Cells[6].Text + "<br/>Duration :" + e.Row.Cells[4].Text + "<br/>InternshipCity :" + e.Row.Cells[5].Text + "<br/>Stipend :" + e.Row.Cells[7].Text + "<br/>InternshipStartDate :" + e.Row.Cells[2].Text + "<br/>ApplicationDeadline :" + e.Row.Cells[3].Text + "<br/>Required Degree :" + e.Row.Cells[9].Text + "<br/>Required Branch :" + e.Row.Cells[10].Text;

Here you are reading values of particular Cells by index and I guess, you have written some invalid index. Note that index always starts from 0.

While debugging check each value in Immediate Window to know if any of these Cell Index is right or wrong.
 
Share this answer
 
See you are missing one line inside this event
C#
protected void GridView2_RowDataBound(object sender, GridViewRowEventArgs e)
{//You are missing this line here
if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Now you can get the lines
      string str = " Category :" + e.Row.Cells[6].Text + "<br />Duration :" + e.Row.Cells[4].Text + "<br />InternshipCity :" + e.Row.Cells[5].Text + "<br />Stipend :" + e.Row.Cells[7].Text + "<br />InternshipStartDate :" + e.Row.Cells[2].Text + "<br />ApplicationDeadline :" + e.Row.Cells[3].Text + "<br />Required Degree :" + e.Row.Cells[9].Text + "<br />Required Branch :" + e.Row.Cells[10].Text;
            e.Row.Cells[1].Text = str;
 
            //e.Row.Cells[1].Visible = false;
            //e.Row.Cells[2].Visible = false;
            e.Row.Cells[3].Visible = false;
            e.Row.Cells[4].Visible = false;
            e.Row.Cells[5].Visible = false;
            e.Row.Cells[6].Visible = false;
            e.Row.Cells[7].Visible = false;
            e.Row.Cells[8].Visible = false;
            e.Row.Cells[9].Visible = false;
            e.Row.Cells[10].Visible = false;
            e.Row.Cells[11].Visible = false;
            e.Row.Cells[12].Visible = false;
            e.Row.Cells[13].Visible = false;
 
 
            if (GridView2.Rows.Count > 0)
            {
                GridView2.HeaderRow.Cells[0].Text = " ";
                GridView2.HeaderRow.Cells[1].Text = " ";
                GridView2.HeaderRow.Cells[2].Text = " ";
 
 
 
 
            }
            Image img = new Image();
            img.ID = "abc";
            img.ImageUrl = e.Row.Cells[0].Text;
            e.Row.Cells[0].Controls.Add(img);
 
            Button btn = new Button();
            //btn.Text = "preview";
            e.Row.Cells[2].Controls.Add(btn);
            btn.CausesValidation = false;
            btn.Text = "View and Apply";
            if (Session.Count > 0)
            {
 
 
                btn.PostBackUrl = "Apply_internship.aspx?InternshipID=" + e.Row.Cells[8].Text + "&CompanyID=" + e.Row.Cells[11].Text + "&Title=" + e.Row.Cells[12].Text + "&CompanyName=" + e.Row.Cells[13].Text + "";
            }
 
            else
            {
 
                btn.PostBackUrl = "Apply_login.aspx";
            }
 
    }
}

Now see the code. Now I have given a rowtype condition
Get more details
msdn[^]
 
Share this answer
 
Comments
jayraj86 7-May-14 14:26pm    
tried it changes entire view of gridview.. which i dont want
[no name] 7-May-14 21:27pm    
is it showing indexing error?
jayraj86 8-May-14 1:06am    
no..but it changes the layout and doesn't show paging at bottom
[no name] 8-May-14 1:33am    
If you are going to work in rowdatabound and need change anything inside cell of gridview then you have to define your rowtype in this event. right.

see if index error is not coming then this topic is over. But your layout is getting changed due to you have done so many codes inside your rowdatabound. which is changing your format at this event time. Try to understand what is your business logic as per that it's defined or not you have to check that and you can do that keep confidence.

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