none
c# Access Database INSERT RRS feed

  • Question


  • Why I am not able to insert the data. Really appreciate the help.

    CODE:

    private void button1_Click_1(object sender, EventArgs e)
            {
               
             
               string str = "INSERT INTO Customers(ContactFirstName,ContactLastname,BillingAddress,PhoneNumber,EmailAddress,Notes)VALUES( ('" + contactFirstNameTextBox.Text + "'),('" + contactLastNameTextBox.Text + "'),('" + billingAddressTextBox.Text + "'),('" + phoneNumberTextBox.Text + "'),('" + emailAddressTextBox.Text + "'),('" + notesTextBox.Text + "'))";
             
             
               OleDbDataAdapter cmd = new OleDbDataAdapter(str, con);
               DataSet dset = new DataSet();
               con.Open();
          
               cmd.Fill(dset);
               cmd.Update(dset);

             
               con.Close();
    }
              
           


    Monday, November 26, 2007 5:16 AM

Answers

  • Hi SCHHAJ,

    I have modified the code a little bit. I have tested the below code also and it works for me. I hope this is useful.

    Regards,
    Raghu

    --------------------------------------------------------------------------
     OleDbConnection myAccessConn = null;
                try
                {              
                       string dbName = @"Access.mdb";
                       string accessConn = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", dbName);
                       myAccessConn = new OleDbConnection(accessConn);
                       myAccessConn.Open();

    string str = "INSERT INTO Customers(ContactFirstName,ContactLastname,BillingAddress,PhoneNumber,EmailAddress,Notes)VALUES( ('" + contactFirstNameTextBox.Text + "'),('" + contactLastNameTextBox.Text + "'),('" + billingAddressTextBox.Text + "'),('" + phoneNumberTextBox.Text + "'),('" + emailAddressTextBox.Text + "'),('" + notesTextBox.Text + "'))";

    OleDbCommand myAccessCommand = new OleDbCommand(str, myAccessConn);
    myAccessCommand.ExecuteNonQuery();
    }
              catch (Exception ex)
              {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.StackTrace);
                    Console.WriteLine(ex.InnerException.Message);
              }
    finally
              {
                    if (myAccessConn != null)
                    {
                        myAccessConn.Close();
                        MessageBox.Show("Completedd Adding row");
                    }
              }



    Monday, November 26, 2007 3:41 PM