Saving information from .NET Listbox into SQL Server 2000

Please support our ASP.NET advertiser: $4.95 a Month - ASP.NET Web Hosting – Click Here!
Reply

Join Date: Jun 2005
Posts: 50
Reputation: Naters_uk is an unknown quantity at this point 
Solved Threads: 0
Naters_uk Naters_uk is offline Offline
Junior Poster in Training

Saving information from .NET Listbox into SQL Server 2000

 
0
  #1
Jan 24th, 2006
I am doing a movie booking system, my booking page consists of buttons, representing as seats. Each time this seat is clicked, the button number is listed into a listbox.

For example, buttonA1, buttonA2 are clicked. The listbox display this two as A1 and A2. How can i save A1 & A2 into the SQL Database without selecting them with index.

Also, how can i save them as two separated records with commas in the same column in the SQL table known as SeatNo.

Is it possible? Help urgently! :!:
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Saving information from .NET Listbox into SQL Server 2000

 
0
  #2
Jan 24th, 2006
I'm not real great with VB (I work in c#) but if your code for getting from the buttons into the select box, I may be able to help you figure out what you want to do.
Reply With Quote Quick reply to this message  
Join Date: Jan 2006
Posts: 275
Reputation: f1 fan is an unknown quantity at this point 
Solved Threads: 11
f1 fan f1 fan is offline Offline
Posting Whiz in Training

Re: Saving information from .NET Listbox into SQL Server 2000

 
0
  #3
Jan 24th, 2006
I already gave you the code to get the seat numbers and put them into a table. Why didnt you just bind this table to the listbox? Then your table is ready to be saved in the database.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 50
Reputation: Naters_uk is an unknown quantity at this point 
Solved Threads: 0
Naters_uk Naters_uk is offline Offline
Junior Poster in Training

Re: Saving information from .NET Listbox into SQL Server 2000

 
0
  #4
Jan 25th, 2006
This is what you wrote, f1_fan

Re: URGENT!: Saving Database information from ASP.NET Button
Jan 20th 2006, 02:43 AM | Add to f1 fan's Reputation | Flag Bad Post | #11

im not doing your homework for you as i said... and you dont seem to want to help yourself.
To store information in the session you create a key for it and assign your information you want to store in there to it.

Session("theseats") = the_collection_you_store_your_seats_in

you have to get your collection out of the session to add your seats to it and then store it back in the session so you need

if (session("theseats") != null) then
the_collection_you_store_your_seats_in = Ctype(session("theseats"), thecollectiontype)
end if

then when you want to store in the database just store the collection



This is what i try to write with what you said
Private Sub a1_Command(ByVal sender As System.Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs) Handles a1.Command
If (a1.BackColor.Equals(System.Drawing.Color.Red)) Then
TextBox2.Text = e.CommandArgument
End If

I am not a programming expert. I can't understand persudo codes at one go. You said there's a table. I dont know what you are referring to. On top of that, since i put everything into a listbox. I just want to know how to bind the information of the seatbuttons from the listbox into the database, with a separate commas in between using Split function which i am still figuring out. For example, Seat A1 A2 etc will be save as A1,A2,A3 in the database.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 50
Reputation: Naters_uk is an unknown quantity at this point 
Solved Threads: 0
Naters_uk Naters_uk is offline Offline
Junior Poster in Training

Re: Saving information from .NET Listbox into SQL Server 2000

 
0
  #5
Jan 25th, 2006
campkev: Thanks, i appreciate it. I manage to find a C# to VB converter.

I'm not real great with VB (I work in c#) but if your code for getting from the buttons into the select box, I may be able to help you figure out what you want to do.

This is how i have written for the button to be place as an information into the listbox. Basically, this button represents seat A3 and it is added to the listbox as A3 once clicked. I would like to save this from the list box into the database. For instance, A3,A4,A5 with a comma to separate the seats. I read up about the split function but i am not sure if its the right one to use.
Sub a3_Click(ByVal sender As Object, ByVal e As EventArgs)

If (a3.BackColor.Equals(System.Drawing.Color.Red)) Then

a3.BackColor = System.Drawing.Color.Black
listbox_seatselected.Items.Remove("A3")

Else

a3.BackColor = System.Drawing.Color.Red
listbox_seatselected.Items.Add("A3")

End If
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Saving information from .NET Listbox into SQL Server 2000

 
0
  #6
Jan 25th, 2006
then you want something like
  1. string newValue = "";
  2. for(int = 0, i < listbox_seatselected.Items.Count; i++){
  3. newValue = newValue + ", " + listbox_seatselected.Items[i].ToString();
  4. }
  5. //newValue should now look like ", A1, A3, A5"
  6. if(newValue.Length > 2){
  7. newValue = newValue.Substring(2); //removes the first ", " from the list
  8. //newValue should now look like "A1, A3, A5"
  9. //then just update the database with this string
  10. }
Last edited by campkev; Jan 25th, 2006 at 11:01 am. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 50
Reputation: Naters_uk is an unknown quantity at this point 
Solved Threads: 0
Naters_uk Naters_uk is offline Offline
Junior Poster in Training

Re: Saving information from .NET Listbox into SQL Server 2000

 
0
  #7
Jan 25th, 2006
I have tried rewriting this part into vb.net codes.

'To create an array of the seat names in the listbox as i have 152 buttons for seats
Dim nameArray(151) As String 'make room for 151 elements in the array
Dim i As Integer

For i = 0 To listbox_seatselected.Items.Count - 1
'assign a name to each indea in the array and should looke like "A1,A2,A3"
nameArray(i) = nameArray(151) + ", " + listbox_seatselected.Items(i).ToString()
Next
I do not understand the if(newValue.Length > 2){
newValue = newValue.Substring(2); //removes the first ", " from the list. I would appreciate if u can explain this section to me. I appreciate all your responses for trying to help me out.

Originally Posted by campkev
then you want something like
  1. string newValue = "";
  2. for(int = 0, i < listbox_seatselected.Items.Count; i++){
  3. newValue = newValue + ", " + listbox_seatselected.Items[i].ToString();
  4. }
  5. //newValue should now look like ", A1, A3, A5"
  6. if(newValue.Length > 2){
  7. newValue = newValue.Substring(2); //removes the first ", " from the list
  8. //newValue should now look like "A1, A3, A5"
  9. //then just update the database with this string
  10. }
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 483
Reputation: campkev is an unknown quantity at this point 
Solved Threads: 19
campkev campkev is offline Offline
Posting Pro in Training

Re: Saving information from .NET Listbox into SQL Server 2000

 
0
  #8
Jan 25th, 2006
no no no.
you aren't making an array of strings you are making one string called newValue

change this
  1. 'To create an array of the seat names in the listbox as i have 152 buttons for seats
  2. Dim nameArray(151) As String 'make room for 151 elements in the array
  3. Dim i As Integer
  4.  
  5. For i = 0 To listbox_seatselected.Items.Count - 1
  6. 'assign a name to each indea in the array and should looke like "A1,A2,A3"
  7. nameArray(i) = nameArray(151) + ", " + listbox_seatselected.Items(i).ToString()
  8. Next
  9.  
to this
  1. 'To create an array of the seat names in the listbox as i have 152 buttons for seats
  2. Dim seatListString As String 'make new string
  3. Dim i As Integer
  4. seatListString = "" ' start off with an blank string
  5. For i = 0 To listbox_seatselected.Items.Count - 1
  6. seatListString= seatListString + ", " + listbox_seatselected.Items(i).ToString()
  7. Next
  8.  
as for your next question
I do not understand the if(newValue.Length > 2){
newValue = newValue.Substring(2); //removes the first ", " from the list. I would appreciate if u can explain this section to me. I appreciate all your responses for trying to help me out.
when you are done with the for loop the seatListString will look like this:
", A1, A2, A3"
but we don't want that.
so we check that seatListString is longer than 2 characters (which indicates that the user actually select at least one seat. it also prevents the next step from causing an error)
we take the substring of that string starting after the second character;

which gives us a string that looks like "A1, A2, A3"
and we are ready to update our database with this new string
  1. if seatListString.Length > 2 then
  2. seatListString.Length = seatListString.Length.Substring(2);
  3. // then update your database
  4. else
  5. //do whatever you want to do if they didn't select any seats
  6. end if

and hey, I am learning VB
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 50
Reputation: Naters_uk is an unknown quantity at this point 
Solved Threads: 0
Naters_uk Naters_uk is offline Offline
Junior Poster in Training

Re: Saving information from .NET Listbox into SQL Server 2000

 
0
  #9
Jan 26th, 2006
I am getting an error that substring is not a member of integer
If seatListString.Length > 2 Then
seatListString.Length = seatListString.Length.Substring(2)
End If
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 50
Reputation: Naters_uk is an unknown quantity at this point 
Solved Threads: 0
Naters_uk Naters_uk is offline Offline
Junior Poster in Training

Re: Saving information from .NET Listbox into SQL Server 2000

 
0
  #10
Jan 26th, 2006
This is what i have written to save it into the database, but nothing was save into the table and no error was displayed. What's possible wrong??

Protected Sub btn_confirm_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_confirm.Click
Dim Conn As New SqlConnection
Conn = New SqlConnection("workstation id=""IBM-89118400585"";packet size=4096;integrated security=SSPI;data source=""IBM-89118400585\MSSQLSERVER1"";persist security info=True;initial catalog=Republic Theatre DataBase")


DropDownMovie.SelectedValue = Session("MovieSelected")
DropDown_Time.SelectedValue = Session("MovieTimeSelected")
DropDown_Date.SelectedValue = Session("MovieDate")
'listbox_seatselected.Rows = Session("SeatSelected")
Conn.Open()
'To create an array of the seat names in the listbox as i have 152 buttons for seats
Dim seatListString As String 'make new string
Dim i As Integer
seatListString = "" ' start off with an blank string
For i = 0 To listbox_seatselected.Items.Count - 1
seatListString = seatListString + ", " + listbox_seatselected.Items(i).ToString()
Next

If seatListString.Length > 2 Then
seatListString = seatListString.Substring(2) 'removes the first ", " from the list () As NewValue.Substring(2)
End If

Dim cmd As New SqlCommand("Insert into Ticketing (MemberUserName, MovieID, MovieTitle, MovieDate, MovieTime, NRIC, SeatNum, MemberEmail, Amount) Values (@MemberUserName, @MovieID, @MovieTitle, @MovieDate, @MovieTime, @NRIC, @SeatNum, @MemberEmail, @Amount)", Conn)
With cmd.Parameters
.Add("@MemberUserName", Session("Username"))
.Add("@MovieID", txtbox_movieid.Text.Trim)
.Add("@MovieTitle", DropDownMovie.SelectedValue)
.Add("@MovieDate", DropDown_Date.SelectedValue)
.Add("@MovieTime", DropDown_Time.SelectedValue)
.Add("@NRIC", lblnric.Text.Trim)
.Add("@SeatNum", seatListString)
.Add("@MemberEmail", lblemail.Text.Trim)
.Add("@Amount", lbl2.Text)

End With
Conn.Close()
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC