Small issue when using INSERT INTO from a C# Windows App (1 Viewer)

StoneTheCrows

New member
Local time
Today, 17:07
Joined
Feb 6, 2013
Messages
4
*SOLVED* - Small issue when using INSERT INTO from a C# Windows App

Hi all,
I wonder if anyone has come across this issue before and if so did they find a resolution?

I have a Windows application that allows users to submit issues to an Access Issues database table, the code for which is as follows:

Code:
private void
CommitIssue
      ()
        {
            int newStatus = 1; // = New
            int newOpener = Employees[EmployeeName];
            int newCategory = Categories[Category];
            int newPriority = Priorities[Priority]; //Normal
            string newTitle = SummaryText;
            string newDescription = DescriptionText;

            using (OleDbConnection conn = GetConnection())
            {
                string cmdText = @"INSERT INTO Issues " +
                                 "([Title], [Assigned To], [Opened By], [Opened Date], [Status], [Category], [Priority], [Description], [Due Date], [TaskID]) " +
                                 "VALUES (@Title, @AssignedTo, @OpenedBy, @OpenedDate, @Status, @Category, @Priority, @Description, @DueDate, @TaskID )";
                OleDbCommand cmd = new OleDbCommand(cmdText, conn);

                using (cmd)
                {
                    try
                    {
                        cmd.Parameters.Add("@Title", OleDbType.VarWChar, 150).Value = newTitle;
                        cmd.Parameters.Add("@AssignedTo", OleDbType.Integer).Value = DBNull.Value;
                        cmd.Parameters.Add("@OpenedBy", OleDbType.Integer).Value = newOpener;
                        cmd.Parameters.Add("@OpenedDate", OleDbType.Date).Value = ReportedDateTime;
                        cmd.Parameters.Add("@Status", OleDbType.Integer).Value = newStatus;
                        cmd.Parameters.Add("@Category", OleDbType.Integer).Value = newCategory;
                        cmd.Parameters.Add("@Priority", OleDbType.Integer).Value = newPriority;
                        cmd.Parameters.Add("@Description", OleDbType.LongVarWChar, 0).Value = newDescription;
                        cmd.Parameters.Add("@DueDate", OleDbType.Date).Value = DBNull.Value;
                        cmd.Parameters.Add("@TaskID", OleDbType.Integer).Value = DBNull.Value;
                        conn.Open();
                        cmd.ExecuteNonQuery();
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "INSERT Error");
                    } // End catch
                } // End using
            } // End using
        } // End of method

This code works exactly as I expect it to with one small exception.

The 'Description' is being sent to a Memo field in the Datatable, but if the description text contains the newline characters \r\n the newline does not make it into the Memo field and is actually replaced by the space character. I have debugged the code and checked that the newline characters are properly passed into the Parameter, which they are.

Any ideas anyone?

Thanks in advance.
 
Last edited:

StoneTheCrows

New member
Local time
Today, 17:07
Joined
Feb 6, 2013
Messages
4
Ok, in case anyone is interested, I found the problem and the solution.

The Memo field in the Datatable is set up as a Rich Text Memo Field and as such the text is stored as HTML.

The solution was to modify the text from the user entry form as follows:

Code:
string newDescription = "<div>" + DescriptionText.Replace("\r\n","</div> <div>") + "</div>";

Cheers,
 

Users who are viewing this thread

Top Bottom