Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
im able to sort gridview based on 2 dropdown list selection. But if suppose dropdown list selected text doesnt have any corresponding records then it must display error msg . how to check this condition. below code sorts gridview when dropdown list items selected and search button pressed. Please note: My dropdown lists are outside gridview.
protected void btnSearch_Click(object sender, EventArgs e)
        {
            string constr = WebConfigurationManager.ConnectionStrings["ConnectString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(constr))
            {
                con.Open();

                DataTable dt = new DataTable();
                SqlCommand cmd = new SqlCommand();
                SqlDataAdapter adp = new SqlDataAdapter();
                try
                {
                    if (ddlMainmenu.SelectedIndex != 0 && ddlStentCat.SelectedIndex != 0)
                    {
                      
                        cmd = new SqlCommand("SearchStentRecords_Sp", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@Action", "SELECT1");
                        cmd.Parameters.AddWithValue("@ddl1", ddlMainmenu.SelectedItem.Text);
                        cmd.Parameters.AddWithValue("@ddl2", ddlStentCat.SelectedItem.Text);
                        adp.SelectCommand = cmd;
                        adp.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                        //da = new SqlDataAdapter("select Addstock.*, Tbl_StentUsageStock.Patient_name,Tbl_StentUsageStock.IP_no,Tbl_StentUsageStock.Date,Tbl_StentUsageStock.Technician_name FROM Addstock INNER JOIN Tbl_StentUsageStock ON  Addstock.StentId=Tbl_StentUsageStock.StentId where Addstock.Device='" + ddlMainmenu.SelectedValue + "' AND Addstock.Category='" + ddlStentCat.SelectedValue + "' AND  Addstock.Used='Y' ", con);

                    }
                    else if (ddlMainmenu.SelectedIndex != 0 && ddlStentCat.SelectedIndex == 0)
                    {

                        //da = new SqlDataAdapter("select Addstock.*, Tbl_StentUsageStock.Patient_name,Tbl_StentUsageStock.IP_no,Tbl_StentUsageStock.Date,Tbl_StentUsageStock.Technician_name FROM Addstock INNER JOIN Tbl_StentUsageStock ON  Addstock.StentId=Tbl_StentUsageStock.StentId where Addstock.Device='" + ddlMainmenu.SelectedValue + "' AND  Addstock.Used='Y' ", con);
                        cmd = new SqlCommand("SearchStentRecords_Sp", con);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@Action", "SELECT2");
                        cmd.Parameters.AddWithValue("@ddl1", ddlMainmenu.SelectedItem.Text);
                        
                        adp.SelectCommand = cmd;
                        adp.Fill(dt);
                        GridView1.DataSource = dt;
                        GridView1.DataBind();
                    }
                  

                }
                catch (Exception ex)
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Error occured : " + ex.Message.ToString() + "');", true);
                }
                finally
                {
                    con.Close();

                }
            }

        }
Posted
Updated 27-Apr-15 19:50pm
v3
Comments
Member 11377180 26-Apr-15 4:50am    
Suppose Mainmenu dropdownlist has following items: apple,mango,grapes. in database there is no Apple itself and hence no records matching it, in such case how to check and display error msg? Now if i select apple no msg is displayed instead it shows all other records of gridview corresponding to mango and grapes.

1 solution

C#
public static void SortGridView(GridView gridViewId, string pSortExp, DataTable dtTableToSort, string pSortDirection)
       {
           VS_CommonMethods.ReUsableTryCatch(() =>
           {
               HttpContext.Current.Session["sortDirection"] = (VS_Utils.ToString(HttpContext.Current.Session["sortDirection"]) == VS_Constants.ASC) ? VS_Constants.DESC : VS_Constants.ASC;

               DataView dvSearch = new DataView(dtTableToSort);

               //Sorting the data view with given expression and direction
               dvSearch.Sort = pSortExp + " " + pSortDirection;
               //Set Grid pageIndex to 0
               gridViewId.PageIndex = 0;

               //Bind Data to gridView
               FillGridDatasource(gridViewId, dvSearch.ToTable());
           });
       }
 
Share this answer
 

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