Click here to Skip to main content
15,891,633 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,
I created one web page and that page in i writed code like below

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Gridpage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadData();
BindGridView();
}
}
private void LoadData()
{
string constr = "Data Source=.;Database=abcClassifieds;user id=sa;password=123;";
string query = "SELECT ID, ContactPhoto, Title, Description, Price, City, DateAdded FROM productDetailsTable where categories='education'";

SqlDataAdapter da = new SqlDataAdapter(query, constr);
DataTable table = new DataTable();
da.Fill(table);
GridView1.DataSource = table;
GridView1.DataBind();
}
protected void BindGridView()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["abcConnectionString"].ConnectionString);
SqlCommand cmd = new SqlCommand();
SqlDataReader sdr = null;
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;

switch (DropDownList1.SelectedValue)
{
case "High To Low":
cmd.CommandText = "Select * From productDetailsTable where categories='education' order by price desc";
break;
case "Low To High":
cmd.CommandText = "select * from productDetailsTable where categories='education' order by price asc";
break;
case "Recently Ads":
cmd.CommandText = "select * from productDetailsTable where categories='education' order by id desc";
break;
default:
break;
}
try
{
sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
}
finally
{
GridView1.DataSource = sdr;
GridView1.DataBind();

if (!sdr.IsClosed)
{
sdr.Dispose();
sdr.Close();
}
con.Close();
con.Dispose();
}
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
BindGridView();
}
}


here got a small error like :The data source does not support server-side data paging.

how resolve this problem please reply me
Posted
Comments
Tom Marvolo Riddle 28-Mar-14 0:20am    
May i know what's the reason for rejecting the accepted answer?
member258 28-Mar-14 1:15am    
this page contain gridview and dropdownlist,
once selecteditem in dropdownlist high to low and low to high coming properly
but here in gridview with dropdownlist and paging is not coming properly output
thats am rejected please dont mind jas
member258 28-Mar-14 1:19am    
Dear Jas dont mind please
Tom Marvolo Riddle 28-Mar-14 1:26am    
Hi no worries,it's not an issue.Did u see my last comment yesterday?
The comment clearly shows that in selectedindexchanged event of dropdownlist according to the selected value gridview will be bind.

In paging bind the recent method().If you have any issues feel free to ask

I guess the answer is clear enough .Then it's upto you and cheers :)
member258 28-Mar-14 2:21am    
Dear Jas please check this code here cmd error

public partial class Gridpage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadData();
BindGridView();
}
}
private void LoadData()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["abcConnectionString"].ConnectionString);

string query = "SELECT ID, ContactPhoto, Title, Description, Price, City, DateAdded FROM productDetailsTable where categories='education'";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();

da.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}

protected void BindGridView()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["abcConnectionString"].ConnectionString);

if (DropDownList1.SelectedItem.Text == "High To Low")
{
SqlCommand cmd = new SqlCommand("Select * From productDetailsTable where categories='education' order by price desc", con);
}
else if (DropDownList1.SelectedItem.Text == "Low To High")
{
SqlCommand cmd = new SqlCommand("select * from productDetailsTable where categories='education' order by price asc", con);
}
else if (DropDownList1.SelectedItem.Text == "Recently Ads")
{
SqlCommand cmd = new SqlCommand("select * from productDetailsTable where categories='education' order by id desc", con);
}

SqlDataAdapter adap = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
adap.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void DropDownList1_SelectedIndexChanged1(object sender, EventArgs e)
{
BindGridView();
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGridView();
}
}

1 solution

C#
try
{
sdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);//Use Dataset/Datatable 
}
finally
{
GridView1.DataSource = sdr;
GridView1.DataBind();
 }


Don't use datareader when binding with data controls.
 
Share this answer
 
Comments
member258 27-Mar-14 5:20am    
these line am already used please check my code reply me please
Tom Marvolo Riddle 27-Mar-14 5:23am    
Do you see the comments in the code block?. You are using DataReader sdr. Get the values in DataSet or DataTable and bind it with gridview
member258 28-Mar-14 2:50am    
Jas got output thank you jas thank you so much
member258 28-Mar-14 2:33am    
the name cmd does not exist in the current context

this is the error
member258 27-Mar-14 5:23am    
here how i can modify please reply me

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