PHP User Warning: fetch_template() calls should be replaced by the vB_Template class. Template name: bbcode_highlight in ..../includes/functions.php on line 4197
4.0 Incorrect syntax near '{'.-VBForums
Results 1 to 5 of 5

Thread: Incorrect syntax near '{'.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Mar 2018
    Posts
    84

    Incorrect syntax near '{'.

    hi all,

    I want to insert data from datagridview to sql server but I got error message 'Incorrect syntax near '{'. ,when i run my program it doesn't show any red underline or error here is my code for insert.
    thanks..

    Code:
     public void insert()
            {
                try
                {
                    sqlcon.Open();
                    for (int i = 0; i < data_dgv.Rows.Count; i++)
                    {
                        sqlcmd = new SqlCommand("Insert into PCN_CHECKER_SYSTEM_CP2 (insertDate,employeeId,parts_no,issue_set,jo_No,mo_No,model,customer,spec)values(@date,@empId," + data_dgv.Rows[i].Cells["parts_no"].ToString() + "," + data_dgv.Rows[i].Cells["issue_set"].ToString() + "," + data_dgv.Rows[i].Cells["jo_No"].ToString() + "," + data_dgv.Rows[i].Cells["mo_No"].ToString() + "," + data_dgv.Rows[i].Cells["model"].ToString() + "," + data_dgv.Rows[i].Cells["customer"].ToString() + "," + data_dgv.Rows[i].Cells["spec"].ToString() + ")",sqlcon);
                        sqlcmd.Parameters.AddWithValue("@date", date_lbl.Text);
                        sqlcmd.Parameters.AddWithValue("@empId", empId_txt.Text);
                        sqlcmd.ExecuteNonQuery();
                        sqlcon.Close();
    
                    }
                   
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message,"inserting");
                    sqlcon.Close();
                }
            }

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,733

    Re: Incorrect syntax near '{'.

    That code is bad for several reasons.

    It appears that you are inserting text values into your database but your SQL code is wrong in that case. Just as literal strings must be wrapped in double quotes in C# code, so they must be wrapped in single quotes in SQL code. If you had bothered to look at the SQL code that your C# code generates, you probably would have seen that it didn't make sense.

    You really shouldn't be using a loop at all to save the data. You should be populating a DataTable with the data and then calling Update on a data adapter to save the lot in one go. Maybe you could use a loop to copy the data from the grid to the DataTable but, unless you have a specific reason not to, you should be creating the DataTable at the start and binding it to the grid. That way, any data entered into the grid goes into the DataTable automatically.

    I would suggest that you follow the Blog link in my signature below and check out my post on Parameters In ADO.NET and also follow the CodeBank link and check out my thread on Retrieving & Saving Data to learn a bit more about ADO.NET.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,733

    Re: Incorrect syntax near '{'.

    OK, I just looked more closely at your code and I can see that you actually do know how to use parameters because you are using them. You just decided, for some unknown reason, that it would be a great idea to use them for some values and not others. That is a terrible idea. Use them for ALL values. If you don't then, even in cases where you're safe from SQL injection, you're still prone to formatting errors and the like, which is exactly what happened here.

    Also, if you really were going to use the same SQL code in a loop and just change the values then the correct approach is to create the command and the parameters first and then simply change the parameter values in the loop.

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,733

    Re: Incorrect syntax near '{'.

    Also, this is abominable:
    vb.net Code:
    1. sqlcmd.Parameters.AddWithValue("@date", date_lbl.Text);
    You are using a literal String as the value for a parameter that represents a date. That means that one of two bad things is occurring. You are either relying on that String to be implicitly converted to a Date or you are storing your dates in a text column. Both are bad. You should be using a Date as the parameter value and storing the data in a date column.

    You should pretty much NEVER be taking data FROM a Label. Labels are for displaying text to the user. If your Label is displaying a date then that must have come from somewhere, e.g. a Date variable. It's that variable, which is inherently the correct data type, that should be the source of truth whenever you need that value.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    32,406

    Re: Incorrect syntax near '{'.

    Als, if you're going to use parameters an a loop, create the SQL and add the parameters, with out a value outside the loop... then simply set the value inside the loop and then execute it.
    Don't keep rebuilding it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width