471,627 Members | 1,715 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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 1912
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Giulio Santorini | last post: by
3 posts views Thread by Miguel Dias Moura | last post: by
13 posts views Thread by Leszek Taratuta | last post: by
reply views Thread by JohnZing | last post: by
2 posts views Thread by Yoshitha | last post: by
7 posts views Thread by Miguel Dias Moura | last post: by
1 post views Thread by XIAOLAOHU | last post: by

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.