Question

I have 4 controls. One is reference no, date, subject and body. reference no and subject are text fields in msaccess with 255 characters length. Body field is memo field. Problem is when I try to insert values into the table using parameter function and I take only memo field in my SQL commandText the command function is executed successfully.

But as soon as I include any other field I start getting error "The field is too small to accept the amount of data you entered. Try inserting or pasting less data." Can anybody help me how to solve this error?

Coding is as follows :

if (btnNew.Content.Equals("_New"))
{
    btnNew.Content = "_Save";

    Clear_Contents();

    dtpDate.Focus();

    btnMod.IsEnabled = false;

    btnDel.IsEnabled = false;

   btnPrint.IsEnabled = false;

}

else

        {

            OleDbConnection conn = DBConnection.getDBConnection();

            OleDbCommand command = conn.CreateCommand();

            string rtfText;

            TextRange richText = new TextRange(txtBody.Document.ContentStart,txtBody.Document.ContentEnd);

            using (MemoryStream ms = new MemoryStream())

            {

                richText.Save(ms, DataFormats.Rtf);

                rtfText = Encoding.ASCII.GetString(ms.ToArray());

            }

            try
            {
                command.Parameters.Add("@body", OleDbType.LongVarChar).Value=rtfText;
              command.Parameters.Add("@refno",OleDbType.VarChar,255).Value=txtPCode.Text;
                command.CommandText = "insert into letter_master(total_ref,let_body)values (@refno,@body)";
                conn.Open();
                command.ExecuteNonQuery();
                conn.Close();
                MessageBox.Show("Record Saved Successfully !!!", DBConnection.Comp_Name, MessageBoxButton.OK, MessageBoxImage.Information);
            }                
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
Was it helpful?

Solution

You need to change the order in which you add the parameters to your OleDbCommand parameters collection

command.Parameters.Add("@refno",OleDbType.VarChar,255).Value=txtPCode.Text;
command.Parameters.Add("@body", OleDbType.LongVarChar).Value=rtfText;
command.CommandText = "insert into letter_master(total_ref,let_body)values (@refno,@body)";
conn.Open();

This is required by the OleDb provider that cannot recognize the parameter by their name, but only by their position.

This means that the first parameter's value added to the collection will be used where the first placeholder is found, the second for the second placeholder. So, in your original code, the OleDb provider will try to save the value from the richtextbox field in the field for the total_ref causing the mentioned error.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top