-1

I had opened a related topic before, but I realized the problem now. When I set allow paging correctly in the properties of gridview in web form.aspx, I get this error. I don't know if the codes I wrote in aspx.cs browser cause this problem, please help

''' public partial class WebForm1 : System.Web.UI.Page { SqlConnection con = new SqlConnection("Data Source=MERIH-PC;Initial Catalog=aspidus;Integrated Security=True"); protected void Page_Load(object sender, EventArgs e) {

        if (!IsPostBack)
        {
            GVbind();
        }
    }

    void clear()
    {
        txtName.Text = "";
        txtPhone.Text = "";
        txtAdd.Text = "";
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {

        con.Open();
        SqlCommand cmd = new SqlCommand(@"INSERT INTO [dbo].[idus] VALUES ('" + txtName.Text + "', '" + txtPhone.Text + "', '" + txtAdd.Text + "')", con);
        int t = cmd.ExecuteNonQuery();
        if (t > 0)
        {
            Response.Write("<script>alert('Data inserted successfully') </script>");
            con.Close();
        }
        GVbind();
        clear();

    }

    //protected void btnDelete_Click(object sender, EventArgs e)
    //{
    //    SqlCommand cmd = new SqlCommand(@"DELETE FROM [dbo].[idus]
    //    WHERE [ID]='" + txtID.Text + "'", con);
    //    con.Open();
    //    cmd.ExecuteNonQuery();
    //    Response.Write("Data deleted successfully");
    //    con.Close();
    //}

    //protected void btnUpdate_Click(object sender, EventArgs e)
    //{
    //    SqlCommand cmd = new SqlCommand(@"UPDATE [dbo].[idus]
    //    SET[ID] = '" + txtID.Text + "',[name] = '" + txtName.Text + "',[phone] = '" + txtPhone.Text + "',[address] = '" + txtAdd.Text + "' WHERE [ID]= '" + txtID.Text + "'", con);
    //    con.Open();
    //    cmd.ExecuteNonQuery();
    //    Response.Write("Data updated successfully");
    //    con.Close();
    //}

    protected void GVbind()
    {
        con.Open();
        SqlCommand cmd = new SqlCommand("select * from [dbo].[idus]", con);
        SqlDataReader dr = cmd.ExecuteReader();

        if (dr.HasRows == true)
        {
            GridView1.DataSource = dr;
            GridView1.DataBind();
        }
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        GVbind();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        int ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        string name = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        string phone = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        string address = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;

        con.Open();
        SqlCommand cmd = new SqlCommand("update [dbo].[idus] set name='" + name + "', phone='" + phone + "', address='" + address + "' where ID = '" + ID + "'", con);
        int t = cmd.ExecuteNonQuery();
        if (t > 0)
        {
            con.Close();
            Response.Write("<script>alert('Data has been updated') </script>");
            GridView1.EditIndex = -1;
            GVbind();
        }
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        GVbind();

    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        con.Open();
        int id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
        SqlCommand cmd = new SqlCommand("delete from [dbo].[idus] where ID='" + id + "'", con);
        int t = cmd.ExecuteNonQuery();
        if (t > 0)
        {
            con.Close();
            Response.Write("<script>alert('Data has been deleted') </script>");
            GridView1.EditIndex = -1;
            GVbind();
        }
    }
    protected void DisplayData()
    {
        SqlConnection con = new SqlConnection("Data Source=MERIH-PC;Initial Catalog=aspidus;Integrated Security=True");
        DataTable dt = new DataTable();
        SqlDataAdapter da = new SqlDataAdapter("select * from [dbo].[idus]", con);
        con.Open();
        da.Fill(dt);
        con.Close();
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }

    protected void GridView1_PageIndexChanging1(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        GVbind();

''' enter image description here

enter image description here

  • I hope this code never gets to production. You have many SQL injection attacks ! – Neil Mar 02 '22 at 18:39
  • Does this answer your question? [The data source does not support server-side data paging](https://stackoverflow.com/questions/1661292/the-data-source-does-not-support-server-side-data-paging) – quaabaam Mar 02 '22 at 19:05
  • Basically, you are binding to a DataReader but you need to bind your GridView to an ICollection based object to implement paging in the GridView. [The data source does not support server-side data paging](https://www.aspsnippets.com/Articles/ASPNet-GridView-The-data-source-does-not-support-server-side-data-paging.aspx) – quaabaam Mar 02 '22 at 19:11
  • Including code that is commented out in your post just adds noise to your question and will cause potential responders to just skip your question. – quaabaam Mar 02 '22 at 19:18

1 Answers1

0

You are assigning the GV a "reader", and you can't use a reader - you have to fill a table, or use some other ennumberable collection. Say like a data table.

So, this code:

protected void GVbind()
{
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from [dbo].[idus]", con);
    SqlDataReader dr = cmd.ExecuteReader();

    if (dr.HasRows == true)
    {
        GridView1.DataSource = dr;
        GridView1.DataBind();
    }
}

Change to :

protected void GVbind()
{
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from [dbo].[idus]", con);
    Datatable dt = new dt();
   
    dt.load(cmd.ExecuteReader());
    GridView1.DataSource = dt;
    GridView1.DataBind();
 }

So, while you can "shove" the GV to a reader directly? If you going to use paging, then you can't shove into the GV a reader - since paging does not work with a non innumerable type of data set (like a reader).

So, just load up a data table. And note how I did not even need a data adaptor to load up a data table (the data table has a .Load command for you. (so, you can shorten your other code this way also).

Albert D. Kallal
  • 42,205
  • 3
  • 34
  • 51