Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 3rd, 2004, 09:35 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen N, , Denmark.
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default Syntax error in INSERT INTO statement

Hi all.
My code is generated via a method in a class called DBTool. In this code snippet I'll use an object of that class called myTool:
Code:
string []fieldNames = {"CustomerType", "Name", "Password", "Address", "ZipCode", "City",
 "Phone", "EMail", "PaymentRecord", "CreditAmount", "Balance"};
object [] values = {0 , "Hans", "goggle", "Baldersgade 123", "2200", "Copenhagen",
 "298120843", "007-is-his-iq@ofir.dk", 0, 5000.00m, 0.00m};
    //string []fieldNames = {"BookID", "Comment"};
    //object [] values = {3, "Fantastisk hvis det virker!"};
//IsInDB = myTool.Insert("Comments", fieldNames, values);
IsInDB = myTool.Insert("Customers", fieldNames, values);
The commented code works fine..
The Insert method creates an INSERT INTO SQL string:
Code:
public bool Insert(string table, string[] schema, params object[] fields)
{
    if (schema.Length != fields.Length)
    throw new ArgumentException("The number of fields does not match the size of the schema.");

    string query = "INSERT INTO " + table + " (";
    for (int i = 0; i < schema.Length; i++)
        {
            query += schema[i];
            if (i != schema.Length - 1)
                query += ", ";
        }
        query += ") VALUES (";
        for (int i = 0; i < fields.Length; i++)
        {
            query += Formatted(fields[i]);
            if (i != fields.Length -1)
                query += ", ";
        }
        query += ")";
            return this.ExplicitNonQuery(query);
    }
The values a define as fields and formatted via the method Formatted:
Code:
protected override string Formatted(object obj)
    {
        switch(Convert.ToString(obj.GetType()))
        {
            case "System.Int32":
                return ((int)obj).ToString();
            case "System.String":
                return "'" + (string)obj + "'";
            case "System.Byte":
                return ((byte)obj).ToString();
            case "System.Boolean":
                return (bool)obj ? "1" : "0";
            case "System.Decimal":
                return Convert.ToString(obj);
            case "System.Double":
                return Convert.ToString(obj);
            case "System.DBNull":
                return "NULL";            
            default:
                // There is an unhandled conversion
                throw new MyException("Total crab: " + obj.ToString() + "
 er af typen " + obj.GetType());
                return obj.ToString();
        }
The last line of code (default:) courses an Exeption, I know that so I throw an error. Just to see if I use some data types I don't convert. The last thing I do is executing the SQL string against the db:
Code:
public bool ExplicitNonQuery(string query)
    {
        if (query.Length > 0)
        {
            int RowsAffected;
            this.command.CommandText = query;
    /*        INSERT INTO Customers (CustomerType, Name, Password, Address, 
ZipCode, City, Phone, EMail, PaymentRecord, CreditAmount, Balance)
VALUES (0, 'Hans','fasd', 'engade 2', '2123', 'Hornbæk', '29800843',
 'jon_ronnenberg@hotmail.dk',0, 100, 0);
       */
    OleDbTransaction trans = onnection.BeginTransaction(IsolationLevel.ReadCommitted);
    this.command.Transaction = trans;
    try
        {                
        RowsAffected = this.command.ExecuteNonQuery();
        trans.Commit();
        return RowsAffected>0;
        }
        catch (Exception ex)
            {    
                try
                {
                    trans.Rollback();
                    throw new MyException(ex.Message, ex.Source);
                //    return false;
                }
                catch(OleDbException OleDbex)
                {
                throw new MyException(OleDbex.Message, OleDbex.InnerException);
                }
            }
        } 
        else 
        {
            throw new MyException("query is empty");
        }
    }
Yet again the commented code works, but only if I run it inside Access. I tried unescaping the query field (like this @), right before I ship it to ExplicitNonQuery but in vain. If anyone can pick up on this code flaw I would greatly appreciate it. This code is due on monday, so I got little time. Thanks in advance.
- mega

 - mega
Moving to C# .NET
__________________
 - mega
Aspiring JavaScript Ninja
  #2 (permalink)  
Old December 4th, 2004, 05:56 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

What error do you get? And how does the final SQL statement look like before you pass it to the database?

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
  #3 (permalink)  
Old December 4th, 2004, 10:35 AM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen N, , Denmark.
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default

Hi Imar.

I get an OleDbExeption with the message: Syntax error in INSERT INTO statement. and the source says: Microsoft JET Database Engine
My SQL string look like this: INSERT INTO Customers (CustomerType, Name, Password, Address, ZipCode, City, Phone, EMail, PaymentRecord, CreditAmount, Balance) VALUES (0, 'Hans', 'goggle', 'Baldersgade 123', '2200', 'København', '298120843', '007-is-his-iq@ofir.dk', 0, 5000, 0) (printed out into a txt file).

I can't spot any error in the SQL but then again maybe it's just me.

 - mega
Moving to C# .NET
  #4 (permalink)  
Old January 12th, 2005, 04:30 PM
Friend of Wrox
 
Join Date: Jun 2003
Location: Copenhagen N, , Denmark.
Posts: 217
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via MSN to mega
Default

JET got some reserved keywords like Name and Password. When I renamed all the fields it worked out..
Thanks anyway.

 - mega
Moving to C# .NET
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
NEED HELP INSERT INTO statement syntax error koco ASP.NET 1.0 and 1.1 Basics 6 June 2nd, 2006 04:01 PM
Syntax error in INSERT INTO statement. askaggs Classic ASP Databases 5 June 10th, 2004 12:21 AM



All times are GMT -4. The time now is 05:11 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.