Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to filter the data in dataset and that data display in gridview.
I have tried with

C#
public DataSet Ds
            {
                get
                {
                    object temp = ViewState["Ds"];
                    return temp == null ? null : temp as DataSet;
                }
                set
                {
                    ViewState["Ds"] = value;
                }
            }
    
            public void gridCustBind(int pageIndex)
            {
                try
                {
                    //open the db connection if it is closed...  
                    if (connection.State == ConnectionState.Closed)
                        connection.Open();
                    command = new SqlCommand();
                    command.CommandText = "sp_Get_CustInfoSerach";
                    command.CommandType = CommandType.StoredProcedure;
                    command.Connection = connection;
                    command.Parameters.AddWithValue("@PageIndex", pageIndex);
                    command.Parameters.AddWithValue("@PageSize", int.Parse(ddlPaging.SelectedValue));
                    command.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
                    command.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
    
                    //SqlParameter outParam = command.Parameters.Add("@bal", SqlDbType.Float);
                    //outParam.Direction = ParameterDirection.Output;
    
                    command.Parameters.Add("@bal", SqlDbType.Int, 4);
                    command.Parameters["@bal"].Direction = ParameterDirection.Output;
    
                    SqlDataAdapter daAcc = new SqlDataAdapter(command);
                   
                    daAcc.Fill(Ds);
                   }
                catch (Exception ex)
                {
                    lblMessageCustSerach.Text = ex.Message;
                    lblMessageCustSerach.Visible = true;
                }  
                        finally //Close db Connection if it is open....  
                {
                    if (connection.State == ConnectionState.Open)
                        connection.Close();
    
                }
            }        
    
            protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack == true)
                {
                   
                    gridCustBind(1);
                    if (gridCustomer.Rows.Count > 0)
                    {
                        gridCustomer.DataSource = Ds;
                        gridCustomer.DataBind();
                        
                    }
    
                }
            }

If I do like this then throwing error: Value cannot be null.
Parameter name: dataSet
daAcc.Fill(Ds); //at this line
Posted
Updated 12-Apr-15 23:41pm
v2

1 solution

1.It is not OK to cache the data set inside your view state. The view state is designed to be used for caching small amount of data from the page, like last user inputs from the page controls.

2.A solution to your problem is in my next article (including full source code): Advanced ASPX GridView Pagination and Data Entities[^]
 
Share this answer
 
Comments
Dawood Abbas 13-Apr-15 6:31am    
Its hard to understand and apply on my requirement.
Raul Iloc 14-Apr-15 1:50am    
You could get only the parts related with filtering.

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