Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to add <asp:ListItem></asp:ListItem> dynamicaly to dropdownlist? 1

Status
Not open for further replies.

passs

Programmer
Dec 29, 2003
170
RU
Hello!
Can someone help me?

I need to send data from DataBase to DropDownList Items.
I have DropDownList withour any <asp:ListItem></asp:ListItem>
and i want to add it dynamicaly, due to i do not know how many datas can be in DataBase.

Will be very thankfull for any help!
Best regards,
Alexander
 
Open a datareader, iterate through it creating list items for each record to be added to the list box.

Code:
private void popCounterparties()
{
	string sSQL = &quot;SELECT Field1, Field2 FROM a_Table&quot;;
	SqlCommand cSQL = new SqlCommand(sSQL,cnSQLLogin);
	cSQL.CommandTimeout = 120;
	cSQL.Connection.Open();
	SqlDataReader drSQL = cSQL.ExecuteReader();
	while(drSQL.Read())
	{
		string sText = drSQL[&quot;Field2&quot;].ToString();
		string sValue = drSQL[&quot;Field1&quot;].ToString();
		ListItem li = new ListItem(sText,sValue);
		ddlCounterparty.Items.Add(li);
	}
	drSQL.Close();
	cSQL.Connection.Close();
	cSQL.Connection.Dispose();
}

My SQL Connection object (cnSQLLogin) is instantiated at a page level to make it freely available throughout the page so you'll need to set up your SQL Connection object prior to instantiating the SQLCommand in the above.

Hope this helps...

Rhys

Be careful that the light at the end of the tunnel isn't a train coming the other way.
 
Thank you, i've found a little bit another way, but at least it is the same:
while (dreadCalls.Read())
{
ddlYear.Items.Add(new ListItem(dreadCalls.GetInt32(0).ToString(),dreadCalls.GetInt32(0).ToString()));
}

But thanks a lot!
Best regards,
Alexander
 
Yep, that's doing the same thing, I've just had to break mine down in the app.'s I've worked on as although I'm developing in .Net, none of our support team (who work on completed app.'s) have any .net training and it makes it far simpler for them to understand as they're working up from VB 6.0 backgrounds.

Rhys

Be careful that the light at the end of the tunnel isn't a train coming the other way.
 
Hey Rhys!
Thanks for help! Actualy i do not know what is &quot;app.'s&quot;:)
And may be you can help me again?
Look, i have first dropdownlist, which is fullfilling with data from DB in Page_Load. After user chose something in this dropdownlist request sends to server (autopostback) and i have to show another dropdownlist with data from another table of DB and concerns to data from first dropdownlist.
I use folowing code:

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
if (!IsPostBack)
{
SqlCommand MaxID = new SqlCommand(&quot;SELECT MAX(YearID) FROM Year&quot;, sqlConnection);
//open connection
sqlConnection.Open();
int num_of_str, jCount=0;
num_of_str = (int)MaxID.ExecuteScalar();
for (int iCount = 1; iCount <= num_of_str; iCount++)
{
//create command object
SqlCommand cmdGetCalls = new SqlCommand(&quot;SELECT Year FROM Year WHERE YearID =&quot; + iCount, sqlConnection);
//create reading data object
SqlDataReader dreadCalls;
//do a command
dreadCalls = cmdGetCalls.ExecuteReader();
//show strings
while (dreadCalls.Read())
{
ddlYear.Items.Add(new ListItem(dreadCalls.GetInt32(0).ToString(),dreadCalls.GetInt32(0).ToString()));
}
jCount++;
//close reading data object
dreadCalls.Close();
}
sqlConnection.Close();
}
else
{
SqlCommand MaxIDMonth = new SqlCommand(&quot;SELECT MAX(MonthID) FROM Month&quot;, sqlConnection);
//open connection
sqlConnection.Open();
int num_of_str_m, jCount=0;
string year;
year = ddlYear.SelectedItem.Value.ToString();
Response.Write(year);
num_of_str_m = (int)MaxIDMonth.ExecuteScalar();
for (int iCount = 1; iCount <= num_of_str_m; iCount++)
{
//create command object
SqlCommand cmdGetMonth = new SqlCommand(&quot;SELECT Month FROM Month WHERE MonthID =&quot; + iCount + &quot; AND &quot; + year + &quot;=1&quot;, sqlConnection);
//create reading data object
SqlDataReader readMonth;
//do a command
readMonth = cmdGetMonth.ExecuteReader();
//show strings
while (readMonth.Read())
{
Response.Write(readMonth.GetString(0));
ddlMonth.Items.Add(new ListItem(readMonth.GetString(0),readMonth.GetString(0)));
}
jCount++;
//close reading data object
readMonth.Close();
}
sqlConnection.Close();
ddlMonth.Visible = true;
}
//sqlConnection.Close();
}

first dropdown menu works well, second is appiaring after postback, but it's list is empty. I don't know why.
Maybe you can tell me where i have a mistake?

Thank you.
Best regards,
Alexander
 
Have you put in a breakpoint to check the value of
num_of_str_m
...and...
ddlYear.SelectedItem.Value.ToString();

That's where I'd start, and if that value's here are correct, check the iteration that creates the Months in the Month DropDown List to make sure that the query is returning rows and the code is executing and trying to add items to the list.

NB: App.'s - Applications

Rhys

Be careful that the light at the end of the tunnel isn't a train coming the other way.
 
Hi:) Thanks, you'll be laughing, but mistake was in MS SQL DB, cells were named &quot;2003&quot; &quot;2004&quot; etc, when i changed to &quot;m2003&quot;... everything begin to work correct.:)
Don't you know how can i delete all the items from dropdownlist if i do not know how many of them are there?
Actualy i need to know the number of existing items in currne dropdownlist , and then will do something like:

for (i =0; i<number_of_items;i++)
{
dropdownlist.Items.RemoveAt(0);
}
i think it has to work, but i dont know how to get this number of items:)

Thank you for help!
Best Regards,
Alexander
 
Tee, Hee! Never mind, happens to all of us at some point. Anyway...

...clearing a dropdownlist...
dropdownlist.Items.Clear();

so you shouldn't need to know the number of items. I believe it generates an error if you iteratively try to delete items by index too, though I think there's a Count property for the Items collection of a dropdownlist so you should be able to get the number of items there if you still need it.

Rhys

Be careful that the light at the end of the tunnel isn't a train coming the other way.
 
YES!!! Working!
You're the best!!
THANK YOU!!!

How long do you work with ASP.NET?

Sometime i do not know such a simple thing!:)

Thank you so much!

Best regards!
Alexander
 
Worked on a 12 month asp.Net project, with all of 5 days training in .Net, by basically being told to get on with it. So I've learnt most of it as i've gone along and from sites like this, The Code Project and Asp.Net. That ended around August last year, and since then I've done some asp.Net prototyping, a couple of small .Net Windows Forms applications in c# and had to make changes to VB 6.0 and Office 97 Based VBA applications.

It is a steep learning curve to start I think, but when you're fairly comfortable with your design and presentation, populating your dynamic data etc. it does start dropping into place as you'll have more of a handle on the the assemblies in the framework and the properties and methods of asp.Net Web controls. Let's face it the controls are far richer than almost anything else that allows such Rapid Application Development as asp.Net.

No worries for the assist, glad to help!

Rhys

Be careful that the light at the end of the tunnel isn't a train coming the other way.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top