I need help to fix this error(I am able to read the records, but i am not able to add a new one) Thanks.
I am working on chapter 16
Page 534
Try out Adding Records page, but i have the following error:
System.Data.SqlClient.SqlException was unhandled
Class=15
ErrorCode=-2146232060
LineNumber=1
Message="Line 1: Incorrect syntax near 'â'."
Number=170
Procedure=""
Server="RIDGEWAY_SQL"
Source=".Net SqlClient Data Provider"
State=1
StackTrace:
This happen at this line:
Dim maxId As Object = maxIdCommand.ExecuteScalar()
Here is the full code I am using:
Code:
Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
' Declare local variables and objects...
Dim intPosition As Integer, intMaxID As Integer
Dim strID As String
Dim objCommand As SqlCommand = New SqlCommand()
' Save the current record position...
intPosition = objCurrencyManager.Position
' Create a new SqlCommand object...
Dim maxIdCommand As SqlCommand = New SqlCommand _
("SELECT MAX(title_id) AS MaxID " & _
"FROM titles WHERE title_id LIKE âDM%â", objConnection)
' Open the connection, execute the command
objConnection.Open()
Dim maxId As Object = maxIdCommand.ExecuteScalar()
' If the MaxID column is null...
If maxId Is DBNull.Value Then
' Set a default value of 1000...
intMaxID = 1000
Else
' otherwise set the strID variable to the value in MaxID...
strID = CType(maxId, String)
' Get the integer part of the string...
intMaxID = CType(strID.Remove(0, 2), Integer)
' Increment the value...
intMaxID += 1
End If
' Finally, set the new ID...
strID = "DM" & intMaxID.ToString
' Set the SqlCommand object properties...
objCommand.Connection = objConnection
objCommand.CommandText = "INSERT INTO titles " & _
"(title_id, title, type, price, pubdate) " & _
"VALUES(@title_id,@title,@type,@price,@pubdate);" & _
"INSERT INTO titleauthor (au_id, title_id) VALUES(@au_id,@title_id)"
' Add parameters for the placeholders in the SQL in the
' CommandText property...
' Parameter for the title_id column...
objCommand.Parameters.AddWithValue("@title_id", strID)
' Parameter for the title column...
objCommand.Parameters.AddWithValue("@title", txtBookTitle.Text)
' Parameter for the type column
objCommand.Parameters.AddWithValue("@type", "Demo")
' Parameter for the price column...
objCommand.Parameters.AddWithValue("@price", txtPrice.Text).DbType _
= DbType.Currency
' Parameter for the pubdate column
objCommand.Parameters.AddWithValue("@pubdate", Date.Now)
' Parameter for the au_id column...
objCommand.Parameters.AddWithValue _
("@au_id", BindingContext(objDataView).Current("au_id"))
' Execute the SqlCommand object to insert the new data...
Try
objCommand.ExecuteNonQuery()
Catch SqlExceptionErr As SqlException
MessageBox.Show(SqlExceptionErr.Message)
End Try
' Close the connection...
objConnection.Close()
' Fill the dataset and bind the fields...
FillDataSetAndView()
BindFields()
' Set the record position to the one that you saved...
objCurrencyManager.Position = intPosition
' Show the current record position...
ShowPosition()
' Display a message that the record was added...
ToolStripStatusLabel1.Text = "Record Added"
End Sub