Add new row to binded Dropdownlist retrieved from database table

  • Thread starter Thread starter dungdang
  • Start date Start date
D

dungdang

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 said:
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
 
Back
Top