473,385 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Listbox multiple insert

I'm using ASP.Net and SQL Server 2000. I'm trying to insert or update
records from a multiselect listbox. How can I get the contents of the
listbox if there is more than one selected and update that into the
database. But most important how do I update or insert those records.
Any articles or ideas would be appreciated.

Thanks.

Big e
Nov 18 '05 #1
3 2532
Dim cn As New
SqlClient.SqlConnection("Server=(local);Database=E pionDataCenter;User
Id=scaliendo;Password=scaliendo;Trusted_Connection =False;")

Dim Command As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand("", cn)

cn.open

dim i as integer

for i = 1 to listbox1.items.count

if listbox1.items(i-1).selected = true then

command.commandtext = "update yourtable set yourfield = '" &
listbox1.items(i-1) & "' where yourfieldindex = currentfieldindex"

command.executenonquery

end if

next

or build an array out of the seleceted items in the listbox first if you'd
like to run only one query.

Steve


"Big E" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I'm using ASP.Net and SQL Server 2000. I'm trying to insert or update
records from a multiselect listbox. How can I get the contents of the
listbox if there is more than one selected and update that into the
database. But most important how do I update or insert those records.
Any articles or ideas would be appreciated.

Thanks.

Big e

Nov 18 '05 #2
Here is what I currently have. It inserts the first record and then gives me
the error that there are too many arguments or parameters specified.

Dim cnPortal As SqlConnection = New SqlConnection(Portal)
Dim dsCommQuestion As New DataSet
Dim dtCommQuestion As New DataTable
Dim MyCommand As SqlDataAdapter = New SqlDataAdapter("spCommQuest_Insert",
cnPortal)
MyCommand.SelectCommand.CommandType = CommandType.StoredProcedure
Dim li As ListItem

For Each li In lstCommunities.Items

If li.Selected = True Then

MyCommand.SelectCommand.Parameters.Add(New SqlParameter("@QuestionId",
SqlDbType.Int))

MyCommand.SelectCommand.Parameters("@QuestionId"). Value = txtQuestionId.Text

MyCommand.SelectCommand.Parameters.Add(New SqlParameter("@CommunityID",
SqlDbType.SmallInt))

MyCommand.SelectCommand.Parameters("@CommunityID") .Value = li.Value

MyCommand.SelectCommand.Parameters.Add(New SqlParameter("@IsDisplayed",
SqlDbType.Bit))

MyCommand.SelectCommand.Parameters("@IsDisplayed") .Value =
Convert.ToInt16(chkDisplayed.Checked)

MyCommand.Fill(dsCommQuestion, "tblCommQues")

dtCommQuestion = dsCommQuestion.Tables("tblCommQues")

End If

Next

cnPortal.Close()

cnPortal = Nothing

"Steve Caliendo" <sc*******@epion.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Dim cn As New
SqlClient.SqlConnection("Server=(local);Database=E pionDataCenter;User
Id=scaliendo;Password=scaliendo;Trusted_Connection =False;")

Dim Command As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand("", cn)

cn.open

dim i as integer

for i = 1 to listbox1.items.count

if listbox1.items(i-1).selected = true then

command.commandtext = "update yourtable set yourfield = '" &
listbox1.items(i-1) & "' where yourfieldindex = currentfieldindex"

command.executenonquery

end if

next

or build an array out of the seleceted items in the listbox first if you'd
like to run only one query.

Steve


"Big E" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I'm using ASP.Net and SQL Server 2000. I'm trying to insert or update
records from a multiselect listbox. How can I get the contents of the
listbox if there is more than one selected and update that into the
database. But most important how do I update or insert those records.
Any articles or ideas would be appreciated.

Thanks.

Big e


Nov 18 '05 #3
I figured out what the problem was.
I needed to put the loop outside everything.
After it ran the stored procedure I needed to destroy it and do the next
record.
Thanks

"Big E" <no****@nospam.com> wrote in message
news:O3**************@tk2msftngp13.phx.gbl...
Here is what I currently have. It inserts the first record and then gives me the error that there are too many arguments or parameters specified.

Dim cnPortal As SqlConnection = New SqlConnection(Portal)
Dim dsCommQuestion As New DataSet
Dim dtCommQuestion As New DataTable
Dim MyCommand As SqlDataAdapter = New SqlDataAdapter("spCommQuest_Insert",
cnPortal)
MyCommand.SelectCommand.CommandType = CommandType.StoredProcedure
Dim li As ListItem

For Each li In lstCommunities.Items

If li.Selected = True Then

MyCommand.SelectCommand.Parameters.Add(New SqlParameter("@QuestionId",
SqlDbType.Int))

MyCommand.SelectCommand.Parameters("@QuestionId"). Value = txtQuestionId.Text
MyCommand.SelectCommand.Parameters.Add(New SqlParameter("@CommunityID",
SqlDbType.SmallInt))

MyCommand.SelectCommand.Parameters("@CommunityID") .Value = li.Value

MyCommand.SelectCommand.Parameters.Add(New SqlParameter("@IsDisplayed",
SqlDbType.Bit))

MyCommand.SelectCommand.Parameters("@IsDisplayed") .Value =
Convert.ToInt16(chkDisplayed.Checked)

MyCommand.Fill(dsCommQuestion, "tblCommQues")

dtCommQuestion = dsCommQuestion.Tables("tblCommQues")

End If

Next

cnPortal.Close()

cnPortal = Nothing

"Steve Caliendo" <sc*******@epion.com> wrote in message
news:%2***************@TK2MSFTNGP10.phx.gbl...
Dim cn As New
SqlClient.SqlConnection("Server=(local);Database=E pionDataCenter;User
Id=scaliendo;Password=scaliendo;Trusted_Connection =False;")

Dim Command As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand("", cn)

cn.open

dim i as integer

for i = 1 to listbox1.items.count

if listbox1.items(i-1).selected = true then

command.commandtext = "update yourtable set yourfield = '" &
listbox1.items(i-1) & "' where yourfieldindex = currentfieldindex"

command.executenonquery

end if

next

or build an array out of the seleceted items in the listbox first if you'd like to run only one query.

Steve


"Big E" <no****@nospam.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
I'm using ASP.Net and SQL Server 2000. I'm trying to insert or update
records from a multiselect listbox. How can I get the contents of the
listbox if there is more than one selected and update that into the
database. But most important how do I update or insert those records.
Any articles or ideas would be appreciated.

Thanks.

Big e



Nov 18 '05 #4

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

Similar topics

2
by: Geir Baardsen | last post by:
Hi! From a listbox I'd like to send only selected items to a report. Items will include: OrderNr,Date,EmployeeNr from tblOrders ZipCode,City from tblZipCodes Name,Adr,ZipID from...
2
by: DC Gringo | last post by:
I have two listboxes, the first of which is an autopostback=true that allows multiple row selection. When I select multiple values (by holding down CTL) in the first one, it should query the...
2
by: Big E | last post by:
I'm using ASP.Net and SQL Server 2000. I have a Listbox that users can select multiple values. Selection Mode = Multiple. The multiple values are associated with one record. I need to insert into...
3
by: Joey | last post by:
Hi, I'm trying to add a default item to my listbox but when I do it tells me that it's not defined, could someone tell me the syntax I need to use to get the listbox control to display a default...
0
by: deepak | last post by:
Hi all, i m transefering values from one listbox to another listbox using javascript(both are html controls), the first listbox is populating with database at page_load time and the second listbox...
7
by: Dave | last post by:
Hi all, After unsuccessfully trying to make my own dual listbox control out of arraylists, I decided to look for a 3rd party control. I've looked for over a week now and can't find anything but...
3
by: Bernard Lebel | last post by:
Hello, Is there an option or a way to allow the selection of multiple entries in the Listbox widget? I could not find any, and would like to allow the end user to select multiple entries. ...
0
by: Banu | last post by:
I have 2 listboxes, one with all application available, and the other with the selected applications by user (dual listbox). I have 3 tables (incident,application and ListApplication) ...
3
by: Kevin Walzer | last post by:
I'm trying to set the active item in a Tkinter listbox to my application's currently-defined default font. Here's how I get the fonts loaded into the listbox: ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.