gridview将显示所有记录,即使文本框在搜索时为空

时间:2014-04-24 01:06:17

标签: c# asp.net search gridview

我有一个文本框和一个搜索按钮。在文本框中输入内容并单击搜索后,gridview将显示所有记录。问题是如果我在文本框中没有输入任何内容并单击搜索,它将为我提供所有记录。如果搜索时没有在文本框中输入任何内容,我希望它给我记录未找到。

  protected void SearchBlog(object sender, EventArgs e)
    {
        String ConStr = ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(ConStr);

        try
        {
            string invalid = txtSearch.Text;

            String SQL = null;

            if (invalid == "all")
            {
                SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable]";
            }
            else
            {
                SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable] WHERE AdminNumber LIKE @searchAdminNumber OR BlogType LIKE @searchBlogType OR Name LIKE @searchName";
            }


            SqlCommand cmd = new SqlCommand(SQL, con);
            con.Open();

            cmd.Parameters.Add("@searchBlogType", SqlDbType.NVarChar, 50);
            cmd.Parameters["@searchBlogType"].Value = txtSearch.Text + "%";

            cmd.Parameters.Add("@searchName", SqlDbType.NVarChar, 50);
            cmd.Parameters["@searchName"].Value = txtSearch.Text + "%";

            cmd.Parameters.Add("@searchAdminNumber", SqlDbType.NVarChar, 50);
            cmd.Parameters["@searchAdminNumber"].Value = txtSearch.Text + "%";

            SqlDataReader reader = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(reader);
            grdResult.DataSource = dt;
            grdResult.DataBind();
            lblError.Text = "";


            if (dt.Rows.Count > 0)
            {
                lblError.Text = null;
            }
            else
            {
                lblError.Text = "Record not found";
            }

            reader.Close();
        }
        catch (Exception)
        {
            lblError.Text = "Error!";
            //lblOrderError.Text = ex.Message;
        }
        finally
        {
            con.Close();
        }
    }



    private void bindResultGridView()
    {
        String ConStr = ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(ConStr);

        try
        {
            string invalid = txtSearch.Text;

            String SQL = null;

            if (invalid == "all")
            {
                SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable]";
            }
            else
            {
                SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable] WHERE AdminNumber LIKE @searchAdminNumber OR BlogType LIKE @searchBlogType OR Name LIKE @searchName";
            }


            SqlCommand cmd = new SqlCommand(SQL, con);
            con.Open();

            cmd.Parameters.Add("@searchBlogType", SqlDbType.NVarChar, 50);
            cmd.Parameters["@searchBlogType"].Value = txtSearch.Text;

            cmd.Parameters.Add("@searchName", SqlDbType.NVarChar, 50);
            cmd.Parameters["@searchName"].Value = txtSearch.Text;

            cmd.Parameters.Add("@searchAdminNumber", SqlDbType.NVarChar, 50);
            cmd.Parameters["@searchAdminNumber"].Value = txtSearch.Text;



            SqlDataReader reader = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(reader);
            grdResult.DataSource = dt;
            grdResult.DataBind();
            lblError.Text = "";

            reader.Close();
        }
        catch (SqlException ex)
        {
            lblError.Text = "Error:" + ex.Message.ToString();
        }
        finally
        {
            con.Close();
        }
    }

protected void grdResult_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    int newPageIndex = e.NewPageIndex;
    grdResult.PageIndex = newPageIndex;
    bindResultGridView();
}

编辑此作品

protected void SearchBlog(object sender, EventArgs e)
    {
        String ConStr = ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(ConStr);

        try
        {
            string invalid = txtSearch.Text;

            String SQL = null;

            if (invalid == "")
            {
                SQL = "SELECT * FROM [EntryTable] WHERE BlogID=-1";
            }
            else if (invalid == "all")
            {
                SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable]";
            }
            else
            {
                SQL = "SELECT BlogID, AdminNumber, Name, Description FROM [EntryTable] WHERE AdminNumber LIKE @searchAdminNumber OR BlogType LIKE @searchBlogType OR Name LIKE @searchName";
            }


            SqlCommand cmd = new SqlCommand(SQL, con);
            con.Open();

            cmd.Parameters.Add("@searchBlogType", SqlDbType.NVarChar, 50);
            cmd.Parameters["@searchBlogType"].Value = txtSearch.Text + "%";

            cmd.Parameters.Add("@searchName", SqlDbType.NVarChar, 50);
            cmd.Parameters["@searchName"].Value = txtSearch.Text + "%";

            cmd.Parameters.Add("@searchAdminNumber", SqlDbType.NVarChar, 50);
            cmd.Parameters["@searchAdminNumber"].Value = txtSearch.Text + "%";

            SqlDataReader reader = cmd.ExecuteReader();
            DataTable dt = new DataTable();
            dt.Load(reader);
            grdResult.DataSource = dt;
            grdResult.DataBind();
            lblError.Text = "";


            if (dt.Rows.Count > 0)
            {
                lblError.Text = null;
            }
            else
            {
                lblError.Text = "Record not found";
            }

            reader.Close();
        }
        catch (Exception)
        {
            lblError.Text = "Error!";
            //lblOrderError.Text = ex.Message;
        }
        finally
        {
            con.Close();
        }
    }

2 个答案:

答案 0 :(得分:0)

你可能想要这样的东西:

string searchString = txtSearch.Text;

if (!string.IsNullOrWhiteSpace(searchString))
{
    // your search logic here
}
else
{
    // inform user of invalid search string / do nothing / whatever
}

如果搜索一个空字符串无效,那么最好只是不搜索如果提供了一个字符串。

答案 1 :(得分:0)

我认为,即使文本框为空,您也会不必要地访问数据库。

如果文本框不为空,则应首先检查。如果它为空或为null,则让用户知道搜索字段为空或者您可以在文本框上使用RequiredField验证器,这样当他们单击“搜索”按钮而文本中没有任何数据时,将通知用户错误消息说,盒子不能空。

<asp:TextBox runat="server" id="txtSearch" />
<asp:RequiredFieldValidator runat="server" id="reqName" controltovalidate="txtSearch" errormessage="Field cannot be empty!" />

希望这会有所帮助!!