Lyard
Lyard

Reputation: 23

Search and display in gridview

I've been working on this one since someone teaches me to use gridview to display my search result.

My problem is, I can't even make it work, when I click or hit the search button, nothing happen. I have:

-1 textbox for last name -2 dropdownlist for the province and city -and a search(trigger)button

Here's what I've done so far:

 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());
    } 
}

I'm new to this, please assist me. Thanks!

Upvotes: 1

Views: 3120

Answers (1)

Brian Webster
Brian Webster

Reputation: 30855

You are not using the field string variable that you are passing to BindGridView and you are mismanaging your SQL parameters (adding the same parameter twice and assigning a DropDown object as a parameter value).

You are adding the same parameter twice.

To fix this, remove this line: comm.Parameters.AddWithValue("@pcode", ddlProvince.SelectedValue);

You are not using the field variable.

To fix this, change this line

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

to

 param.Value = field;

in your BindGridView function.

Upvotes: 1

Related Questions