473,414 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,414 software developers and data experts.

Add new row to binded Dropdownlist retrieved from database table

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

Jun 13 '07 #1
2 1653

"dungdang" <og****@gmail.comwrote in message
news:11**********************@x35g2000prf.googlegr 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

Jun 13 '07 #2
Thanks Lloyd, I've tried ur first suggestion and it solved my
problem..

On Jun 13, 1:56 pm, "Lloyd Sheen" <a...@b.cwrote:
"dungdang" <ogs...@gmail.comwrote in message

news:11**********************@x35g2000prf.googlegr oups.com...


Jun 13 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: charliewest | last post by:
I have binded a dropdownlist control to a data view. I am trying to add a listitem to this already binded control, however, i cannot figure this out. My code is: // code to create data view...
4
by: Jim Bancroft | last post by:
Hi everyone, I'm have a DropDownList that populates itself from a DataTable. Is there some way to "append" additional rows after the datasource has been attached? For example, I'd like to bind...
7
by: Xarky | last post by:
Hi, I am using a DropDownList and populating with items. One of the items is set as blank(""). I need to check that the items selected from this DropDownList is not that value but another one....
2
by: Ville Mattila | last post by:
Hi there, I will post my question to this group too bacause the .data group seems to be rather quiet. I've been playing with VB.NET and ADO for a week now and find the different data handling...
0
by: bh | last post by:
I'm trying to edit data in a datagrid through a dropdown list with different id/text values. The problem comes in when I click the edit button & nothing appears to be happening. Did I do...
3
by: Jon Paal | last post by:
this text keeps showing up in my gridview dropdownlist "System.Data.DataRowView" How do I prevent this problem ?
2
by: =?Utf-8?B?VmljdG9yaW91czE=?= | last post by:
When the editcommand is execured for my datagrid the dropdown list appears for the bound item status. However, the item selected by default does not match the unedited data. How do I make the...
1
by: JJ | last post by:
Hi. I am having trouble getting a dropdownlist to work properly in a detailsview: The code is something like: <asp:DetailsView ID="dvwSubscriber" runat="server" AutoGenerateRows="False" ...
0
by: asmx126453 | last post by:
Hey mensen I am having some big troubles here i tryd solving it myself with internet for 2 days but i kind fix it. Its about this i have a DotNet project that alrydi is online and working for...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.