SQL Errors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Eric

    #1

    SQL Errors

    I get a syntax error every time I try to use an INSERT statement. My statement is correct. Strings seem to be the problem. The primary key is a number and that's the only field I can insert

    Doesn't Work

    db.InsertComman d.CommandText = "INSERT INTO Person(ID, First, MI, Last, Address, Phone, Parents_Names, School, Grade, Comments, Type) VALUES (9, '" + name.Text + "', '" + mi.Text + "', '" + last.Text + "', '" + address.Text + "', '" + phone.Text + "', '" + parents.Text + "', '" + school.Text + "', '" + grade.Text + "', '" + comments.Text + "', 'S')"

    Doesn't Wor

    db.InsertComman d.CommandText = "INSERT INTO Person(Last) VALUES('Smith') "

    Works

    db.InsertComman d.CommandText = "INSERT INTO Person(ID) VALUES(3)";
  • William Ryan

    #2
    Re: SQL Errors

    If you set the commandtext and print it to the debug window, then put it
    into Query Analyzer or whatever tool you use to fire queries against your
    DB, will the first one work? Anything that won't work in QA for instance
    isn't going to work via an ADO.NET command, so that's the first place I'd
    check. In your example with Smith that doesn't work, you aren't giving the
    PK a value (unless Last is the PK) and you can't insert nulls into a PK
    field. If the PK field is an autoincrement field or identity, send in a
    negative number and see if that doesn't fix it.

    BTW, which specific error are you getting? Does it say Primary Key
    violation for isntance, or concurrency exeption or what? That'll definitely
    help you track it down.

    Also, I'd REALLY recommend not concatenating your command text and use
    Parameters instead.
    [color=blue]
    > db.InsertComman d.CommandText = "INSERT INTO Person(ID, First, MI, Last,[/color]
    Address, Phone, Parents_Names, School, Grade, Comments, Type) VALUES (@ID,
    @FirstName, @MI, @Last, @Address, @Phone,@Parents ,@School, @Grade,
    @Commands, @WhateverFieldS Represents)

    cmd.Parameters. Clear();

    cmd.Parameters. Add("@ID", SqlDBType.Int). Value = 9;
    //do the same for the rest of the param values.

    This will give you a much cleaner implementation, get you around the issue
    of people having an apostrophe in their name and minize the risks of
    Injection attacks. Furthermore, I've been told that there is a performance
    difference. While a query like this will probably execute pretty quickly
    (especially if you are only firing it once) either way, over time user bases
    grow....things change, and saving resources becomes necessary not just a
    luxury.

    You may also want to consider making this thing a stored proc (That'd
    definitely be worth doing if at all possible), and just passing in the
    params. This way, you can test your proc in QA, verify that it works, and
    be done with it.

    HTH,

    Bill

    "Eric" <anonymous@disc ussions.microso ft.com> wrote in message
    news:AAD50335-B77B-4F43-A1BB-23BDDBA293D6@mi crosoft.com...[color=blue]
    > I get a syntax error every time I try to use an INSERT statement. My[/color]
    statement is correct. Strings seem to be the problem. The primary key is a
    number and that's the only field I can insert.[color=blue]
    >
    > Doesn't Work:
    >
    > db.InsertComman d.CommandText = "INSERT INTO Person(ID, First, MI, Last,[/color]
    Address, Phone, Parents_Names, School, Grade, Comments, Type) VALUES (9, '"
    + name.Text + "', '" + mi.Text + "', '" + last.Text + "', '" + address.Text
    + "', '" + phone.Text + "', '" + parents.Text + "', '" + school.Text + "',
    '" + grade.Text + "', '" + comments.Text + "', 'S')";[color=blue]
    >
    > Doesn't Work
    >
    > db.InsertComman d.CommandText = "INSERT INTO Person(Last) VALUES('Smith') ";
    >
    > Works:
    >
    > db.InsertComman d.CommandText = "INSERT INTO Person(ID) VALUES(3)";[/color]


    Comment

    • Eric

      #3
      Re: SQL Errors

      The Error I'm getting when I print the exception is Syntax Error in INSERT INTO statement. "Last" is a last name field in my database. I've compared what I did with other projects and everything is exactly the same. I don't get it at all

      btw, the DB is Access if that helps any.

      Comment

      • William Ryan

        #4
        Re: SQL Errors

        Eric:

        I made a table with those fields (just guessed at the types) in Access and
        didn't have a problem, so I'm wondering if a DataType conflict is happening
        or one of a few other things.

        Does it fail on every record or is it a specific one? If an Apostrohpe was
        used in this case, like O'Flynn, that would cause a syntax error.

        If the command text taken from the debug window will run in Access, and you
        are getting a syntax error, that is usually the use of a reserved word as a
        column name that can cause the problem, but First and Last aren't reserved
        so that's not it. I wasn't sure from your last post, but if you use the
        commandtext from the debug and put it in Access, will it work? For
        instance, use the same text and right before you fire executenonquery ...use
        Debug.WritleLin e(myCommand.Com mandText);

        Then if it fails, copy and paste the command text into access and see if it
        runs.

        If not, can you post the exact line from the Debug.WriteLine ? I can try
        running it here and see if it works b/c so far it does...just wondering if
        the data may be a problem.

        Bill




        "Eric" <anonymous@disc ussions.microso ft.com> wrote in message
        news:F36B99D7-205F-4536-8115-8BC89F7FD47A@mi crosoft.com...[color=blue]
        > The Error I'm getting when I print the exception is Syntax Error in INSERT[/color]
        INTO statement. "Last" is a last name field in my database. I've compared
        what I did with other projects and everything is exactly the same. I don't
        get it at all.[color=blue]
        >
        > btw, the DB is Access if that helps any.[/color]


        Comment

        • Eric

          #5
          Re: SQL Errors

          My project is at www.bsu.edu/web/ejcrump/kokomo_dec.zip StudentForm is the problem form

          I used a MessageBox to show the string before it executed and InsertCommand is as I would expect

          Thanks for all your help.

          Comment

          Working...