We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,620 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Incorrect syntax near ','.

Hi,
I am experiencing irritating error (undiscriptive rather), when I want to insert a single statement into my db.
Following is the code:

protected void Button1_Click(object sender, EventArgs e)
    {
        SqlConnection dataConnection = new SqlConnection();
        dataConnection.ConnectionString =@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
        SqlCommand dataCommand = new SqlCommand();
        dataCommand.CommandText=("INSERT INTO citylist(city_id,city_name,latitude,longitude,state) VALUES ((1, 'Port Blair', '11.67 N', '92.76 E', 'Andaman and Nicobar Islands')");

        dataCommand.Connection = dataConnection;    
        dataConnection.Open();

            dataCommand.ExecuteNonQuery();
            dataConnection.Close();

   }

Following is the error:

Incorrect syntax near ','.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near ','.

Source Error:


Line 46:         dataConnection.Open();
Line 47:         
Line 48:             dataCommand.ExecuteNonQuery();
Line 49:             dataConnection.Close();
Line 50:         
Source File: c:\Users\sujit\Documents\Visual Studio 2008\WebSites\WebSite1\Default.aspx.cs    Line: 48 

The similar INSERT statement has been working in one of the other page, but this.
What could be the problem, please?

5
Contributors
10
Replies
22 Hours
Discussion Span
6 Months Ago
Last Updated
13
Views
Question
Answered
mail.sujitkulkarni
Newbie Poster
5 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
VALUES ((1, 'Port

there is extra ( after values remove one from it

urtrivedi
Posting Virtuoso
1,724 posts since Dec 2008
Reputation Points: 299
Solved Threads: 366
Skill Endorsements: 24

Thanks, it works.
Now, when I've to insert 500 such records, same error still persists.
Here's what I've written for 500 records..

 protected void Button1_Click(object sender, EventArgs e)
    {

        SqlConnection dataConnection = new SqlConnection();
        dataConnection.ConnectionString =@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True";
        SqlCommand dataCommand = new SqlCommand();

        //this is the sql command for 500 records to insert
        //For multiple records I have to put it like VALUES((...),(..)) So double brackets are necessary here. 

        dataCommand.CommandText=(@"INSERT citylist(city_id,city_name,latitude,longitude,state) VALUES ((1, 'Port Blair', '11.67 N', '92.76 E', 'Andaman and Nicobar Islands'), "+
        "(2, 'Adilabad', '19.68 N', '78.53 E', 'Andhra Pradesh'),"+
        "(3, 'Adoni', '15.63 N', '77.28 E', 'Andhra Pradesh'),"+
        "(4, 'Alwal', '17.50 N', '78.54 E', 'Andhra Pradesh'),"+
        ...
        ...
        ...
        "(500, 'Kannur', '11.86 N', '75.35 E', 'Kerala'))");

            dataCommand.Connection = dataConnection;
            dataConnection.Open();

            dataCommand.ExecuteNonQuery();
            dataConnection.Close();


    }

Still it says Incorrect Value Near ','.

mail.sujitkulkarni
Newbie Poster
5 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Are you sure that your database allows to insert multiple records at once? You are using Access? From what I've found that syntax is for single records only.

pritaeas
Posting Prodigy
Moderator
9,534 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,494
Skill Endorsements: 98

Front end is Visual Studio 2008, and back end is default sql server that comes within the Visual Studio.
And yes, somehow you are getting it right. Even though I make it a 2 line statement rather than 500, still the error pops. What should I do, if the database doesn't allow multiple inserts at once?

mail.sujitkulkarni
Newbie Poster
5 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

You can always insert them one by one. There may be ways to do what you want, but I'm not too familiar with SqlExpress' features.

pritaeas
Posting Prodigy
Moderator
9,534 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,494
Skill Endorsements: 98

Haha gosh, inserting 1000 of such records would make me too unfamiliar with SQLExpress!
Anyway, thanks for the help.

mail.sujitkulkarni
Newbie Poster
5 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

Perhaps this is an option.

pritaeas
Posting Prodigy
Moderator
9,534 posts since Jul 2006
Reputation Points: 1,194
Solved Threads: 1,494
Skill Endorsements: 98

or use UNION to insert multiple records at once:

dataCommand.CommandText=(@"INSERT citylist(city_id,city_name,latitude,longitude,state) select 1, 'Port Blair', '11.67 N', '92.76 E', 'Andaman and Nicobar Islands' UNION ALL "+
        "SELECT 2, 'Adilabad', '19.68 N', '78.53 E', 'Andhra Pradesh' UNION ALL "+
        "SELECT 3, 'Adoni', '15.63 N', '77.28 E', 'Andhra Pradesh' UNION ALL "+
        "SELECT 4, 'Alwal', '17.50 N', '78.54 E', 'Andhra Pradesh'"
adam_k
Veteran Poster
1,057 posts since Jun 2011
Reputation Points: 274
Solved Threads: 205
Skill Endorsements: 11

For MS SQL Server, you don't have to include the parens around the entire set of values for it to work.

Your syntax should look like this:

insert into myTable
(col1, col2, col3, col4)
values
(1, 'a', 'v1', 23),
(2, 'b', 'v2', 24),
(3, 'c', 'v3', 25),
(4, 'd', 'v4', 26),
(5, 'e', 'v5', 27)

You just keep tacking on a comma and the next set of values.

Compare with your technique:

insert into myTable
(col1, col2, col3, col4)
values
(                     -- < this is extraneous!
(1, 'a', 'v1', 23),
(2, 'b', 'v2', 24),
(3, 'c', 'v3', 25),
(4, 'd', 'v4', 26),
(5, 'e', 'v5', 27)
)                      -- < this is also extraneous!

The parens surrounding the list of value sets are extraneous.

Hope that helps!

BitBlt
Practically a Posting Shark
894 posts since Feb 2011
Reputation Points: 482
Solved Threads: 148
Skill Endorsements: 14

Adam K, worthy! Thanks a lot. That really works.

mail.sujitkulkarni
Newbie Poster
5 posts since Nov 2012
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 6 Months Ago by pritaeas, urtrivedi, BitBlt and 1 other

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.0957 seconds using 2.69MB