5 Replies - 714 Views - Last Post: 21 November 2012 - 05:10 PM Rate Topic: -----

#1 DrisDoe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-November 12

Database exception

Posted 21 November 2012 - 02:57 PM

Hi I'm trying to get this code which should take values from a textbox and insert them into fields in database to work but I keep getting this exception. "System.Data.OleDb.OleDbException (0x80040E14): Syntax error in INSERT INTO statement." I can't find anything wrong with the insert to statement though so I'm not really sure what the problem is.

    public class DBConnection
    {
        public static OleDbConnection GetConnection()
        {
            string connString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=F:\WebApplication4\LOLDatabase.accdb";
            return new OleDbConnection(connString);
        }
        public static string SaveTeacherDetails(string name, string surn, string user, string pass)
        {
            OleDbConnection myConnection = GetConnection();
            string myQuery = "INSERT INTO Teacher( FirstName, Surnname, Username, Password) VALUES ('" + name + "', '" + surn + "', '" + user + "', '" + pass + "')";
            OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);

            try
            {
                myConnection.Open();
                myCommand.ExecuteNonQuery();
                return "Registration successful";
            }
            catch (Exception ex)
            {
                return "Exception in DBHandler" + ex;
            }
            finally
            {
                myConnection.Close();
            }

        }


Is This A Good Question/Topic? 0
  • +

Replies To: Database exception

#2 modi123_1   User is online

  • Suitor #2
  • member icon



Reputation: 14497
  • View blog
  • Posts: 58,115
  • Joined: 12-June 08

Re: Database exception

Posted 21 November 2012 - 03:55 PM

Try using parameters and see whatcha get.
Was This Post Helpful? 1
  • +
  • -

#3 JackOfAllTrades   User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: Database exception

Posted 21 November 2012 - 04:01 PM

Radical idea: somehow check out the contents of the query variable.
Was This Post Helpful? 1
  • +
  • -

#4 DrisDoe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-November 12

Re: Database exception

Posted 21 November 2012 - 04:30 PM

Not really sure what either of you mean
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6535
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: Database exception

Posted 21 November 2012 - 05:02 PM

If you don't understand how to debug and look at the actual value of a variable during run time then you need to STOP. Go do this tutorial, then continue.

What does this error message mean?

You should seriously consider doing at least a couple of these as well.


Q: I do x and y happens which I didn't expect but I don't know how to figure out why. How do I debug and find my problem?
A:

This post has been edited by tlhIn`toq: 21 November 2012 - 05:02 PM

Was This Post Helpful? 1
  • +
  • -

#6 DrisDoe   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 21-November 12

Re: Database exception

Posted 21 November 2012 - 05:10 PM

If thats what you meant then I'm sure all the values are strings, but either way I figured it out but still don't really understand what the problem was. For some reason it wouldn't work with the field name "Password", I changed that and everything works fine.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1