Wrox Programmer Forums
|
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics 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 October 8th, 2003, 12:42 PM
Registered User
 
Join Date: Oct 2003
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default Adding data to a Database

I am having a small problem I am trying to add data to my database on my local SQL Server. I inserted this Try...Catch... Statement in my code to find the problem.


Code:
Code'Execute the SqlCommand object to insert the new data... 
Try 
            objCommand.ExecuteNonQuery() 
        Catch ex As Exception 
            MessageBox.Show(Err.Description) 
        End Try



And it gives me an error message saying Incorrect Syntax near 'nvarchar' must declare the variable @Annual


Code:
'set the SqlCommand object properties... 
        objCommand.Connection = SqlConnection1 
        objCommand.CommandText = "INSERT INTO Demo " & _ 
        "(Address, [Annual Income], [Case Number], [Estimated Cost], [First Name], ID, [Last Name], Status)" & _ 
            "VALUES (@Address, @Annual Income, @Case Number, @Estimated Cost, @First Name, @ID, @Last Name, @Status);":


To my understanding if I put a @before my column name it acts like a placeholder for data that will be inserted by the SqlParameter objects in the parameters collection?

So if anyone can see the problem please help me out.

 
Old October 8th, 2003, 04:22 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

"@abc" represents a variable to SQL. You are problably having a problem because you have split var names.

You should try to stick with non-spaced names for vars:
...VALUES (@Address, @AnnualIncome, @CaseNumber, @EstimatedCost, @FirstName, @ID, @LastName, @Status)...

then you refer to them in your command object parameters collection

objCommand.Parameters("@Address").Value = "....."

Peter
 
Old October 8th, 2003, 08:18 PM
Authorized User
 
Join Date: Jun 2003
Posts: 30
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to Dhodie
Default

As I am looking thru your codes i noticed a space in the variable "@Annual Income" of the CommandText property. Have it change to "@AnnualIncome"





Similar Threads
Thread Thread Starter Forum Replies Last Post
Adding data in a combo box from the database Gini Visual Studio 2008 0 June 20th, 2008 02:17 AM
adding new user to the database ali786 Classic ASP Databases 1 March 21st, 2005 01:34 AM
Adding record to database? werD420 Classic ASP Databases 5 January 20th, 2005 06:27 PM
adding to a database gmoney060 Classic ASP Databases 5 April 9th, 2004 02:21 AM





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