8 Replies - 1560 Views - Last Post: 01 August 2013 - 03:19 PM Rate Topic: -----

#1 PSCA   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 10-October 12

C# form connection to Access Database

Posted 01 August 2013 - 08:34 AM

Successfully connected the access database to the form in VB 2010 C#

However, I cant insert information using the form to add new record in my access database

OleDbCommand command = new OleDbCommand();
string FirstName = FName.Text;
string LastName = LName.Text;

try{
command.CommandText = "INSERT INTO Customers (FirstName, LastName) values ('" + FirsName + "','" + LastName + "');";
command executeNonQuery();
}

catch{
MessageBox.Show("Failed to sign up");
}

finally{
connect.Close();
}




Note: the database and form are successfully connected

Is This A Good Question/Topic? 0
  • +

Replies To: C# form connection to Access Database

#2 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,250
  • Joined: 12-December 12

Re: C# form connection to Access Database

Posted 01 August 2013 - 08:52 AM

Disable the try..catch block during development so that you can see, and post, the full error message - if any.

There are no statements which associate your new oleDbCommand object with your connect object.

And it should be

command.executeNonQuery();

This post has been edited by andrewsw: 01 August 2013 - 08:53 AM

Was This Post Helpful? 0
  • +
  • -

#3 Charles:)   User is offline

  • D.I.C Regular

Reputation: 149
  • View blog
  • Posts: 359
  • Joined: 26-November 09

Re: C# form connection to Access Database

Posted 01 August 2013 - 08:52 AM

What's the actual error that you're getting? Does line 11 execute and display the message box? If so then change your catch block like so:

catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}



Then post the exact message here.
Was This Post Helpful? 0
  • +
  • -

#4 PSCA   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 10-October 12

Re: C# form connection to Access Database

Posted 01 August 2013 - 08:56 AM

View PostCharles:), on 01 August 2013 - 08:52 AM, said:

What's the actual error that you're getting? Does line 11 execute and display the message box? If so then change your catch block like so:

catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}



Then post the exact message here.



"Data type mismatch in criteria expression" -- the message im receiving after clicking the button

View Postandrewsw, on 01 August 2013 - 08:52 AM, said:

Disable the try..catch block during development so that you can see, and post, the full error message - if any.

There are no statements which associate your new oleDbCommand object with your connect object.

And it should be

command.executeNonQuery();


Sorry andrewsw, im new to this, i dont quite understand what you are saying
Was This Post Helpful? 0
  • +
  • -

#5 Michael26   User is offline

  • Futurama: Insert funny joke here
  • member icon

Reputation: 414
  • View blog
  • Posts: 1,664
  • Joined: 08-April 09

Re: C# form connection to Access Database

Posted 01 August 2013 - 08:57 AM

Line 6 should say FirstName not FirsName, and learn to use sql parameters

This post has been edited by Michael26: 01 August 2013 - 08:58 AM

Was This Post Helpful? 0
  • +
  • -

#6 PSCA   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 10-October 12

Re: C# form connection to Access Database

Posted 01 August 2013 - 08:58 AM

Thank you guys for all of the replies
managed to fix the problem!
Was This Post Helpful? 0
  • +
  • -

#7 andrewsw   User is offline

  • never lube your breaks
  • member icon

Reputation: 6819
  • View blog
  • Posts: 28,250
  • Joined: 12-December 12

Re: C# form connection to Access Database

Posted 01 August 2013 - 09:17 AM

View PostPSCA, on 01 August 2013 - 03:58 PM, said:

Thank you guys for all of the replies
managed to fix the problem!

So how did you fix it?
Was This Post Helpful? 0
  • +
  • -

#8 PSCA   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 35
  • Joined: 10-October 12

Re: C# form connection to Access Database

Posted 01 August 2013 - 09:20 AM

the type for LastName in the access database was set to date/time by accident
all i had to do was change that to text
Was This Post Helpful? 0
  • +
  • -

#9 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: C# form connection to Access Database

Posted 01 August 2013 - 03:19 PM

It's still a really good idea to learn how to parameterize your queries. Read this thread to understand why and how:

http://www.dreaminco...ery-a-database/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1