Add new row to binded Dropdownlist retrieved from database table

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=============================
 
L

Lloyd Sheen

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top