By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,476 Members | 889 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,476 IT Pros & Developers. It's quick & easy.

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

P: 5
Expand|Select|Wrap|Line Numbers
  1. Private Sub Button_Submit_Click(sender As System.Object, e As System.EventArgs) Handles Button_Submit.Click
  2.         Submit.Submit()
  3.  
  4.         Dim Connection As New OleDbConnection(_conString)
  5.         Dim DataReader As OleDbDataReader
  6.         Dim Command As New OleDbCommand
  7.         Dim Query As String
  8.  
  9.         Connection.Open()
  10.  
  11.         Query = "INSERT INTO LOGIN(user_name, password, user_type, firstname, lastname, email, address) values ( ?,?,?,?,?,?,?)"
  12.  
  13.  
  14.         'Command.Connection = Connection
  15.         'Command.CommandText = Query
  16.  
  17.         DataReader = Command.ExecuteReader()
  18.  
  19.         Command.Parameters.AddWithValue("User_Name", TextBox_UserName.Text)
  20.         Command.Parameters.AddWithValue("Password", TextBox_Password.Text)
  21.         Command.Parameters.AddWithValue("User_Type", TextBox_Usertype.Text)
  22.         Command.Parameters.AddWithValue("FirstName", TextBox_FirstName.Text)
  23.         Command.Parameters.AddWithValue("LastName", TextBox_LastName.Text)
  24.         Command.Parameters.AddWithValue("Email", TextBox_Email.Text)
  25.         Command.Parameters.AddWithValue("Address", TextBox_Address.Text)
  26.  
  27.         'Command.ExecuteNonQuery()
  28.  
  29.         MessageBox.Show("Added Successful")
  30.  
  31.         Try
  32.         Catch ex As Exception
  33.             MessageBox.Show(ex.Message)
  34.         Finally
  35.             Connection.Close()
  36.  
  37.  
  38.         End Try
  39.     End Sub
  40.  
  41. End Class
Apr 21 '13 #1
Share this Question
Share on Google+
4 Replies


Rabbit
Expert Mod 10K+
P: 12,382
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 @
Apr 21 '13 #2

P: 5
Which parameters names? They all look they same to me. What do you mean?
Apr 21 '13 #3

Rabbit
Expert Mod 10K+
P: 12,382
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.
Apr 21 '13 #4

100+
P: 158
Hey jsand03z, you have to use like as follows...

Replace line number 11 with the following code...

Expand|Select|Wrap|Line Numbers
  1. 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...

Expand|Select|Wrap|Line Numbers
  1. Command.Parameters.AddWithValue("@User_Name", TextBox_UserName.Text)
  2. Command.Parameters.AddWithValue("@Password", TextBox_Password.Text)
  3. Command.Parameters.AddWithValue("@User_Type", TextBox_Usertype.Text)
  4. Command.Parameters.AddWithValue("@FirstName", TextBox_FirstName.Text)
  5. Command.Parameters.AddWithValue("@LastName", TextBox_LastName.Text)
  6. Command.Parameters.AddWithValue("@Email", TextBox_Email.Text)
  7. 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).
Apr 23 '13 #5

Post your reply

Sign in to post your reply or Sign up for a free account.