0

Hallo everyone,

Iam using C# to insert a row into access table. Iam getting the values to insert from textbox. I got the error "cannot open action query". Here is my code

private void button1_Click(object sender, EventArgs e)
        {
            txt1 = textBox1.Text;
            txt2 = textBox2.Text;
            String connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Userinfo.accdb";
     
            OleDbConnection connection = new OleDbConnection(connectionString);
            OleDbDataAdapter adapter = new OleDbDataAdapter();


            OleDbCommand command = new OleDbCommand();
            // Create the InsertCommand.

            command.CommandType = CommandType.Text;
            command.CommandText = "INSERT INTO userinfo (Username, Pwd) VALUES (‘" + txt1 + "’ , ‘" + txt2 + "’)";
            command.Connection = connection;
            //command.Parameters.Add("txt1", OleDbType.Char, 5, "Username");
            //command.Parameters.Add("txt2", OleDbType.VarChar, 10, "Pwd");

            adapter.InsertCommand = command;

            connection.Open();

            command.ExecuteNonQuery();
            connection.Close();
}

.

Any idea ?.

Thank you.

Dinesh.

3
Contributors
3
Replies
5
Views
8 Years
Discussion Span
Last Post by sarabonn
0

Hi,

private void button1_Click(object sender, EventArgs e)
        {
            txt1 = textBox1.Text;
            txt2 = textBox2.Text;
            String connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Userinfo.accdb";

            OleDbConnection connection = new OleDbConnection(connectionString);
              connection.Open();
            OleDbCommand command = new OleDbCommand("INSERT INTO userinfo (Username, Pwd) VALUES (‘" + txt1 + "’ , ‘" + txt2 + "’)";
           command.ExecuteNonQuery();
            connection.Close();
}

Edited by Nick Evan: Fixed formatting

0

hi
u need to specify proper database extension. i think here s the probelm ? u given

String connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Userinfo.accdb";

so plz change to mdb this s ths access dbase extension and u have to use

connection.open()

chk it this and let me know

if ur problem solved plz mark as Solved

0

thank you for your reply. actually i solved the problem but the problem is not because of the extension. This is office 2007 access database extension. ".mdb" is the extension of the previous version office. Anyway Thank you for reply.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.