Click to See Complete Forum and Search --> : Save elements of a List to a Database


CybIRO
03-01-2004, 06:40 PM
Hi. I have a problem.

I use a Javascript to add elements dinamically to a List in my Form. That works fine. Now I want to save the elements to a Database.

I already have the connection stablished to the database and I already save the data of other fields in the form to the database, but I don't know how to refer to each element of the list in order to save them one by one...

Can anybody help me??? :confused:

PeOfEo
03-01-2004, 07:49 PM
first thing is first, ditch the java script. Keep all working scripting server side if you want your applications to run for everyone and not exclude the 13% of the internet that does not support java script.

buntine
03-01-2004, 08:21 PM
Is the list a combo-box? If so, ASP will only send over the selected index..

So it isnt possible to get every list member unless you were to send each one in a hidden field.

PeOfEo
03-01-2004, 08:37 PM
Originally posted by buntine
Is the list a combo-box? If so, ASP will only send over the selected index..

So it isnt possible to get every list member unless you were to send each one in a hidden field. If he were doing this server side I could see populating an array then looping the sql statement through the array working. Though I prefer not to use loops....

buntine
03-02-2004, 01:03 AM
WHAT?

Loops are a godsend.. What dont you like about them??

Also, i dont understand using a loop here.. There will be no array because only the selected index would be sent over.

Regards.

CybIRO
03-02-2004, 11:56 AM
It is not a Combo box... it is a Listbox. and actually what I want is to pass each and every added item of the list.

If using the ASP is the only solution... I-m sorry. But I wish to know if there is a way to refer to each element on the list, not only the selected one.

I had thought that looping would be a good Idea. I wish to do something like this at the server side

for i=0 to request.Form("NumeroList")
agregaPista = "tpistaalta " & id & "," & request.Form("TituloList").list(i) & "," & request.Form("ContenidoList").list(i) & "," & request.Form("NumeroList").list(i) & "," & request.Form("DuracionList").list(i)
MM_editCmd.CommandText = agregaPista
MM_editCmd.Execute
next

And at the For line, add something like request.Form("NumeroList").listCount, but Actually that doesn-t work.

Is there a way of referring at the server side to each element of the list?

buntine
03-02-2004, 12:16 PM
Could i have a look at the HTML code you are using to create the list-box?


Thanks,
Andrew Buntine.

CybIRO
03-02-2004, 01:04 PM
Originally posted by buntine
Could i have a look at the HTML code you are using to create the list-box?


Thanks,
Andrew Buntine.

Look... I create the listbox statically like this
<select name="NumeroList" size="12" style="width: 150px" width="15" onclick="SeleccionaListas(0)">
</select>

Function SeleccionaListas(0) is a javascript that selects the same listindex in other listboxes also at the same form. In fact... I want to save the information on several listboxes on my form... but knowing how to save the info on one would be enough for the others.

Now. I Have some textboxes and a button that uses another javascript to add the info to the listbox. that info is dinamically added. In the javascript I read with a statement like this:

ctlList.options.length = 0;
var objOption;
for(i = 0; i < objCollection[frm].Count(); i++) {
objOption = new Option(objCollection[frm].Keys[i],
objCollection[frm].Keys[i]);
ctlList.options[ctlList.options.length] = objOption;
}

Like this I add an element dinamically to the list by passing the listbox as an argument.

Now, when I press the "Save Changes" button I-m suppose to use the server side, but here is the point when I don-¨t know how to pass each data in the listbox.

I could use some hidden fields... but the info is dinamic and there are 4 listboxes.

PeOfEo
03-02-2004, 04:04 PM
Originally posted by buntine
WHAT?

Loops are a godsend.. What dont you like about them??

Also, i dont understand using a loop here.. There will be no array because only the selected index would be sent over.

Regards. For some things loops are fine, like I use them here and there... the first one I use that comes to mind checks a long form and goes through and makes sure all of the feilds have been filled in. I do not like looping through loads of data though, because usually there is a more efficient way to do things... I mean when would I use one? I could see if I were sorting data, but then again I could just do that with an sql statement. Mainly I use if statements and case statements. I probably use try catch as #3 on my how often I use list, to give you an idea of how often I use all of the others.

CybIRO
03-03-2004, 06:13 PM
I didn't exactly solved the problem the way I wanted to, but I used a different way.

I created a Hidden Field and programmed a Javascript to create the SQL Statements onto a string... and so I passed the value to the Hidden field and then execute another routine to catch it at the server side... and execute the statements.

Thanx to everyone who helped me.

CybIRO.

XeroInfinity
03-23-2004, 02:57 AM
So how do to it for my case? I want to save each element in my listbox to my database too

buntine
03-23-2004, 03:03 AM
It is not a Combo box...

Yes it is...

lisha
06-14-2006, 08:57 AM
hi........
i happened to go through this thread.........i have got somewhat the same problem...
I have two listboxes.The values in first listbox is selected and moved to the second listbox now once i click an update button i want to store all the values of the second listbox into the database............plz..help

russell
06-14-2006, 11:18 AM
When the form is submitted, use JavaScript to iterate the 2nd listbox (it is actually called a SELECT element in HTML) and store the values in a string. Put that into a hidden element in the form.

On the server side split the string into an array and loop through the array popping the values into the databse.

CybIRO
06-14-2006, 06:01 PM
hi........
i happened to go through this thread.........i have got somewhat the same problem...
I have two listboxes.The values in first listbox is selected and moved to the second listbox now once i click an update button i want to store all the values of the second listbox into the database............plz..help

Hi.

So let me see if I understood.
You have two listboxes.

The values from the first are moved to the second (randomly) when you press a button and then... the values are stored into the database when the user presses the submit button.

For the insert (or update) task, i guess you need only the identity values from the elements of your listbox.

Ex.
ListBox1:
Value Text
1 John
2 George
3 Paul
4 Ringo
5 Stuart

Suppose you want to insert into ListBox2 the values from John and Paul. You need only the values 1 and 3.

You could encode them like "1,3" and then, you could decode them when you reload the page.

First you need a javascript that encodes the listbox elements and saves it into a hidden field (which we will
call fldCode and we will create it first as a textbox, so we can see the value in design time). This javascript
will be called at the onClick() event of the button in the middle of the lists or at the onClick() event of the
ListBox1. Every time you call this javascript, it will rebuild the whole code. If you select now also Ringo, the
code would now be "1,3,4"

When you create this control, you need just to catch it on the server side. So, you will decode it then. You can
use the Split(String, Char) function to separate each element into an array. You will use de comma character to
separate each element.

strMyArray = Split(Request("fldCode"), ",")

Finally... you make a For statement to make every insert (or update) to the database for each element of the array.

For i = 0 To UBound(strMyArray)
'Your insertion code goes here.
'I will use a function called "InsertFunction(id)" that makes an insert with the id value.
InsertFunction(strMyArray(i))
Next

Basically this is how it will work. I have tested this on my systems. If you need some code, let me know.

:p CybIRO.

lisha
06-14-2006, 11:18 PM
well....you mean to say ..first i will have to save all elements of the second listbox to a hidden field...which is a textbox....so these text boxes should be dynamically generated ..right?:-(...
i am able to move the selected items of the first list box to the second listbox on clicking an add button using javascript..............but could you please help me with the code for inserting the values of the second listbox to the database.....


thanks for the immediate response.............:-)(i am very new to asp so do expect lots of doubts from me)

lisha
06-15-2006, 06:26 AM
I managed to do whatever you had suggested to do..............ie till the for loop where you include your insertion code.............i have put the values in a hidden array and as per your advive ,i have used split function to remove the comas..now am not able to write the insertion code.........could you plz help.

thanks a lot for the superb explaination!!! :)

CybIRO
06-15-2006, 04:41 PM
I managed to do whatever you had suggested to do..............ie till the for loop where you include your insertion code.............i have put the values in a hidden array and as per your advive ,i have used split function to remove the comas..now am not able to write the insertion code.........could you plz help.

thanks a lot for the superb explaination!!! :)

Of course I can help you.

Can you give me the names of your database tables and fields? so I can tell you the querys for doing the insertion and the code for the Recordsets and Commands that will do the insert.

In fact... can you send your form?

If you can't (for your company polithics) maybe you can give me different names of the fields wich can give me an idea of the querys you need.