Click here to Skip to main content
15,913,722 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
.aspx code
ASP.NET
<asp:GridView ID="GridView2" runat="server" GridLines="None" 
                onrowdatabound="GridView2_RowDataBound" align="center" 
        PageSize="4" onpageindexchanging="GridView2_PageIndexChanging" ShowHeaderWhenEmpty="true" AllowPaging="true" >
                <AlternatingRowStyle BackColor="White" />
                <RowStyle BackColor="#CCCCCC" />
            </asp:GridView>



.cs code

C#
protected void Page_Load(object sender, EventArgs e)
        {
            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
Updated 27-Apr-14 21:41pm
v2

hi,
please put your page load code in !ispostback.
ex :
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Recentinternship();
}
}
 
Share this answer
 
Comments
jayraj86 28-Apr-14 3:34am    
I tried that also but I'm getting same error
what you are doing on onrowdatabound="GridView2_RowDataBound" show code

rowdatabound event insert data into gridview row by row and your paging not working might its not geting value.
 
Share this answer
 
Comments
jayraj86 28-Apr-14 3:36am    
here it is


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";
}

}

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