insert statement into sql not working correctly. something is wrong with the dataread

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jsand03z
    New Member
    • Apr 2013
    • 5

    insert statement into sql not working correctly. something is wrong with the dataread

    Code:
    Private Sub Button_Submit_Click(sender As System.Object, e As System.EventArgs) Handles Button_Submit.Click
            Submit.Submit()
           
            Dim Connection As New OleDbConnection(_conString)
            Dim DataReader As OleDbDataReader
            Dim Command As New OleDbCommand
            Dim Query As String
    
            Connection.Open()
    
            Query = "INSERT INTO LOGIN(user_name, password, user_type, firstname, lastname, email, address) values ( ?,?,?,?,?,?,?)"
    
    
            'Command.Connection = Connection
            'Command.CommandText = Query
    
            DataReader = Command.ExecuteReader()
    
            Command.Parameters.AddWithValue("User_Name", TextBox_UserName.Text)
            Command.Parameters.AddWithValue("Password", TextBox_Password.Text)
            Command.Parameters.AddWithValue("User_Type", TextBox_Usertype.Text)
            Command.Parameters.AddWithValue("FirstName", TextBox_FirstName.Text)
            Command.Parameters.AddWithValue("LastName", TextBox_LastName.Text)
            Command.Parameters.AddWithValue("Email", TextBox_Email.Text)
            Command.Parameters.AddWithValue("Address", TextBox_Address.Text)
    
            'Command.ExecuteNonQuery()
    
            MessageBox.Show("Added Successful")
    
            Try
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            Finally
                Connection.Close()
    
    
            End Try
        End Sub
    
    End Class
    Last edited by Rabbit; Apr 21 '13, 06:20 PM. Reason: Please use code tags when posting code.
  • Rabbit
    Recognized Expert MVP
    • Jan 2007
    • 12517

    #2
    You gave your parameters a name but you didn't use those names in the SQL query. You should also start your parameter names with @

    Comment

    • jsand03z
      New Member
      • Apr 2013
      • 5

      #3
      Which parameters names? They all look they same to me. What do you mean?

      Comment

      • Rabbit
        Recognized Expert MVP
        • Jan 2007
        • 12517

        #4
        Line 11, you use ? to designate your variables. But then you add parameters with names on lines 19-25. The query has no idea which of those names matches up with the question marks. Just because you gave the parameter the same name as a field in the table doesn't mean the engine knows to translate that into the same location in the SQL. You need to use the parameter names in the SQL itself and you need to start your parameter names with the @ symbol.

        Comment

        • vijay6
          New Member
          • Mar 2010
          • 158

          #5
          Hey jsand03z, you have to use like as follows...

          Replace line number 11 with the following code...

          Code:
          Query = "INSERT INTO LOGIN(user_name, password, user_type, firstname, lastname, email, address) values (@User_Name, @Password, @User_Type, @FirstName, @LastName, @Email, @Address)"

          Then replace line number 19 to 25 with the following codes...

          Code:
          Command.Parameters.AddWithValue("@User_Name", TextBox_UserName.Text)
          Command.Parameters.AddWithValue("@Password", TextBox_Password.Text)
          Command.Parameters.AddWithValue("@User_Type", TextBox_Usertype.Text)
          Command.Parameters.AddWithValue("@FirstName", TextBox_FirstName.Text)
          Command.Parameters.AddWithValue("@LastName", TextBox_LastName.Text)
          Command.Parameters.AddWithValue("@Email", TextBox_Email.Text)
          Command.Parameters.AddWithValue("@Address", TextBox_Address.Text)

          Check your code again you misplaced "Try" (It should be placed before line number 9 in your code).

          Comment

          Working...