ASP.Net GridView: Data Source does not support server side paging

navinkuma
 
on Feb 26, 2013 05:50 AM
4030 Views
protected void BtnSave_Click(object sender, EventArgs e)
    {
        
        try
        {
            lblErrorMsg.Text = "";
            GridView1.Visible = true;
            string str = ConfigurationManager.AppSettings["prink"].ToString();
            SqlConnection con = new SqlConnection(str);
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = con;
            con.Open();
            SqlDataReader reader;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "search_EntryDetails";
            if (Convert.ToInt32(ddlRegistrationNumber.SelectedItem.Value)==0)
            {
                cmd.Parameters.AddWithValue("@RegistrationNumber", System.Data.SqlTypes.SqlInt32.Null);
               
            }
            else
            {
                cmd.Parameters.AddWithValue("@RegistrationNumber", ddlRegistrationNumber.SelectedItem.Text);
            }
            if (txtname.Text.Trim() != "")
            {
                cmd.Parameters.AddWithValue("@Name", txtname.Text.Trim());
            }
            else
            {
                cmd.Parameters.AddWithValue("@Name", System.Data.SqlTypes.SqlString.Null);
            }
            if (txtOrganisation.Text.Trim() != "")
            {
                cmd.Parameters.AddWithValue("@Organisation", txtOrganisation.Text.Trim());
            }
            else
            {
                cmd.Parameters.AddWithValue("@Organisation", System.Data.SqlTypes.SqlString.Null);
            }
            if (txtaddress.Text.Trim() != "")
            {
                cmd.Parameters.AddWithValue("@Address", txtaddress.Text.Trim());
            }
            else
            {
                cmd.Parameters.AddWithValue("@Address", System.Data.SqlTypes.SqlString.Null);
            }
            if (txtLatterNumber.Text.Trim() != "")
            {
                cmd.Parameters.AddWithValue("@LatterNumber", txtLatterNumber.Text.Trim());
            }
            else
            {
                cmd.Parameters.AddWithValue("@LatterNumber", System.Data.SqlTypes.SqlString.Null);
            }
            if ( txtDateOFEntry4.Text.Trim() != "")
            {
                cmd.Parameters.AddWithValue("@LatterDate", DateTime.ParseExact(txtDateOFEntry4.Text, "dd/MM/yyyy", System.Globalization.CultureInfo.InvariantCulture));
            }
            else
            { 
                cmd.Parameters.AddWithValue("@LatterDate",System.Data.SqlTypes.SqlDateTime.Null);
            }

            if (Convert.ToInt32(ddlstatename.SelectedItem.Value) != 0)
            {
                cmd.Parameters.AddWithValue("@STATE", ddlstatename.SelectedItem.Text);
            }
            else
            {
                cmd.Parameters.AddWithValue("@STATE", System.Data.SqlTypes.SqlString.Null);
            }
                    
          reader = cmd.ExecuteReader();
             if (reader.HasRows)
               {
                   GridView1.DataSource = reader;
                   GridView1.DataBind();
                    con.Close();
                 //reader = null;
               }
            else
            {
                if (this.GridView1.DataSource != null)
                {
                    this.GridView1.DataSource = null;
                }
                else
                {
                    this.GridView1.DataSource = null;
                    GridView1.Visible = false;
                }
                message = "Date Is Not Found:";
                string strScript = "javascript: alert('" + message + "')";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "strScript", strScript, true);
            }
        }
        catch (Exception ex)
        {

            lblErrorMsg.Text = ex.ToString();
        }
        finally
        {

        }
    }

///

 protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

        GridView1.PageIndex = e.NewPageIndex;
        girdview();

      
    }////  public void girdview()
    {
        string str = ConfigurationManager.AppSettings["prink"].ToString();
        SqlConnection con = new SqlConnection(str);
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from   ENTRY_FORM", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        con.Close();
        GridView1.DataSource = ds;
        GridView1.DataBind();
    }

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mudassar
 
on Feb 26, 2013 05:51 AM

If you want to do paging in GridView you will have to make use of DataTable and SqlDataAdapter instead if DataReader.

Please visit

Paging in ASP.Net GridView Example