insert a row into a access database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sarabonn
    New Member
    • Nov 2008
    • 69

    insert a row into a access database

    hallo ,

    Iam trying to insert a row into access table. the values i insert is from textboxes. iam using visual c# .. No error is there but the row is not there after the execution, here is my code.

    private void button1_Click(o bject sender, EventArgs e)
    {
    txt1 = textBox1.Text;
    txt2 = textBox2.Text;
    String connectionStrin g = "Provider=Micro soft.ACE.OLEDB. 12.0;Data Source=C:\\User info.accdb";
    OleDbConnection connection = new OleDbConnection (connectionStri ng);
    OleDbDataAdapte r adapter = new OleDbDataAdapte r();
    OleDbCommand command;

    // Create the InsertCommand.
    command = new OleDbCommand("I NSERT INTO userinfo (Username, Pwd) VALUES (‘" + txt1 + "’ , ‘" + txt2 + "’)", connection);

    connection.Open ();

    adapter.InsertC ommand = command;
    connection.Clos e();

    }

    Thank you..

    Dinesh.
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    You missed the crucial line where you are then supposed to call the ExecuteNonQuery command.

    Comment

    • sarabonn
      New Member
      • Nov 2008
      • 69

      #3
      Originally posted by r035198x
      You missed the crucial line where you are then supposed to call the ExecuteNonQuery command.
      Thank you for you reply, where and how should i insert the executenonquery command.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Just read the articles called "How to use a database in your program" in the howTos area.
        While we are at this I just want to say that ...
        That kind of code is not very good practice at all even though there is lots of it floating around. Form designing code (view) should not be mixed with processing code (controller) which should not be mixed with database access code (model).
        Better to have your database connection code in separate model classes.

        Comment

        • kunal pawar
          Contributor
          • Oct 2007
          • 297

          #5
          For insert command not need to use Data adapter
          Following is modified code

          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);
          //[B]OleDbDataAdapter adapter = new OleDbDataAdapter();[/B]
          OleDbCommand command;
          
          // Create the InsertCommand.
          command = new OleDbCommand("INSERT INTO userinfo (Username, Pwd) VALUES (‘" + txt1 + "’ , ‘" + txt2 + "’)", connection);
          
          connection.Open();
          
          //[B]adapter.InsertCommand = command;[/B]
          if(command.ExecuteNonQuery()>0)
          {
               MessageBox.Show("Records inserted successfully.");
          }
          else
          {
               MessageBox.Show("Records can insert.");
          }
          connection.Close();
          
          }
          Last edited by r035198x; Nov 20 '08, 01:30 PM. Reason: Added code tags. Please don't forget them next time.

          Comment

          Working...