473,508 Members | 2,396 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

populate dropdown items via stored procedure

14 New Member
Hi All,

I am new to this field, and is using VS.2005, language is C#.

1) created a database "Test" in sql 2005, had a table "NameList", a stored proc "GetAllName"
2) created a new website, on .aspx page has a dropdown control "ddlName"

Wish could have help at following:

Now I want to all the names from NameList will be populated at 'ddlName" when page loaded.

There is no problem for me to complete this task using data adapter, but I want to use stored procedure.

Thank you in advance. Your time is great appreciated.

ellen89
Mar 11 '08 #1
8 1426
nateraaaa
663 Recognized Expert Contributor
Please see the following article in our Howtos section.

http://www.thescripts.com/forum/thread635615.html

Nathan
Mar 11 '08 #2
ellen89
14 New Member
nateraaaa,

Thank you very much for your time.

Now I had another problem, my code is:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string cnString = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
SqlConnection cnn = new SqlConnection(cnString);
SqlCommand cmd = new SqlCommand("GetAllName", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetAllName";
cnn.Open();

SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "NameList");
DropDownList1.DataSource = ds;
DropDownList1.DataMember = "NameList";
DropDownList1.DataBind();

cnn.Close();
}
}

But the at Dropdown list will have "System.Data.DataRowView" instead of real data.
Mar 12 '08 #3
saran23
28 New Member
nateraaaa,

Thank you very much for your time.

Now I had another problem, my code is:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string cnString = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
SqlConnection cnn = new SqlConnection(cnString);
SqlCommand cmd = new SqlCommand("GetAllName", cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetAllName";
cnn.Open();

SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds, "NameList");
DropDownList1.DataSource = ds;
DropDownList1.DataMember = "NameList";
DropDownList1.DataBind();

cnn.Close();
}
}

But the at Dropdown list will have "System.Data.DataRowView" instead of real data.
Hi, The problem is in this line,
Expand|Select|Wrap|Line Numbers
  1. DropDownList1.DataSource = ds;
ur binding a dataset to a dropdownlist, a dataset may contain more than one table also, so there comes the problem, even if u hv used Datamember,the table name to which the "NameList" column belongs is not specified,So u hv to do this,

Expand|Select|Wrap|Line Numbers
  1. DropDownList1.DataSource = ds.Tables[0]
The '[0]' denotes the index of the datatable in the dataset.

Im sure this works...

Thanks
Saran
Mar 12 '08 #4
ellen89
14 New Member
Sarah,

Thank you very much. I had tried, unfortunately, I have no lucky.

I am not sure the .DataMember property, NameList is my table name, with 2 columns (firstName, lastName).

Gusee I should not have the column name in my code because I am using stored procedure to get the result. This stored procedure is very simple and tested in sql server 2005.

Thanks again,

ellen89
Mar 12 '08 #5
kunal pawar
297 Contributor
Hi, ur code correct but to display text in drop down control u have to set column name, same for value

DropDownList1.DataSource = ds;
DropDownList1.DataValueField = "Field name for selected item value"
DropDownList1.DataTextField = "Field name for selected item Text"
DropDownList1.DataBind();
Mar 12 '08 #6
ellen89
14 New Member
Thank you!

Now the problem is I want to show the full name on the dropdown list, but the Name is stored in databas as FirstName & LastName (2 columns).

What should I put for DropDownList1.DataTextField ?

I had tried DropDownList1.DataTextField = "FirstName+' '+LastName", it gave run time error.

ellen89
Mar 16 '08 #7
ellen89
14 New Member
I had solved this problem by using a VIEW table at SQL server to have FirstName and LastName at same column.

Thank you for all help.

ellen89
Mar 16 '08 #8
kunal pawar
297 Contributor
You not need to create view for this single reason, just add column in ur sql query
as FirstName + ' '+LastName as Name.

and

DropDownList1.DataTextField = "Name"
Mar 17 '08 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

20
12352
by: Dannyboyo | last post by:
I have what I hope is a simple request. I can't really code in javascript, but I am pretty good at cusomizing it with slight modifications. I code in ASP and HTML. I am trying to capture customer...
1
1965
by: Mark Goldin | last post by:
How can I populate a dropdownlist with data from a database using Microsoft.ApplicationBlocks.Data ? Thanks
3
2682
by: Yul | last post by:
Hi, We are in the process of designing an ASP.NET app, where a user will enter some 'Customer ID' to be queried in the database. If the ID is valid, several stored procedures will be called to...
3
1155
by: JIM.H. | last post by:
Hello, I have two Tables: Table1: GroupID1, GroupName1 and Table2:GroupID2, GroupName2. I have only one dropdown list (DropDownList1) in my application and through a stored procedure I want to...
3
2401
by: Chris Kettenbach | last post by:
Hello all, Quick question. I have a listbox that is populated by items from a database. When the user select something from the list, I want to populate another listbox based on the selection in...
6
3920
by: blouie | last post by:
Is there a way that a bound ddl control can: 1. display the bound data, even if it does not match a selection in the DDL? 2. allow the user to enter something other than the look up values? I...
1
1647
by: Mike P | last post by:
I am trying to populate a dropdown from a stored procedure, but I am getting the error 'Object doesn't support this property or method: 'Next' : <!--#include file="include_connection.asp"-->...
0
1758
by: Javilen | last post by:
Hello, Maybe one of you can offer a possible solution to the problem I have below. The request to me is to build a dynamically populating dropdown list from a text box.(something similar to...
0
2734
TonFrere
by: TonFrere | last post by:
Hello, I'm building a windows form application in Visual C# using VS 2005. On my form I need to populate a combobox with Invoices# linked to the current reccord's Order# value. This means that: -...
1
1127
by: ellen89 | last post by:
Hi All, I am new to this field, and is using VS.2005, language is C#. 1) created a database "Test" in sql 2005, had a table "NameList", a stored proc "GetAllName" 2) created a new website,...
0
7323
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
7379
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...
1
7038
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
7493
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5625
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
4706
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3180
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
763
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
415
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.