Add new row to binded Dropdownlist retrieved from database table

Discussion in 'ASP .Net' started by dungdang, Jun 13, 2007.

  1. dungdang

    dungdang Guest

    Hello,

    I want to add a new row on top of the list retrieved from a database
    table..aparantely the it can retirne the list from a table but can't
    add the new row as indicated below..what should I do? or is there any
    other way to this rather.

    dungdang,

    =========================CODE START=================
    objConnection = new SqlConnection(strConnection);
    objConnection.Open();

    string strSelect = "SELECT staff_id, staff_l_name FROM
    Faculty_Staff_Details";
    objCommand3 = new SqlCommand(strSelect,
    objConnection);
    objDataReader3 = objCommand3.ExecuteReader();
    drpStaff.DataSource = objDataReader3;

    drpStaff.DataTextField = "staff_l_name";
    drpStaff.DataValueField = "staff_id";

    ListItem liItem = new ListItem("Vacant", "Vacant");
    drpStaff.Items.Insert(0, liItem);
    drpStaff.Items.Add(liItem);

    drpStaff.DataBind();
    objDataReader3.Close();
    =====================CODE END=============================
    dungdang, Jun 13, 2007
    #1
    1. Advertising

  2. dungdang

    Lloyd Sheen Guest

    "dungdang" <> wrote in message
    news:...
    > Hello,
    >
    > I want to add a new row on top of the list retrieved from a database
    > table..aparantely the it can retirne the list from a table but can't
    > add the new row as indicated below..what should I do? or is there any
    > other way to this rather.
    >
    > dungdang,
    >
    > =========================CODE START=================
    > objConnection = new SqlConnection(strConnection);
    > objConnection.Open();
    >
    > string strSelect = "SELECT staff_id, staff_l_name FROM
    > Faculty_Staff_Details";
    > objCommand3 = new SqlCommand(strSelect,
    > objConnection);
    > objDataReader3 = objCommand3.ExecuteReader();
    > drpStaff.DataSource = objDataReader3;
    >
    > drpStaff.DataTextField = "staff_l_name";
    > drpStaff.DataValueField = "staff_id";
    >
    > ListItem liItem = new ListItem("Vacant", "Vacant");
    > drpStaff.Items.Insert(0, liItem);
    > drpStaff.Items.Add(liItem);
    >
    > drpStaff.DataBind();
    > objDataReader3.Close();
    > =====================CODE END=============================
    >


    There are two ways to fix your problem.

    First and simplest is to change the order of your processing. You are
    adding the item and then binding. The binding will clear all current data
    (your "Vacant" item). Add the new ListItem after you have databound.

    OR

    Change your SQL statement to add a UNION to add the extra row.

    Hope this helps

    Lloyd Sheen
    Lloyd Sheen, Jun 13, 2007
    #2
    1. Advertising

  3. dungdang

    dungdang Guest

    Thanks Lloyd, I've tried ur first suggestion and it solved my
    problem..

    On Jun 13, 1:56 pm, "Lloyd Sheen" <> wrote:
    > "dungdang" <> wrote in message
    >
    > news:...
    >
    >
    dungdang, Jun 13, 2007
    #3
    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. =?Utf-8?B?U3VyZXNo?=
    Replies:
    1
    Views:
    453
    Saravana
    Nov 22, 2004
  2. phl
    Replies:
    1
    Views:
    4,332
    Martin Jay
    Jun 8, 2006
  3. Hugo Flores
    Replies:
    1
    Views:
    380
    Teemu Keiski
    Aug 30, 2006
  4. CAFxX
    Replies:
    1
    Views:
    287
    CAFxX
    Mar 18, 2006
  5. Paul
    Replies:
    1
    Views:
    2,255
    Hermit Dave
    Jul 11, 2008
Loading...

Share This Page