Reputation: 21
I have been troubleshooting this for a week. I try to insert a value into a SQL Server database. It doesn't show any error but when I check the database, there's no data inserted. I might doing something that is wrong here but I can't find it. Thanks for helping.
Dim connect As New SqlConnection(ConfigurationManager.ConnectionStrings("SqlServer").ToString())
Using coa As New SqlCommand()
With coa
.Connection = connect
.CommandType = CommandType.Text
End With
Try
connect.Open()
Dim insertcmd As String
insertcmd = "insert into tblUserSection (@newValue, @SectionName, @newSectionID) values ," _
& "@newValue, @SectionName, @newSectionID);"
coa.Parameters.Add(New SqlParameter("@newValue", SqlDbType.BigInt))
coa.Parameters("@newValue").Value = newValue
coa.Parameters.Add(New SqlParameter("@SectionName", SqlDbType.NVarChar))
coa.Parameters("@SectionName").Value = SectionName.ToString
coa.Parameters.Add(New SqlParameter("@newSectionID", SqlDbType.BigInt))
coa.Parameters("@newSectionID").Value = newSectionID
coa.ExecuteNonQuery()
connect.Close()
MsgBox("success insert")
Catch ex As Exception
MsgBox("Fail to Save to database")
End Try
End Using
Upvotes: 0
Views: 1106
Reputation: 187
You need to set CommandText property of SqlCommand after creating the insert command string. like:
Dim connect As New SqlConnection(ConfigurationManager.ConnectionStrings("SqlServer").ToString())
Using coa As New SqlCommand()
With coa
.Connection = connect
.CommandType = CommandType.Text
End With
Try
connect.Open()
Dim insertcmd As String
insertcmd = "insert into [TableName] (newValue, SectionName, newSectionID) values " _
& "(@newValue, @SectionName, @newSectionID);"
coa.CommandText = insertcmd
coa.Parameters.Add(New SqlParameter("@newValue", SqlDbType.BigInt))
coa.Parameters("@newValue").Value = newValue
coa.Parameters.Add(New SqlParameter("@SectionName", SqlDbType.NVarChar))
coa.Parameters("@SectionName").Value = SectionName.ToString()
coa.Parameters.Add(New SqlParameter("@newSectionID", SqlDbType.BigInt))
coa.Parameters("@newSectionID").Value = newSectionID
coa.ExecuteNonQuery()
connect.Close()
MsgBox("success insert")
Catch ex As Exception
MsgBox("Fail to Save to database")
End Try
End Using
Upvotes: 0
Reputation: 7484
The insert command is incorrect. It has parameters for both the column names and the value; the parameter names should only be used for the values.
Assuming the column names match the parameter names, here's an updated version of the command.
insertcmd = "insert into tblUserSection (newValue, SectionName, newSectionID) values ," _
& "@newValue, @SectionName, @newSectionID);"
The more curious question is why isn't an error showing up. That's because the insert statement is never getting executed. The ExecuteNonQuery command is run against the connection but insertcmd is never associated with the execution in any way.
I'd recommend creating a SQLCommand and using that to execute the query. Here's a sample (and my code might have mistakes, my vb.net is pretty rusty):
Dim sqlcommand as New SqlCommand(coa)
sqlcommand.text = insertcmd
sqlcommand.type = Text
sqlcommand.Parameters.Add(New SqlParameter("@newValue", SqlDbType.BigInt))
sqlcommand.Parameters("@newValue").Value = newValue
sqlcommand.Parameters.Add(New SqlParameter("@SectionName", SqlDbType.NVarChar))
sqlcommand.Parameters("@SectionName").Value = SectionName.ToString
sqlcommand.Parameters.Add(New SqlParameter("@newSectionID", SqlDbType.BigInt))
sqlcommand.Parameters("@newSectionID").Value = newSectionID
sqlcommand.ExecuteNonQuery()
Upvotes: 1