views:

258

answers:

4

Hey Guys, here is my code for this, the only help i get from VS is that the INSERT INTO statement syntax is incorrect?

I have gone through all of the code and just cannot see where i have gone wrong, can someone gimme a hand please?

        public void New(string ApplicationStartupPath, string FileName, string Department, string Month, string Year)
    {
        string sql = "INSERT INTO PodcastsDir (FileName, Department, Month, Year) VALUES (@FileName, @Department, @Month, @Year)";
        using (OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" + ApplicationStartupPath.ToString() + ""))
        using (OleDbCommand cmd = new OleDbCommand(sql, conn))
        {
            cmd.Parameters.Add("@FileName", OleDbType.VarChar);
            cmd.Parameters.Add("@Department", OleDbType.VarChar);
            cmd.Parameters.Add("@Month", OleDbType.VarChar);
            cmd.Parameters.Add("@Year", OleDbType.VarChar);

            conn.Open();

            cmd.Parameters[0].Value = FileName;
            cmd.Parameters[1].Value = Department;
            cmd.Parameters[2].Value = Month;
            cmd.Parameters[3].Value = Year;

            cmd.ExecuteNonQuery();
        }

    }

Thanks Ash

+2  A: 

You seem to be using Access - aren't Month and Year reserved words? Try enclosing them in square brackets (is this the correct delimiter for Access?) and trying again.

David M
that sounds familiar!
Mitch Wheat
+1. nicely spotted
Mitch Wheat
Correct, but that's not the main reason why it's not working
Philippe Leybaert
He is not using Access -- he's programming in C#, and thus using only Jet/ACE.
David-W-Fenton
+1  A: 

If you are using Access aren't the parameters actually place holders rather than named parameters.

Change your SQL string to use ? as a placeholder rather than a named parameter and ensure you add the parameters in the same sequence as the ? appear in the SQL string.

Colin Mackay
Then again, it might be the reserved word thing :)
Colin Mackay
Thanks dude this worked, tried the brackets but it wasnt too suree, i just changed the month and year name an then used the ? and it worked, thanks guys :)
Ash
He's not using Access at all -- he's only using Jet/ACE.
David-W-Fenton
+1  A: 

AFAIK, Access doesn't support named parameters. You should use "?" in your query to specify parameters:

    string sql = "INSERT INTO PodcastsDir (FileName, Department, [Month], [Year]) VALUES (?, ?,?,?)";

You should also put Year and Month in square brackets.

The rest of your code can be left unchanged.

Philippe Leybaert
Access is not involved here, as the programming environment is C#. Only Jet/ACE is involved.
David-W-Fenton
A: 

You might want to try this, parameters are fine in Jet ie Access

"INSERT INTO PodcastsDir ([FileName], [Department], [Month], [Year]) VALUES (@FileName, @Department, @Month, @Year)"; 
Frank Kerrigan