在gridview中搜索和显示

时间:2012-03-28 06:54:45

标签: c# asp.net mysql gridview

我一直在研究这个,因为有人教我使用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());
    } 
}

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

1 个答案:

答案 0 :(得分:1)

您没有使用要传递给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函数中。