Alivia
Alivia

Reputation: 1

problem in selecting a value from drop down

I am having a problem in selecting a value from dropdown list. Regardless of whatever I choose only the first value is selected always. Please help...

Here is the code...

protected void Button4_Click(object sender, EventArgs e)
{
    SqlConnection con;

    String strcon = ConfigurationManager.AppSettings["Constr"].ToString();
    try
    {
        if (!IsPostBack)
        {
        con = new SqlConnection(strcon);
        con.Open();
        SqlDataAdapter da = new SqlDataAdapter("select user_name from user_details where role='USER'", con);     
        DataSet ds = new DataSet();

            da.Fill(ds);
            DropDownList1.DataTextField = "user_name";
            DropDownList1.DataSource = ds.Tables[0].DefaultView;
            DropDownList1.DataBind();
        }

    }
    catch (Exception ex)
    {
        Response.Write(ex.Message.ToString());

    }




protected void Button6_Click(object sender, EventArgs e)
{

    string Name = Session["name"].ToString();
    SqlConnection con;

    String strcon = ConfigurationManager.AppSettings["Constr"].ToString();
    try
    {
        con = new SqlConnection(strcon);
        con.Open();


        SqlDataAdapter da = new SqlDataAdapter("select user_name,Arival,late,Day_count from attedance where user_name='" + DropDownList1.SelectedItem.Text + "' ", con);
        DataSet ds = new DataSet();

        da.Fill(ds);
        GridView1.DataSource = ds.Tables[0].DefaultView;
        GridView1.DataBind();
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message.ToString());

    }

Upvotes: 0

Views: 323

Answers (1)

Mike
Mike

Reputation: 6239

Don't quite get your code. You've shown two button click event handlers.

The first populates the drop-down, so the first item will be selected (that's how it works).

The second one populates a gridview.

If the issue arises from clicking 'Button4' (rename buttons to make it clear what they do), then that's your issue surely?

Also, you're not closing your SqlConnection. Use a using block:

using (SqlConnection connection = new SqlConnection(connectionString))
{
    //Do work here
}

EDIT: Ah, just noticed the (!IsPostBack).

Is ViewState enabled for the drop-down?

Upvotes: 1

Related Questions