Wrox Programmer Forums
Go Back   Wrox Programmer Forums > .NET > .NET 2.0 and Visual Studio. 2005 > Visual Studio 2005
|
Visual Studio 2005 For discussing Visual Studio 2005. Please post code questions about a specific language (C#, VB, ASP.NET, etc) in the correct language forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Visual Studio 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 1st, 2009, 11:28 AM
Registered User
 
Join Date: Mar 2009
Posts: 3
Thanks: 1
Thanked 0 Times in 0 Posts
Default Binding Example (Beginning Visual Basic 2005)

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
 
Old January 8th, 2011, 01:06 PM
Registered User
 
Join Date: Jan 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default Same problem

Having same problem? Any ideas? Do the authors support this book anymore?
 
Old January 8th, 2011, 06:56 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

Have you tried wrapping the value DM in straigh quotes? E.g 'DM'? I don't see a "." in the SQL statement, so I don't know why it would crash otherwise.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old January 8th, 2011, 08:44 PM
Registered User
 
Join Date: Jan 2011
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Quote:
Originally Posted by Imar View Post
Hi there,

Have you tried wrapping the value DM in straigh quotes? E.g 'DM'? I don't see a "." in the SQL statement, so I don't know why it would crash otherwise.

Cheers,

Imar
That was it Imar!

Thanks for the quick response!





Similar Threads
Thread Thread Starter Forum Replies Last Post
visual basic 2005 nidhigupta001 Visual Basic 2005 Basics 1 February 12th, 2007 08:26 AM
visual basic 2005 nidhigupta001 Visual Basic 2005 Basics 2 February 11th, 2007 04:06 PM
FTP in Visual Studio 2005 Pro (Visual Basic) shoopes VB How-To 1 June 29th, 2006 02:08 PM
Beginning Visual Basic 6 database zach007 Wrox Book Feedback 1 January 24th, 2006 11:42 PM
Beginning Visual basic 6.0 DataBase visualizebasic Wrox Book Feedback 0 June 14th, 2005 03:41 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.