在插入未更新的新记录并且我没有收到任何错误后,我正在尝试更新一些记录。
这是我的代码:
If txtSearch.Text <> txtUserID.Text Then
Try
Dim connectionString As String = "Data Source=.\SqlExpress;Initial Catalog=Subscription;Integrated Security=True"
Using cn As New SqlConnection(connectionString)
cn.Open()
cmd.CommandText = "INSERT INTO Customers (UserID, RegisteredDate, ExpiryDate, FirstName, LastName, Address, State, City, Phone, Mobile, Email) VALUES(@UserID, @RegisteredDate, @ExpiryDate, @FirstName, @LastName, @Address, @State, @City, @Phone, @Mobile, @Email)"
Dim param1 As New SqlParameter()
param1.ParameterName = "@UserID"
param1.Value = txtUserID.Text.Trim()
cmd.Parameters.Add(param1)
Dim param2 As New SqlParameter()
param2.ParameterName = "@RegisteredDate"
param2.Value = RegisteredDate.Value
cmd.Parameters.Add(param2)
Dim param3 As New SqlParameter()
param3.ParameterName = "@ExpiryDate"
param3.Value = ExpiryDate.Value
cmd.Parameters.Add(param3)
Dim param4 As New SqlParameter()
param4.ParameterName = "@FirstName"
param4.Value = txtFirstName.Text.Trim()
cmd.Parameters.Add(param4)
Dim param5 As New SqlParameter()
param5.ParameterName = "@LastName"
param5.Value = txtLastName.Text.Trim()
cmd.Parameters.Add(param5)
Dim param6 As New SqlParameter()
param6.ParameterName = "@Address"
param6.Value = txtAddress.Text.Trim()
cmd.Parameters.Add(param6)
Dim param7 As New SqlParameter()
param7.ParameterName = "@State"
param7.Value = cboState.SelectedItem.ToString
cmd.Parameters.Add(param7)
Dim param8 As New SqlParameter()
param8.ParameterName = "@City"
param8.Value = cboCity.SelectedItem.ToString
cmd.Parameters.Add(param8)
Dim param9 As New SqlParameter()
param9.ParameterName = "@Phone"
param9.Value = txtPhone.Text.Trim()
cmd.Parameters.Add(param9)
Dim param10 As New SqlParameter()
param10.ParameterName = "@Mobile"
param10.Value = txtMobile.Text.Trim()
cmd.Parameters.Add(param10)
Dim param11 As New SqlParameter()
param11.ParameterName = "@Email"
param11.Value = txtEmail.Text.Trim()
cmd.Parameters.Add(param11)
cmd.Connection = cn
cmd.ExecuteNonQuery()
cn.Close()
End Using
Successlbl.Show()
Successlbl.ForeColor = Color.DarkBlue
Successlbl.Text = "Record Saved Successfully!."
Catch
Successlbl.Show()
Successlbl.ForeColor = Color.Red
Successlbl.Text = "Error in creating record!"
End Try
Else
Try
Dim connectionString As String = "Data Source=.\SqlExpress;Initial Catalog=Subscription;Integrated Security=True"
Using cn As New SqlConnection(connectionString)
cn.Open()
cmd.CommandText = "UPDATE Customers SET (UserID, RegisteredDate, ExpiryDate, FirstName, LastName, Address, State, City, Phone, Mobile, Email) VALUES(@UserID, @RegisteredDate, @ExpiryDate, @FirstName, @LastName, @Address, @State, @City, @Phone, @Mobile, @Email) WHERE UserID=" & txtUserID.Text & ""
Dim param1 As New SqlParameter()
param1.ParameterName = "@UserID"
param1.Value = txtUserID.Text.Trim()
cmd.Parameters.Add(param1)
Dim param2 As New SqlParameter()
param2.ParameterName = "@RegisteredDate"
param2.Value = RegisteredDate.Value
cmd.Parameters.Add(param2)
Dim param3 As New SqlParameter()
param3.ParameterName = "@ExpiryDate"
param3.Value = ExpiryDate.Value
cmd.Parameters.Add(param3)
Dim param4 As New SqlParameter()
param4.ParameterName = "@FirstName"
param4.Value = txtFirstName.Text.Trim()
cmd.Parameters.Add(param4)
Dim param5 As New SqlParameter()
param5.ParameterName = "@LastName"
param5.Value = txtLastName.Text.Trim()
cmd.Parameters.Add(param5)
Dim param6 As New SqlParameter()
param6.ParameterName = "@Address"
param6.Value = txtAddress.Text.Trim()
cmd.Parameters.Add(param6)
Dim param7 As New SqlParameter()
param7.ParameterName = "@State"
param7.Value = cboState.SelectedItem.ToString
cmd.Parameters.Add(param7)
Dim param8 As New SqlParameter()
param8.ParameterName = "@City"
param8.Value = cboCity.SelectedItem.ToString
cmd.Parameters.Add(param8)
Dim param9 As New SqlParameter()
param9.ParameterName = "@Phone"
param9.Value = txtPhone.Text.Trim()
cmd.Parameters.Add(param9)
Dim param10 As New SqlParameter()
param10.ParameterName = "@Mobile"
param10.Value = txtMobile.Text.Trim()
cmd.Parameters.Add(param10)
Dim param11 As New SqlParameter()
param11.ParameterName = "@Email"
param11.Value = txtEmail.Text.Trim()
cmd.Parameters.Add(param11)
cmd.Connection = cn
cmd.ExecuteNonQuery()
End Using
Successlbl.Show()
Successlbl.ForeColor = Color.DarkBlue
Successlbl.Text = "Record updated Successfully!."
Catch
Successlbl.Show()
Successlbl.ForeColor = Color.Red
Successlbl.Text = "Error in updating record!"
End Try
End If
谁能告诉我我哪里错了?