在网格视图中搜索和显示

本文关键字:显示 搜索 网格 视图 | 更新日期: 2023-09-27 18:29:09

自从有人教我使用gridview来显示搜索结果以来,我一直在研究这个。

我的问题是,我甚至无法让它工作,当我点击或点击搜索按钮时,什么都没有发生。我有:

-1个姓氏文本框-2个省和市的下拉列表-和一个搜索(触发)按钮

以下是我迄今为止所做的:

 public partial class Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
            SqlConnection conn = new SqlConnection(constring);
            DataTable dt = new DataTable("emed_province");
            using (conn)
            {
                conn.Open();
                SqlCommand comm = new SqlCommand("SELECT * FROM emed_province ORDER BY PROVINCE_NAME ASC", conn);
                SqlDataAdapter adptr = new SqlDataAdapter(comm);
                adptr.Fill(dt);
            }
            ddlProvince.DataSource = dt;
            ddlProvince.DataTextField = "PROVINCE_NAME";
            ddlProvince.DataValueField = "PROVINCE_CODE";
            ddlProvince.DataBind();
        }
    }
    protected void ddlProvince_SelectedIndexChanged(object sender, EventArgs e)
    {
        string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
        SqlConnection conn = new SqlConnection(constring);
        DataTable dt = new DataTable("emed_province");
        using (conn)
        { 
            conn.Open();
            SqlCommand comm = new SqlCommand("SELECT * FROM emed_city WHERE PROVINCE_CODE =@pcode", conn);
            comm.Parameters.AddWithValue("@pcode", ddlProvince.SelectedValue);
            SqlDataAdapter adptr = new SqlDataAdapter(comm);
            adptr.Fill(dt);
            SqlParameter param = new SqlParameter();
            param.ParameterName = "@pcode";
            param.Value = ddlProvince;
            comm.Parameters.Add(param);
        }
        ddlCity.DataSource = dt;
        ddlCity.DataTextField = "CITY_NAME";
        ddlCity.DataValueField = "CITY_CODE";
        ddlCity.DataBind();
    }
    private void BindGridView(string field)
    {

        DataTable dt = new DataTable();
        string constring = ConfigurationManager.ConnectionStrings["AccreString"].ConnectionString;
        SqlConnection conn = new SqlConnection(constring);
        try
        {
            conn.Open();
            SqlCommand comm = new SqlCommand("SELECT * FROM emed_accredited_providers WHERE DOCTOR_CODE =@pcode", conn);
            comm.Parameters.AddWithValue("@pcode", ddlProvince.SelectedValue);
            SqlDataAdapter adptr = new SqlDataAdapter(comm);
            adptr.Fill(dt);
            SqlParameter param = new SqlParameter();
            param.ParameterName = "@pcode";
            param.Value = ddlProvince;
            comm.Parameters.Add(param);
            if (dt.Rows.Count > 0)
            {
                GridView1.DataSource = dt;
                GridView1.DataBind();
            }
            else
            {
            }
            // NO RECORDS FOUND 
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Fetch Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            conn.Close();
        }
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        BindGridView(txtName.Text.Trim());
    } 
}

我是新手,请帮帮我。谢谢!

在网格视图中搜索和显示

您没有使用传递给BindGridView的字段字符串变量,并且您对SQL参数管理不善(将同一参数添加两次并将DropDown对象指定为参数值)。

您正在两次添加相同的参数

要解决此问题,请删除此行:comm.Parameters.AddWithValue("@pcode", ddlProvince.SelectedValue);

您没有使用字段变量

要解决此问题,请更改此行

 param.Value = ddlProvince; // Note: You are assigning a dropdown OBJECT as the value here! 

 param.Value = field;

在您的BindGridView函数中。