473,472 Members | 1,719 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Drop Down Lists Query

Hi,

I new to C# so please forgive me if this is simple. I've got 2 drop
down lists the first is hard coded with data where as the second is
populated from a database. The options that appear in the second should
be determined by what is selected in the first drop down list. When the
page loads it automatically populates the second list with the default
value via a proceedure:

public void popDDL(string dbName)
{

string strConn;
string strSQL;
strConn = "SERVER=localhost,1433; DATABASE="+dbName+"; user
id=HMI_User; password=HMI_User; Language=British";
strSQL = @"SELECT name FROM table1 ORDER BY name";
conn = new SqlConnection( strConn );

try
{
conn.Open();
cmdSelect = new SqlCommand(strSQL, conn);
SqlDataReader dtr = cmdSelect.ExecuteReader();
if (dtr.HasRows)
{
ddlMachine.DataSource = dtr;
ddlMachine.DataTextField = "name";
ddlMachine.DataValueField = "name";
ddlMachine.DataBind();
}
}
catch (Exception ex)
{
lblError.Text = "Error! Could not generate search information!
"+ex;
}
finally
{
conn.Close();
}
}

In the first drop down list the values are the database names. What I'm
am trying to do is something like:
onClick(popDDL("databasename"))
so the page is refreshed with the new data however I'm not sure how to
do it in C#. Any help would be appreciated.

Thanks

Simon

Jan 27 '06 #1
1 2078
This is simple in .NET but you must remember that there will be a post back
to the server. In the DropDownList control add this:

AutoPostBack="True"
OnSelectedIndexChanged="FirstDropDownSelectionChan ge"

Where FirstDropDownSelectionChange is the method name that refreshes the 2nd
drop down.

--
adi
"accyboy1981" wrote:
Hi,

I new to C# so please forgive me if this is simple. I've got 2 drop
down lists the first is hard coded with data where as the second is
populated from a database. The options that appear in the second should
be determined by what is selected in the first drop down list. When the
page loads it automatically populates the second list with the default
value via a proceedure:

public void popDDL(string dbName)
{

string strConn;
string strSQL;
strConn = "SERVER=localhost,1433; DATABASE="+dbName+"; user
id=HMI_User; password=HMI_User; Language=British";
strSQL = @"SELECT name FROM table1 ORDER BY name";
conn = new SqlConnection( strConn );

try
{
conn.Open();
cmdSelect = new SqlCommand(strSQL, conn);
SqlDataReader dtr = cmdSelect.ExecuteReader();
if (dtr.HasRows)
{
ddlMachine.DataSource = dtr;
ddlMachine.DataTextField = "name";
ddlMachine.DataValueField = "name";
ddlMachine.DataBind();
}
}
catch (Exception ex)
{
lblError.Text = "Error! Could not generate search information!
"+ex;
}
finally
{
conn.Close();
}
}

In the first drop down list the values are the database names. What I'm
am trying to do is something like:
onClick(popDDL("databasename"))
so the page is refreshed with the new data however I'm not sure how to
do it in C#. Any help would be appreciated.

Thanks

Simon

Jan 27 '06 #2

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

Similar topics

2
by: Phil Longworth | last post by:
Im very new to Access 97 and Im sure I should be able to do this but cant work out how. Im bulding a database for my stamp collection. I have two tables; one with details of all the individual...
0
by: Giulio Santorini | last post by:
Hi, I've got two drop down list controls. When I select a value from the first one I fill the second one. And when I select a value from the second one I would like to fill another control. But...
3
by: Miguel Dias Moura | last post by:
Hello, i have an ASP.NET / VB page where i have a few 4 groups of Drop Down Lists. Each group of Drop Down Lists include 3 Drop Down Lists for date such as: DAY, MONTH, and YEAR. I don't want...
13
by: Leszek Taratuta | last post by:
Hello, I have several drop-down lists on my ASP.NET page. I need to keep data sources of these lists in Session State. What would be the most effective method to serialize this kind of data...
0
by: JohnZing | last post by:
Hi, i have a list of drop down lists. The first one is populated with a Select Query The Second one is populated with a select query where ParentID is the first drop down list selected value...
2
by: Yoshitha | last post by:
hi I have 2 drop down lists in my application.1st list ontains itmes like java,jsp,swings,vb.net etc.2nd list contains percentage i.e it conatains the items like 50,60,70,80,90,100. i will...
7
by: Miguel Dias Moura | last post by:
Hello, i have an ASP.NET / VB page where i have a few 4 groups of Drop Down Lists. Each group of Drop Down Lists include 3 Drop Down Lists for date such as: DAY, MONTH, and YEAR. I don't want...
3
by: penny111 | last post by:
Hi there, For my application, i need to have 3 drop down lists 1. drop down list of folder names 2. drop down list of documents in the folder selected 3. drop down list of instances of the...
3
by: jcassan | last post by:
Hello folks. I am new to these forums and have something, which has been stumping me for little while. I am using pspell to spellcheck a scrolling textbox (textarea) containing user input. I...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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,...
1
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
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
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
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.