1

I am new to .net world and I could not figure out why the below code does not work. I have a dropdownlist populated with DEPARTMENT_NAME and value as DEPARTMENT_ID. I have a gridview which populates the employees data based on department_id selected from dropdown list. I have written the following code but the gridview is not populating. Can someone please help what I am doing wrong here.

protected void ddDepartments_SelectedIndexChanged(object sender, EventArgs e)
        {
            string connStr = ConfigurationManager.ConnectionStrings["myCon"].ConnectionString;
            OracleConnection oConn = new OracleConnection(connStr);
            oConn.Open();
            string SqlText = "Select * from employees where department_id = :department_id";

            OracleCommand cmd = new OracleCommand(SqlText, oConn);
            cmd.CommandType = CommandType.Text;

            OracleParameter p_department_id = new OracleParameter();
            p_department_id.OracleDbType = OracleDbType.Varchar2;
            p_department_id.Value = ddDepartments.SelectedItem.Value;

            cmd.Parameters.Add(p_department_id); 

            OracleDataAdapter adapter = new OracleDataAdapter(cmd);
            DataTable dtEmployees = new DataTable();
            adapter.Fill(dtEmployees);

            gvEmployees.DataSource = dtEmployees;
            gvEmployees.DataBind();

            dtEmployees.Dispose();
            adapter.Dispose();
            cmd.Dispose();
            oConn.Close();
            oConn.Dispose();
        }
1
  • Confirm that the parameter value is being passed into the sql correctly and that its the correct value. Also check that ddDepartments.SelectedItem.Value shouldn't instead be ddDepartments.SelectedValue Commented Aug 24, 2020 at 19:38

1 Answer 1

0

You need to use a Session variable to persist the gridView datasource on Postback which is sent by the dropdownlist.

So right after:

gvEmployees.DataSource = dtEmployees;
gvEmployees.DataBind();

Add:

Session("gvDS") = gvEmployees.DataSource;

In the page Load() method:

if (Session["gvDS"] != null && IsPostBack)
{
    gvEmployees.DataSource = Session["gvDS"];
    gvEmployees.DataBind();
}
else
    BindGridView();  // you initial gvEmployees binding method

Please see my answer @:

Asp.net Web Forms GridView Dynamic Column Issue

Sign up to request clarification or add additional context in comments.

3 Comments

I implemented as you mentioned but still the gridview does not bind with dropdown list value.
Did you set a debug point in the ddDepartments_SelectedIndexChanged to see if it is being called then step though each line and Quick Watch and see what their values are? Also, did you make sure the ddDepartments AutoPostback property is True? Show your HTML markup.
Thank you Jamal. Setting session variable is not required. But, setting AutoPostback property to True worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.