I've got a problem and I don't know if its the insert query statement or the Database it self.
This is the insert statement.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim value As Integer
If txtPassword.Text = txtPassCon.Text Then
Try
constring = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\tmsDB.mdf;Integrated Security=True"
con = New SqlConnection(constring)
con.Open()
cmd.Connection = con
cmd.CommandText = "Insert into Login (Username,Password) values ('" & txtUsername.Text & "' , '" & txtPassword.Text & "')"
cmd.ExecuteNonQuery()
cmd.CommandText = "select max(id) from login"
value = cmd.ExecuteScalar()
cmd.CommandText = "Insert into Scouts (Name,Club,Email, LoginID) values ('" & txtName.Text & "' , '" & txtClub.Text & "' , '" & txtEmail.Text & "', '" & value & "')"
cmd.ExecuteNonQuery()
con.Close()
MessageBox.Show("Registeration Sucessful")
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End If
End Sub
When I run this seems to work because it successfully gets to the messagebox and shows me the message but when I check the db to see if its actually saved there, it doesn't show
CREATE TABLE [dbo].[Login] (
[Id] INT NOT NULL IDENTITY ,
[Username] NVARCHAR (15) NOT NULL,
[Password] NVARCHAR (20) NOT NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
This is the table in the database. set id to Identity because I thought that was how to make an auto number column
'Create ExecuteScalar function
Public Function ExecuteScalar(query As String, ByVal ParamArray param() As Object) As int
Try
_commanda = New SqlCommand(query, _connection)
_commanda.CommandTimeout = 200
_sqlda = New SqlDataAdapter(_commanda)
_commanda.Transaction = _transaction
For i = 0 To param.Count - 1
_commanda.Parameters.AddWithValue("@" & i, param(i))
Next
dim value = _commanda.ExecuteScalar()
_sqlda.Dispose()
_commanda.Dispose()
Return value
Catch ex As Exception
MessageBox.Show(ex.StackTrace)
Return nothing
End Try
End Function
'Create ExecuteNonQuery function
Public Function ExecuteNonQuery(query As String, ByVal ParamArray param() As Object) As Boolean
Try
_commanda = New SqlCommand(query, _connection)
_commanda.CommandTimeout = 200
_sqlda = New SqlDataAdapter(_commanda)
_commanda.Transaction = _transaction
For i = 0 To param.Count - 1
_commanda.Parameters.AddWithValue("@" & i, param(i))
Next
_commanda.ExecuteNonQuery()
_sqlda.Dispose()
_commanda.Dispose()
Return True
Catch ex As Exception
MessageBox.Show(ex.StackTrace)
Return False
End Try
End Function
'Call it
try
if _connection.State = ConnectionState.Closed then
_connection.open()
end if
if ExecuteNonQuery("Insert into Login values (@0,@1)",txtUsername.Text, txtPassword.Text) then
dim value = ExecuteScalar("select max(id) from login")
if ExecuteNonQuery("Insert into Scouts (Name,Club,Email, LoginID) values (@0,@1,@2,@3)", txtName.Text , txtClub.Text , txtEmail.Text, value ) then
_connection.Close()
MessageBox.Show("Registeration Sucessful")
else
MessageBox.Show("Registeration Not Sucessful")
end if
else
MessageBox.Show("Registeration Not Sucessful")
end if
Catch ex As Exception
MessageBox.Show(ex.StackTrace)
End try
在catch块中将异常更改为SQLException也将有所帮助,例如Catch ex As SQLException MessageBox.Show(ex.ToString)End Try
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.