locked
Insert Row into DB RRS feed

  • Question

  • User693597704 posted

    I am attempting to save a new row to an access DB file... I am using winforms with databound textboxes..... When I insert a break point into the procedure it is doing as expected.  However When I open the file in Access I do not see the added row there. What am I missing?

    here is my code

    private void TestInsert_Click(object sender, EventArgs e)
            {
                string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\\cntctpw.mdb";
                System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(ConnectionString);
                DataRow row;
                DataSet ds = new DataSet();
    
                try
                {
                    conn.Open();
                    System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter
                    ("SELECT * FROM Table1", conn);
                    System.Data.OleDb.OleDbCommandBuilder cmdBuilder = new System.Data.OleDb.OleDbCommandBuilder(adapter);
                    adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
                    adapter.Fill(ds, "Table1");
                    row = ds.Tables["Table1"].NewRow();
                    row["FirstName"] = FirstNameText.Text;
                    row["LastName"] = LastNameText.Text;
                    row["CompanyName"] = CompanyNameText.Text;
                    
                    ds.Tables["Table1"].Rows.Add(row);
                    adapter.Update(ds, "Table1");
                    
                }
    
                catch (System.Data.OleDb.OleDbException exp)
                {
                    MessageBox.Show(exp.Message.ToString());
                }
    
                if (conn.State == ConnectionState.Open)
                    conn.Close();
    
            }

    Sunday, September 3, 2017 2:28 AM

All replies

  • User2103319870 posted

    Wrong Post

    Sunday, September 3, 2017 2:45 AM
  • User693597704 posted

    OK So this code kicks out a message saying "No Value given for one or more required parameters.... I look in the DB and there is a field named ID that is auto generated(record number)

    private void SaveRecord_Click(object sender, EventArgs e)
            {
                //LockTextBoxes();
                try
                {
                    System.Data.OleDb.OleDbConnection con = new System.Data.OleDb.OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|\cntctpw.mdb");
                    con.Open();
                    System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand("INSERT INTO Table1(FirstName, LastName,CompanyName,Email,Website,HomePhone,WorkPhone,CellPhone,UserName,WebPW,AddressLine1,AddressLine2,City,State,ZipCode,Notes) values(FirstNameText.Text, LastNameText.Text,CompanyNameText.Text, EmailText, WebsiteText.Text, HomePhoneText.Text, WorkPhoneText.Text,CellPhoneText.Text, UserNameText.text, PasswordText.Text,AddressLine1Text.Text,AddressLine2Text.Text,CityText.Text,StateText.Text,ZipCodeText.Text,NotesText.Text)", con);
                    toolStripTextBox1.Text = "";
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Record inserted successfully.");
                    //lbl_msg.Text = "Record inserted successfully.";
                    con.Close();
                }
                catch (System.Data.OleDb.OleDbException exp)
                {
                    MessageBox.Show(exp.Message.ToString());
                }
    
    
            }

    Sunday, September 3, 2017 4:01 AM
  • User693597704 posted

    sticking with original method

    Sunday, September 3, 2017 6:07 AM
  • Monday, September 4, 2017 7:52 AM