Problem with oleDB Command Text

Posted on 2009-04-16
Last Modified: 2012-05-06
Whats wrong with this?

OleDbCommand cmd = mycon.CreateCommand();
            sMatchID = "test";
            sTime = "0001";
            sEvent = "TB";
            sPlayerNo = "11";
            sPlayerName = "Harry";
 cmd.CommandText = "insert into MatchStats values(" + sMatchID + ",'" + sTime + ",'" + sEvent + ",'" + sPlayerNo + ",'" + sPlayerName + "');";

I have an access database with a table called MatchStats with the above columns.

WHen the debugger comes to this line:
int temp = cmd.ExecuteNonQuery();
 I get an exception saying:
OleDBException was unhandled
Syntax error (missing operator) in query expression '0001',TB'.

Whats wrong?
Question by:Wanting2LearnMan
    LVL 39

    Assisted Solution

    I think if you output your command, you will see a quote mismatch.

    msgbox "insert into MatchStats values("   sMatchID   ",'"   sTime   ",'"   sEvent   ",'"   sPlayerNo   ",'"   sPlayerName   "');";

    Above I can see that 0001 (sTime) and TB (sEvent) appear to have a missing quote based on your output.
    LVL 65

    Accepted Solution

    Brandon has identified the problem, its to do with quotes but for any numeric valus eg player no? ensure there are no quotes
    LVL 39

    Assisted Solution

    I would not assume that player no is a number field.  the potential for player "07" would make it a varchar at least.

    Featured Post

    Free Trending Threat Insights Every Day

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Join & Write a Comment

    'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
    If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
    how to add IIS SMTP to handle application/Scanner relays into office 365.
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now