0

I'm playing around with C# and Access databases trying to connect them and insert data through a web to the Access database, however I keep getting this:

"A first chance exception of type 'System.Data.OleDb.OleDbException' occurred in System.Data.dll"

on my output console and get nothing else.

not sure what I'm doing wrong but any help will do. Thanks

here's my code for getting the connection:

public partial class reg_Test : System.Web.UI.Page
{

private static OleDbConnection GetConnection()
{
    String connString;
    connString = @"Provider=Microsoft.JET.OLEDB.4.0;Data Source=C:\Users\Wisal\Documents\Visual Studio 2012\WebSites\WebSite3\test-db1.mdb";

    return new OleDbConnection(connString);

}

here's my code for the submit button after user fill the text boxes Name and Surname:

protected void submitButtn_Click(object sender, EventArgs e)
{
    OleDbConnection myConnection = GetConnection();
    String TextBox1 = nameBox.Text;
    String TextBox2 = snameBox.Text;

    try
    {
        myConnection.Open();
Console.WriteLine("Connection Opened");
String myQuery = "INSERT  INTO client values ([name], surname) values ('" + nameBox.Text + "','" + snameBox.Text + "');";


OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);
myCommand.ExecuteNonQuery();
    }





    finally
    {
        myConnection.Close();
    }
}
}

1 Answer 1

1

Change your query statement like below.

    myConnection.Open();
    Console.WriteLine("Connection Opened");
    String myQuery = "INSERT INTO client([name], surname) values ('" + nameBox.Text "','"+ snameBox.Text + "');";
    OleDbCommand myCommand = new OleDbCommand(myQuery, myConnection);
    myCommand.ExecuteNonQuery();
Sign up to request clarification or add additional context in comments.

12 Comments

Please remove * in your statement
I'm still getting the same error and nothing else. this leaves me clueless :( Have I done my OleDbConnection right? @KaushikMaheta Thank you
Ah just noticed that removed it but still no change :( @KaushikMaheta Thanks
I used the parameters but still no change.. i tried removing the exception handler and now I got a "Missing semicolon (;) at the end of sql statement" @KaushikMahata
nope without the Exception handler I still get " semicolon missing error" and with the Exception Handler I get "First chance exception" thank you so much
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.