Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Add new row to binded Dropdownlist retrieved from database table

Reply
Thread Tools

Add new row to binded Dropdownlist retrieved from database table

 
 
dungdang
Guest
Posts: n/a
 
      06-13-2007
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=============================

 
Reply With Quote
 
 
 
 
Lloyd Sheen
Guest
Posts: n/a
 
      06-13-2007

"dungdang" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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

 
Reply With Quote
 
 
 
 
dungdang
Guest
Posts: n/a
 
      06-13-2007
Thanks Lloyd, I've tried ur first suggestion and it solved my
problem..

On Jun 13, 1:56 pm, "Lloyd Sheen" <(E-Mail Removed)> wrote:
> "dungdang" <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) oups.com...
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
DataTable - Add New Row as First Row Paul ASP .Net 1 07-11-2008 08:18 PM
Inserting a new item in a dropdownlist which was binded with an ObjectDataSource Hugo Flores ASP .Net 1 08-30-2006 08:03 PM
row bottom border in inner table not matching up with row border in outer table phl HTML 1 06-08-2006 03:43 PM
embeddable early-binded database CAFxX C++ 1 03-18-2006 05:41 PM
How to add a new row to a datagrid with values of the selected row =?Utf-8?B?U3VyZXNo?= ASP .Net 1 11-22-2004 09:04 AM



Advertisments