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
Loading
Koteswararao MallisettiPosted Feb 18, 2011, 8:08 AM
one situation is : for address column you declare nvrchar(100) means the address column accept at the maximum 100 chars if you are trying to insert more than 100 it will give that type of error so please check all of you table column lenghts.
for infinity column u must declare as navarchar(max) if you check all of your table columns then your problem will solve.
bye,
koti.