Web form not putting data into my sql table!!

rawky76

New member
Joined
May 29, 2005
Messages
1
OK, heres my code: -

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim sFN As String
Dim sLN As String
Dim sJT As String
Dim sUN As String
Dim sPW As String
sFN = txtFirstName.Text.ToString
sLN = txtLastName.Text.ToString
sJT = txtJobTitle.Text.ToString
sUN = txtUserName.Text.ToString
sPW = txtPassword.Text.ToString
Const sConnection As String = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=HelpdeskApp;Data Source=MARK"
Dim objConn As New System.Data.OleDb.OleDbConnection(sConnection)
objConn.Open()
Dim sSQL As String
Dim objCmd As New System.Data.OleDb.OleDbCommand(sSQL, objConn)
sSQL = "INSERT INTO STAFF (First Name, Last Name, Username, Password, Job Title, StaffID) VALUES (sFN, sLN, sUN, sPW, sJT, 1)"
Try
objCmd.ExecuteNonQuery()
Catch myException As System.Exception
Console.WriteLine(myException.Message)
End Try
txtFirstName.Text = ""
txtLastName.Text = ""
txtJobTitle.Text = ""
txtUserName.Text = ""
txtPassword.Text = ""
txtConfirmPassword.Text = ""
End Sub

No errors are thrown but nothing appears in the database!!??

Thanks for any help!!!
 
If you step through the code does everything seem to work fine? If you check the return value when you call ExecuteNonQuery() what value is returned from the call?
 
rawky76 said:
OK, heres my code: -

Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
Dim sFN As String
Dim sLN As String
Dim sJT As String
Dim sUN As String
Dim sPW As String
sFN = txtFirstName.Text.ToString
sLN = txtLastName.Text.ToString
sJT = txtJobTitle.Text.ToString
sUN = txtUserName.Text.ToString
sPW = txtPassword.Text.ToString
Const sConnection As String = "Provider=SQLOLEDB.1;Persist Security Info=False;User ID=sa;Initial Catalog=HelpdeskApp;Data Source=MARK"
Dim objConn As New System.Data.OleDb.OleDbConnection(sConnection)
objConn.Open()
Dim sSQL As String
Dim objCmd As New System.Data.OleDb.OleDbCommand(sSQL, objConn)
sSQL = "INSERT INTO STAFF (First Name, Last Name, Username, Password, Job Title, StaffID) VALUES (sFN, sLN, sUN, sPW, sJT, 1)"
Try
objCmd.ExecuteNonQuery()
Catch myException As System.Exception
Console.WriteLine(myException.Message)
End Try
txtFirstName.Text = ""
txtLastName.Text = ""
txtJobTitle.Text = ""
txtUserName.Text = ""
txtPassword.Text = ""
txtConfirmPassword.Text = ""
End Sub

No errors are thrown but nothing appears in the database!!??

Thanks for any help!!!


2 things.
First, you dim sSQL as string, tie it to objCmd and THEN set the value of sSQL. Try this instead:
[VB] Dim sSQL As String = "INSERT INTO STAFF (First Name, Last Name, Username, Password, Job Title, StaffID) VALUES (sFN, sLN, sUN, sPW, sJT, 1)" [/VB]

Next, if you are going to use variables as your VALUES in the insert statement, you need to build your sSQL string to use them. You have the variable names in the string, but your not using the values of those variables.

Try this:
[VB] Dim sSQL As String = "INSERT INTO STAFF (First Name, Last Name, Username, Password, Job Title, StaffID) VALUES (" & sFN & "," & sLN & "," & sUN & "," & sPW & "," & sJT & ", 1)" [/VB]

Hope that helps,

Blokz
 
Back
Top