p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


Go Back   p2p.wrox.com Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.0 and 1.1 Basics
I forgot my password Register Now
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 p2p Programmer to Programmer discussion community. This is a community of more than 40,000 computer programmers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining our free Wrox p2p community you can post your own programming questions and respond to other programmers’ questions. Registered users also don't have to see the ads that are displayed to guests. Registration is fast, simple and absolutely free so please, join today!
Join today and post to win prizes! Post more to increase your chances of being Wrox’s top poster of the month.

Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old December 3rd, 2004, 09:35 PM
Friend of Wrox
Points: 604, Level: 9
Points: 604, Level: 9 Points: 604, Level: 9 Points: 604, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #2 (permalink)  
Old December 4th, 2004, 05:56 AM
Imar's Avatar
Wrox Author
Points: 33,554, Level: 80
Points: 33,554, Level: 80 Points: 33,554, Level: 80 Points: 33,554, Level: 80
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 10,228
Thanks: 7
Thanked 203 Times in 201 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.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #3 (permalink)  
Old December 4th, 2004, 10:35 AM
Friend of Wrox
Points: 604, Level: 9
Points: 604, Level: 9 Points: 604, Level: 9 Points: 604, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
  #4 (permalink)  
Old January 12th, 2005, 04:30 PM
Friend of Wrox
Points: 604, Level: 9
Points: 604, Level: 9 Points: 604, Level: 9 Points: 604, Level: 9
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Reddit!
Reply With Quote
Reply


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

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off
Forum Jump

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 05:01 PM
Syntax error in INSERT INTO statement Stephan BOOK: Beginning JavaScript 16 February 22nd, 2005 01:43 PM
Syntax error in INSERT INTO statement evol77 ASP.NET 1.1 2 December 17th, 2004 06:15 AM
Syntax error in INSERT INTO statement. askaggs Classic ASP Databases 5 June 10th, 2004 01:21 AM
Syntax error in INSERT INTO statement sankar ASP.NET 1.1 9 May 20th, 2004 03:48 PM



All times are GMT -4. The time now is 08:21 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
© 2008 Wiley Publishing, Inc