Solved

OleDB Insert not working

Posted on 2004-10-18
5
206 Views
Last Modified: 2012-05-05
When I execute this block of could I get the following error:

Syntax error in INSERT INTO statement. Can not add record.

Can anybody see the problem?

My temp table contains many fields, but I want to insert into these specific ones for this record

Here is what I have:

string myInsertCmdStr = "INSERT INTO temp " +
                  "(herd_unit, study_no, year_est, date, belt, species_code, distance, recType) " +
                  "VALUES (@herd_unit, @study_no, @year_est, @date, @belt, @species_code, @distance, @recType)";
                                          
myConnection = new OleDbConnection(myConnectionString);
myConnection.Open();

foreach( RecordType7 curRow in allRec7)
{
     OleDbCommand myInsertCmd = new OleDbCommand(myInsertCmdStr,myConnection);

    myInsertCmd.Parameters.Add(new OleDbParameter("@herd_unit",OleDbType.VarChar, 3));
    myInsertCmd.Parameters["@herd_unit"].Value = curRow.herd_unit;
    myInsertCmd.Parameters.Add(new OleDbParameter("@study_no",OleDbType.VarChar, 2));
    myInsertCmd.Parameters["@study_no"].Value = curRow.study_no;
    myInsertCmd.Parameters.Add(new OleDbParameter("@year_est",OleDbType.VarChar, 2));
    myInsertCmd.Parameters["@year_est"].Value = curRow.year_established;
    myInsertCmd.Parameters.Add(new OleDbParameter("@date",OleDbType.VarChar, 10));
    myInsertCmd.Parameters["@date"].Value = curRow.date;
    myInsertCmd.Parameters.Add(new OleDbParameter("@belt",OleDbType.VarChar, 1));
    myInsertCmd.Parameters["@belt"].Value = curRow.belt;
    myInsertCmd.Parameters.Add(new OleDbParameter("@species_code",OleDbType.VarChar, 5));
    myInsertCmd.Parameters["@species_code"].Value = curRow.species;
    myInsertCmd.Parameters.Add(new OleDbParameter("@distance",OleDbType.VarChar, 3));
    myInsertCmd.Parameters["@distance"].Value = curRow.distance;
    myInsertCmd.Parameters.Add(new OleDbParameter("@recType",OleDbType.VarChar, 1));
    myInsertCmd.Parameters["@recType"].Value = 7;
                   
    try
    {
          myInsertCmd.ExecuteNonQuery();
    }
    catch(OleDbException e)
    {
            // Let the user know what went wrong.
            MessageBox.Show (e.Message + "Can not add record.", "Error",
            MessageBoxButtons.OK, MessageBoxIcon.Error);
              break;
    }
                                    
}

myConnection.Close();
0
Comment
Question by:Koderiter
  • 3
5 Comments
 
LVL 12

Accepted Solution

by:
vascov earned 500 total points
ID: 12344635
If this is executing against SQL Serv, date is a reserved word

Try it like this:
string myInsertCmdStr = "INSERT INTO temp " +
               "(herd_unit, study_no, year_est, [date], belt, species_code, distance, recType) " +
               "VALUES (@herd_unit, @study_no, @year_est, @date, @belt, @species_code, @distance, @recType)";

hth

Vasco
0
 
LVL 12

Expert Comment

by:vascov
ID: 12344648
btw, temp is also reserved.

try it like:
string myInsertCmdStr = "INSERT INTO [temp] " +
               "(herd_unit, study_no, year_est, [date], belt, species_code, distance, recType) " +
               "VALUES (@herd_unit, @study_no, @year_est, @date, @belt, @species_code, @distance, @recType)";

hth

Vasco

PS: without the table itself we have to run it virtually :)
0
 
LVL 12

Expert Comment

by:vascov
ID: 12344655
Hmm, actually i take it back :) temp is not reserved.

PS: i need some coffee
0
 
LVL 4

Expert Comment

by:Jigit
ID: 12345045
Koderiter, I can not point on error for sure, but can give you a few directions to check:
1. Check types, for example what is the type of "date" in the database? Is it varchar? What is the type of curRow.date? Is it string? BTW, maybe this should help:
myInsertCmd.Parameters["@recType"].Value = "7";

2. Use some Profiler tool to see what is the final query that is executed against your DB server. Maybe there are some quotes missing or invalid types.

HTH,
Jigit
0
 

Author Comment

by:Koderiter
ID: 12362170
Thanks for your input. That's exactly what the problem was. " date " was the little bugger causing all the problems.

Thanks, agian
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

821 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