sairam

sairam

  • NA
  • 69
  • 0

richtextbox ---- simple mistake ...i could not identify. pleas ehelp

Jan 7 2009 12:52 PM

I have some RTF content (it includes images and text)in Database.

 

I am trying to insert the content into a richtextbox at the current cursor position.

 

I am trying two ways.

 

First Method.

 

1.  SqlConnection cn2 = null;

            SqlCommand cmd2 = null;

            SqlDataReader reader2 = null;

            String gettext = "select TemplateContent from tblAdminForms where TemplateName= '" + treeAdminTemplates.SelectedNode.Text + "' and  FlagInActive = 0";

            cn2 = new SqlConnection("User id=cmslogin;password=cmsunlock;database=CMS2;Data source=primasqltst\\qa");

            cn2.Open();

            cmd2 = new SqlCommand(gettext, cn2);

            reader2 = cmd2.ExecuteReader();

 

            while (reader2.Read())

            {

                string test = reader2[0].ToString();

        String Field2 = rchEditor1.Rtf.Insert(rchEditor1.Rtf.Length - 4, test);

                rchEditor1.Rtf = Field2;

 

            }

            reader2.Close();

 

the output is

 

โ€œโ€โ€ the RTF content is getting inserted in the richtextbox , but at the end of the last word but not at the current cursor position. So the text is being appended in the last line.

 

 

 

 

 

 

Second method (2)

 

SqlConnection cn2 = null;

            SqlCommand cmd2 = null;

            SqlDataReader reader2 = null;

            String gettext = "select TemplateContent from tblAdminForms where TemplateName= '" + treeAdminTemplates.SelectedNode.Text + "' and  FlagInActive = 0";

            cn2 = new SqlConnection("User id=cmslogin;password=cmsunlock;database=CMS2;Data source=primasqltst\\qa");

            cn2.Open();

            cmd2 = new SqlCommand(gettext, cn2);

            reader2 = cmd2.ExecuteReader();

 

            while (reader2.Read())

            {

                string test = reader2[0].ToString();        appendtext_to_cursor_position(rchEditor1, test, rchEditor1.SelectionStart);

            }

            reader2.Close();

 

where Function definition is

 

void appendtext_to_cursor_position(RichTextBox rtf, String str, int positiontoinsert)

        {

 

            int CurrentCeret = rtf.SelectionStart + str.Length;

            rtf.Select(positiontoinsert, rtf.TextLength - positiontoinsert);

            str += rtf.SelectedText;

            rtf.SelectedText = String.Empty;

            rtf.AppendText(str);

            rtf.SelectionStart = CurrentCeret;

        }

 

 

 

 

 

 

Here the output is

 

The entire RTF content is copied in the richtextbox in text format but not in RTF format. What I mean is the images is not getting copied instead the images saved as some junk language in rtf format are being copied.

 

Output should be

An Image and Then after two lines sometext.

 

But output is AEEEEEEEEEEEEEEEEEEEEEEEEEENIFDGGGGGGFIFFFFFFFFFFFFFFFFFFFfff

This is dotnet.

 

The image and text is not getting converted into desired format.

 

SO which of the above methods should I use and please correct the mistake I am doing. Iam breaking my head for this

 

 

 

 


Answers (2)