Click here to Skip to main content
15,888,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Can someone help me correct this code!
C#
public DataView binddata()
      {

          string ConnectionString = ConfigurationManager.ConnectionStrings["lawrece"].ConnectionString;
          SqlConnection con = new SqlConnection(ConnectionString);

          String StrQuery = "select * from TableName";
          SqlDataAdapter adp = new SqlDataAdapter(StrQuery, con);
          DataSet ds = new DataSet();
          adp.Fill(ds);
          if (ViewState["sortExpr"] != null)
          {
              DataView dv = new DataView(ds.Tables[0]);
              dv.Sort = (string)ViewState["sortExpr"];
          }

          else
          {
              DataView dv = ds.Tables[0].DefaultView;
              return dv;
          }


      }
 protected void GridView2_PageIndexChanging(Object sender, GridViewPageEventArgs e)
      {
          GridView2.PageIndex = e.NewPageIndex;
          GridView2.DataSource = bindgrid();
          GridView2.DataBind();
      }

But when i build the above i get these errors
C#
'lawrence.open.Default.bindgrid()': not all code paths return a value'

Am i missing a line on that code? The aim is to enable paging on my grid view so that when the page number is clicked the page will open.

If there is another to do that i will appreciate....thanks
Posted
Updated 6-Aug-13 2:50am
v2

1 solution

Hi,

see my comments on your code

C#
public DataView binddata()
        {
 
            string ConnectionString = ConfigurationManager.ConnectionStrings["lawrece"].ConnectionString;
            SqlConnection con = new SqlConnection(ConnectionString);
 
            String StrQuery = "select * from TableName";
            SqlDataAdapter adp = new SqlDataAdapter(StrQuery, con);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            DataView dv = new DataView();    // create dataview object here
            if (ViewState["sortExpr"] != null)
            {
                dv = new DataView(ds.Tables[0]);
                dv.Sort = (string)ViewState["sortExpr"];
            }
 
            else
            {
                dv = ds.Tables[0].DefaultView;
                
            }
 
             return dv;   //return outside of if,else
        }


Hope it helps
 
Share this answer
 
Comments
Difameg Network Nigeria 6-Aug-13 9:03am    
it built but this error is poping up,
The GridView 'GridView2' fired event PageIndexChanging which wasn't handled.

do i need to do anything on the gridview, all i did on the gridview was to enable paging to be true
Harshil_Raval 6-Aug-13 9:09am    
Your error shows that you have call event from design side but you have not add it in code behind. you need to add PageIndexChanging event on code behind file. like in your example protected void GridView2_PageIndexChanging(Object sender, GridViewPageEventArgs e). hope you have not delete this on code behind. Because it must be there when you call it from design side.
Difameg Network Nigeria 6-Aug-13 9:24am    
thank you i have it ow, but it is now showing me error of Invalid object name 'TableName'.

How do i correct that please, thank you

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