1
Reply

Update and delete functions

Avinash Yoganand

Avinash Yoganand

Feb 18 2011 7:17 AM
2.8k
Hello,

I have a desktop application wherein i have just one form. I have used textbox to display data from the database. I want to update and delete data that is being displayed on the textbox. when i try to do it i get an error message stating
"The field is too small to accept the amount of data you attempted to add. Try inserting or passing less data". This is the error i get when i try to update the textbox. And when i click delete button it says "object reference not set to an instance of an object"

this is my update and delete function code:

private void btnUpdate_Click(object sender, EventArgs e)
      {
try
        {
          Con.Open();
          string insert = "INSERT into Customers(CustomerID, CompanyName, ContactName, Address, City, Region, Country, Phone)" + "Values(@CustomerID,@CompanyName, @ContactName, @Address, @City, @Region, @Country, @Phone)";
        OleDbCommand Cmd = new OleDbCommand(insert, Con);
        Cmd.Parameters.AddWithValue("@CustomerID", txtCustomerID);
        Cmd.Parameters.AddWithValue("@CompanyName", txtCompanyName);
        Cmd.Parameters.AddWithValue("@ContactName", txtContactName);
        Cmd.Parameters.AddWithValue("@Address", txtAddress);
        Cmd.Parameters.AddWithValue("@City", txtCity);
        Cmd.Parameters.AddWithValue("@Region", txtRegion);
        Cmd.Parameters.AddWithValue("@Country", txtCountry);
        Cmd.Parameters.AddWithValue("@phone", txtPhone);
        Cmd.ExecuteNonQuery();
      }
catch (OleDbException exp)
        {
          MessageBox.Show(exp.Message.ToString());
        }
        Con.Close();
      }

private void btnDelete_Click(object sender, EventArgs e)
      {
        Con = new OleDbConnection("Provider = Microsoft.Jet.OLEDB.4.0;data source = F:\\Nwind.mdb");
        DataSet ds = new DataSet();
        try
        {
          Con.Open();
          OleDbDataAdapter adapter = new OleDbDataAdapter("SELECT * FROM Customers", Con);
          OleDbCommandBuilder cmdBuilder = new OleDbCommandBuilder(adapter);
          adapter.MissingSchemaAction = MissingSchemaAction.AddWithKey;
          adapter.Fill(ds, "Customers");
          DataRow row1 = ds.Tables["Customers"].Rows.Find("CustomerID");
          row1.Delete();
          adapter.Update(ds, "Customers");
        }
        catch (OleDbException exp)
        {
          MessageBox.Show(exp.Message.ToString());
        }
        Con.Close();
      }

Please help me in this regard.

Thanks in advance,

Avinash

Answers (1)