finding and selecting the correct value with a databound dropdownlist

Discussion in 'ASP .Net Web Controls' started by Matt, Jul 9, 2003.

  1. Matt

    Matt Guest

    I need to select the correct item in a DDL from the value in the
    database, if the record exists. Basically, the DDL is databound and
    if this record already exists in tblFulfill, then I need the DDL to
    show that value as selected. Everything is working except binding the
    DDL. Here is the code for the DDL and for the IF statement.
    *********************************************************
    // Create the Employee List
    SqlCommand cmdEmpAssign = new SqlCommand
    ("Select Fname + ' ' + Lname AS EmployeeName, EmpID FROM tblEmployee
    ORDER BY Fname, Lname", sqlConnection1);
    SqlDataReader drEmpAssign;
    sqlConnection1.Open();
    drEmpAssign = cmdEmpAssign.ExecuteReader();
    ddlAssign.DataSource = drEmpAssign;
    ddlAssign.DataTextField = "EmployeeName";
    ddlAssign.DataValueField = "EmpID";
    ddlAssign.DataBind();
    drEmpAssign.Close();
    sqlConnection1.Close();
    // Add a blank entry at the top of the Employee List
    ddlAssign.Items.Insert(0,"");

    // Check to see if the RequestID is already in tblFulfill
    bool fill = false;
    SqlCommand cmdTest = new SqlCommand
    ("Declare @fill bit IF ('"+ rid +"'= (SELECT RequestID FROM
    tblFulfill WHERE RequestID = '"+ rid +"')) SET @fill = 1 Else SET
    @fill = 0 Select fill = @fill", sqlConnection1);
    SqlDataReader drTest;
    sqlConnection1.Open();
    drTest = cmdTest.ExecuteReader();
    while (drTest.Read())
    {
    fill = (drTest.GetBoolean(0));
    }
    drTest.Close();
    //If the RequestID is already in tblFulfill then bind the controls
    //to the values in the database
    if (fill == true)
    {
    // Filter the Data View and bind the controls
    dvFulfill.RowFilter = "RequestID = '" + urid + "' ";
    ddlAssign.SelectedIndex =
    ddlAssign.Items.IndexOf(ddlAssign.Items.FindByValue("FulfilledBy"));
    datAssigned.DataBind();
    chkFulfilled.DataBind();
    datFulfilled.DataBind();
    txtNotes.DataBind();
    chkHold.DataBind();
    txtHold.DataBind();
    }
    sqlConnection1.Close();
    *****************************************************************

    Thanks for the help and let me know if you need any more information.

    Matt
     
    Matt, Jul 9, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Luis Ferrao
    Replies:
    1
    Views:
    456
    Eliyahu Goldin
    Feb 16, 2005
  2. Carlos
    Replies:
    0
    Views:
    309
    Carlos
    Jan 12, 2007
  3. rodchar
    Replies:
    0
    Views:
    409
    rodchar
    Nov 26, 2007
  4. Manu
    Replies:
    0
    Views:
    187
  5. Lagrange
    Replies:
    1
    Views:
    582
    Lagrange
    Jan 17, 2009
Loading...

Share This Page