|
-
Feb 3rd, 2011, 06:36 AM
#1
Thread Starter
Addicted Member
VS 2010 Relational Table problem
Hi all
I have 2 tables created through MS Management Studio
Users and Passwords
Now the current code i have is
Code:
''Insert the new Password
newConnection.sendCommand("INSERT INTO passwords VALUES ('" + TextBox2.Text + "')")
'Retrieve the last Insert ID
Dim lastID As Integer = newConnection.retrieveID("SELECT password_id from passwords WHERE password_id = @@IDENTITY")
'insert the last ID into the users table, So we know which password is linked to which user by its id.
newConnection.sendCommand("INSERT INTO users(username,password_id) VALUES ('" + TextBox1.Text + "'," + lastID + ")")
But this is throwing an error message
Conversion from string "INSERT INTO users(username,passw" to type 'Double' is not valid.
When i step through the application LastID does have the last Inserted ID from passwords table, And the table field is set to Integer value, So i cant really see whats going wrong at the moment.
Many Thanks Barrab
-
Feb 3rd, 2011, 06:43 AM
#2
Thread Starter
Addicted Member
Re: VS 2010 Relational Table problem
Just had a thought could it be because
Code:
Public Function sendCommand(ByVal command As String)
This is expecting a String but lastId is an integer value ?
UPDATE : Insert Working But DataGrids not refreshing.
Code:
newConnection.sendCommand("INSERT INTO users(username,password_id) VALUES ('" + TextBox1.Text + "'," + lastID.ToString + ")")
Me.UsersDataGridView.Refresh()
Me.PasswordsDataGridView.Refresh()
The datagrids are filled using dataSetAdapters (Not handcoded as i dont know how to do that yet) lol
The insert now works but the 2 tables displaying the newly entered username and password dont refresh in the form, Do i need to reload the form?
Thanks Barrab
Last edited by Barrabutus; Feb 3rd, 2011 at 06:53 AM.
-
Feb 3rd, 2011, 07:00 AM
#3
Re: VS 2010 Relational Table problem
The problem is that you are using + to concatenate strings instead of &. When everything is a String then + has the same effect as & but, when not everything is a String, it doesn't. Look at this:
Code:
"'," + lastID + ")"
'lastID' is an Integer and you are using +, which is the addition operator. You are assuming that the Integer will be converted to a String but that is NOT what happens. It's the opposite, i.e. the String gets converted to a number. That fails of course, hence the exception. ALWAYS use the concatenation operator (&) when concatenating and use the addition operator (+) only when adding.
Also, your code creates a massive security hole. Look at this code:
Code:
newConnection.sendCommand("INSERT INTO passwords VALUES ('" + TextBox2.Text + "')")
Now, what happens if the user enters this into TextBox2?
Hello'); DELETE FROM passwords; INSERT INTO passwords VALUES ('Hello
Congratulations! You just deleted the entire contents of your entire 'passwords' table. ALWAYS use parameters to insert variables into SQL code. For more information on the how and why, follow the Blog link in my signature and check out my thread on the subject.
-
Feb 3rd, 2011, 07:15 AM
#4
Thread Starter
Addicted Member
-
Feb 3rd, 2011, 08:35 AM
#5
Re: VS 2010 Relational Table problem
 Originally Posted by Barrabutus
Still need to look into a mysql_real_escape_string() function for VB as well.
No you don't. Always use parameters.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|