0

I'm trying to save the selected Item from the listbox to the database but when i choose the item from the listbox I get a Runtime error that the variable(RomID) is not declared. Here's the code. What am I missing?!

    If (con.State = ConnectionState.Closed) Then
        con.Open()
    End If


    Dim Name As SqlParameter = New SqlParameter("@Name", TxtName.Text)
    Dim Pass As SqlParameter = New SqlParameter("@PassportNum", TxtPassNum.Text)
    Dim Mobile As SqlParameter = New SqlParameter("@PhoneNUm", TxtMobile.Text)
    Dim RomID As SqlParameter = New SqlParameter("@ID", Integer.Parse(ListBox1.SelectedItem))
    Dim ChckIn As SqlParameter = New SqlParameter("@CheckIndate", DateTime.Now.Date)
    Dim Email As SqlParameter = New SqlParameter("@Email", TxtEmail.Text)

    Cmd.Parameters.Add(Name)
    Cmd.Parameters.Add(Pass)
    Cmd.Parameters.Add(Mobile)
    Cmd.Parameters.Add(RomID)
    Cmd.Parameters.Add(ChckIn)
    Cmd.Parameters.Add(Email)
    Cmd.CommandText = "Update Rooms set Status ='Booked' where ID = @RomID"
    Cmd.ExecuteNonQuery()
    Cmd.CommandText = "insert into Reservation(RoomID,GuestName,PassportNum,PhoneNUm,CHeckIndate,Email) VALUES(@RomID,@Name,@Pass,@Mobile,@ChckIn,@Email)"
    Cmd.ExecuteNonQuery()
    MessageBox.Show("Reservation Was Successful")
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151

2 Answers2

0

I have not declared in the same way can you try this:

remove:

Dim RomID As SqlParameter =
  New SqlParameter("@ID", Integer.Parse(ListBox1.SelectedItem))

try something like this :

command.Parameters.Add("@ID", SqlDbType.Int)
command.Parameters("@ID").Value = Integer.Parse(ListBox1.SelectedItem)
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
Joe
  • 26
  • 4
  • in VB 2010 i can't have "command.parameters." everytime i try to type that i end up having an error! – user3126241 Dec 22 '13 at 02:23
  • Try to delcare it like this: Using connection As New SqlConnection(connectionString) Dim command As New SqlCommand(commandText, connection) This is from a MSDN example: http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters%28v=vs.110%29.aspx – Joe Dec 22 '13 at 04:09
0

You have this:

Dim RomID As SqlParameter =
  New SqlParameter("@ID", Integer.Parse(ListBox1.SelectedItem))

I think you meant this:

Dim RomID As SqlParameter =
  New SqlParameter("@RomID", Integer.Parse(ListBox1.SelectedItem))
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
John Saunders
  • 160,644
  • 26
  • 247
  • 397