Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
this is .aspx code

ASP.NET
<asp:GridView ID="GridView2" runat="server" GridLines="None" 
                onrowdatabound="GridView2_RowDataBound" align="center"  
                AllowPaging="True" PageSize="3" 

                
                
                onpageindexchanging="GridView2_PageIndexChanging" AllowSorting="True" >
                
                <AlternatingRowStyle BackColor="White" />
                <RowStyle BackColor="#CCCCCC" />
                
            </asp:GridView>



this is binding data code in C#

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_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView2.PageIndex = e.NewPageIndex;
            Recentinternship();
        }
Posted

You have to change Source page code here..
onpageindexchanging = "GridView2_PageIndexChanging" to
OnPageIndexChanging = "GridView2_PageIndexChanging"
C#
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(str,con);
            DataSet ds = new DataSet();
            da.fill(ds,"0");
            
          //  SqlDataReader dr = cmd.ExecuteReader();
 
            GridView2.DataSource = ds.Tables["0"].ToString();
            GridView2.DataBind();
            //GridView2.BottomPagerRow.Visible = true;

 
            dr.Close();
 
            con.Close();
 
        }
 
Share this answer
 
v2
Comments
jayraj86 15-Apr-14 1:43am    
Same error
Siva Hyderabad 15-Apr-14 1:47am    
once debug your code..it is fired or not...other wise, remove the code...once again add onpageindexchanging event by using gridview properity,event option..
jayraj86 15-Apr-14 2:12am    
done.. same error!
Tom Marvolo Riddle 15-Apr-14 2:30am    
It won't work
Siva Hyderabad 15-Apr-14 2:31am    
change this
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(str,con);
DataSet ds = new DataSet();
da.fill(ds,"0");

// SqlDataReader dr = cmd.ExecuteReader();

GridView2.DataSource = ds.Table["0"].ToString();
GridView2.DataBind();
//GridView2.BottomPagerRow.Visible = true;


dr.Close();

con.Close();

}
Check this out:GridView Paging and Sorting[^]
 
Share this answer
 
Use Dataset/Datatable for binding with gridview.

DataReader doesn't allow paging because its forward only
 
Share this answer
 
Comments
jayraj86 15-Apr-14 2:36am    
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 B.CompanyID=A.CompanyID ";
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
//SqlDataReader dr = cmd.ExecuteReader();
da.Fill(dt);
if (dt.Rows.Count > 0)
{

GridView2.DataSource = dt;
GridView2.DataBind();
}
//GridView2.BottomPagerRow.Visible = true;




con.Close();

}


I changed code to like this but the problem i am getting is no data is loding in gridview.
Tom Marvolo Riddle 15-Apr-14 2:51am    
check whether the query is returning values or not
jayraj86 15-Apr-14 3:39am    
yes its returning
Tom Marvolo Riddle 15-Apr-14 3:50am    
put a breakpoint here: GridView2.DataSource = dt; and view it in Quickwatch

If dt has values then gridview must show the value(returning column name must match with gridview row name)
write OnPageIndexChanging instead of onpageindexchanging..and use dataset and datatable for gridview binding..

XML
<asp:GridView ID="GridView2" runat="server" GridLines="None"
                onrowdatabound="GridView2_RowDataBound" align="center"
                AllowPaging="True" PageSize="3"



                 OnPageIndexChanging="GridView2_PageIndexChanging" AllowSorting="True" >

                <AlternatingRowStyle BackColor="White" />
                <RowStyle BackColor="#CCCCCC" />

            </asp:GridView>
 
Share this answer
 
v2
Comments
jayraj86 15-Apr-14 2:38am    
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 B.CompanyID=A.CompanyID ";
DataTable dt = new DataTable();
SqlCommand cmd = new SqlCommand(str, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
//SqlDataReader dr = cmd.ExecuteReader();
da.Fill(dt);
if (dt.Rows.Count > 0)
{

GridView2.DataSource = dt;
GridView2.DataBind();
}
//GridView2.BottomPagerRow.Visible = true;




con.Close();

}


I changed code to like this but the problem i am getting is no data is loding in gridview.

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