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.

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();
}

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.