简体   繁体   中英

Using select all in drop down list and display in grid view

I have two controls a textbox and drop down list to search the database. I have added all to the drop down list that will allow the user to see all the data in the database; if the textbox is empty or not empty to show the employee and their positions (relating to what was entered in the textbox). What show be added to query?

  protected void btnSearch_Click(object sender, EventArgs e)
  {
        string mainconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
        SqlConnection sqlConn = new SqlConnection(mainconn);
        sqlConn.Open();
        SqlCommand com = new SqlCommand();
        string sqlquery = "  SELECT CONCAT(c.FIRSTNAME, ' ', c.LASTNAME) AS 'EmployeeName' , Position FROM[TWCL_OPERATIONS].[dbo].[PP_Employee] c where CONCAT(c.FIRSTNAME, ' ', c.LASTNAME)  LIKE '%' + @EmployeeName + '%' and  position like '%' + @position + '%' ";

        com.CommandText = sqlquery;
        com.Connection = sqlConn;

        com.Parameters.AddWithValue("@EmployeeName", txtEmployeeName.Text.Trim());

        com.Parameters.AddWithValue("@position", ddlPostion.SelectedItem.Text);
        DataSet ds = new DataSet();
        SqlDataAdapter sda = new SqlDataAdapter(com);
        sda.Fill(ds);

        if (ds.Tables[0].Rows.Count == 0)
        {                 
            Response.Write("<script language='javascript'>window.alert('No Records Found!');window.location='AddEmployee.aspx';</script>");
        }
        else
        {
            grdEmployee.DataSource = ds;
            grdEmployee.DataBind();
        }
    }

在网格中

You are passing the All text in to your query so in result its is looking for a position that is equal to All

position like '%All%' 

You just need to add a condition in your btnSearch click event if the selected on ddlposition is equal to All then set the value on your parameter to a empty string.

 protected void btnSearch_Click(object sender, EventArgs e)
      {
            string positionvalue =string.Empty;
            string mainconn = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection sqlConn = new SqlConnection(mainconn);
            sqlConn.Open();
            SqlCommand com = new SqlCommand();
            string sqlquery = "  SELECT CONCAT(c.FIRSTNAME, ' ', c.LASTNAME) AS 'EmployeeName' , Position FROM[TWCL_OPERATIONS].[dbo].[PP_Employee] c where CONCAT(c.FIRSTNAME, ' ', c.LASTNAME)  LIKE '%' + @EmployeeName + '%' and  position like '%' + @position + '%' ";

            com.CommandText = sqlquery;
            com.Connection = sqlConn;

            com.Parameters.AddWithValue("@EmployeeName", txtEmployeeName.Text.Trim());

            if(ddlPostion.SelectedItem.Text == "All"){
                 positionvalue  ="";
             }
            com.Parameters.AddWithValue("@position", positionvalue  );
            DataSet ds = new DataSet();
            SqlDataAdapter sda = new SqlDataAdapter(com);
            sda.Fill(ds);

            if (ds.Tables[0].Rows.Count == 0)
            {                 
                Response.Write("<script language='javascript'>window.alert('No Records Found!');window.location='AddEmployee.aspx';</script>");
            }
            else
            {
                grdEmployee.DataSource = ds;
                grdEmployee.DataBind();
            }
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM