Saving all listbox rows to a database

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have a web form that allow the user to add dates to a listbox control and
they are also added to an arraylist at the same time but I do not know how to
select all the rows into one string then save to the database.

Any suggestions?
 
I'm not really sure what you are trying to get at? Can you postmore details? You have values in a listbox and you have valuesin an arraylist. Are not both the arraylist and listbox thatsame (as you state)? If so just loop through either and runthem into a insert statement. Or will they select which datesthey want from the listbox and the selected one will be the onesinserted? If that is the case try the example below:

foreach(ListItem l in ListBox1.Items)
{
if(l.Selected)
{
Response.Write(l.Value); //or run insert here
}
}

Alan Washington
http://www.aewnet.com
I have a web form that allow the user to add dates to a listboxcontrol and
they are also added to an arraylist at the same time but I donot know how to
select all the rows into one string then save to the database.

Any suggestions?
User submitted from AEWNET (http://www.aewnet.com/)
 
I guess I am asking how do I loop through the listbox to save all the items
to the same database field? Do I need to use an update statement?

There has to be an easier way.

Thanks, Justin.
 
Hi ,
You can append the listitems as a comma seperated string and insert into the
database.
If you are looking for how to make the arraylist as a string then
Dim iCount As Integer = arStr.Count
Dim iIndex As Integer
Dim str As String
For iIndex = 0 To iCount - 1
str = str & arStr.Item(iIndex).ToString & ","
Next
where arStr is your arraylist.
You can now insert/update this to your database.
HTH
srini
 
Here is the code I have so far:

foreach (int i in EventDates.Items)
{
DateList += EventDates.Items.ToString();
}

Every time I try to execute this code the get this error on the foreach
statement: "Specified cast is not valid."

I don't know why this error is coming up.
 
Back
Top