Howto: Connect to Databases

TX Text Control can be connected to any type of database, and store formatted text in any of its text formats. You can save a complete document as a record, or load text from a database into table cells and marked text fields. Typical applications which can be created are mail merge, report generators, formatted data entry masks, and all types of business applications where documents are created from information stored in a database.

This chapter shows you how to save the contents of a Text Control to a database. The data is stored as an RTF formatted string in a text field. For an advanced sample program which shows how to use databases with tables and marked text fields, please also refer to the TX Quote Generator sample program.

The source code for this example is contained in the following directories:

  • %USERPROFILE%\My Documents\TX Text Control 29.0.NET for Windows Forms\Samples\WinForms\VB.NET\Database
  • %USERPROFILE%\My Documents\TX Text Control 29.0.NET for Windows Forms\Samples\WinForms\CSharp\Database

Used TX Text Control controls:

  • TXTextControl.TextControl

Relevant API Links

The Sample Program

The sample program consists of a form with two Text Controls, both of which display the same record of a database. The upper control displays it as formatted text, whereas, for demonstration purposes, the lower control shows the record's RTF source code.

Image

Clicking the Next button will save any changes you have made in the upper window, and advance to the next record.

Saving a Record

All data access is handled by two methods, which are Load and Save. The Save method stores formatted text to a string, which is then passed on to the database engine using Visual Studio's ADO functions.

private void PutRecord()
{
    string sRTFData;

    textControl1.Save (out sRTFData,
        TXTextControl.StringStreamType.RichTextFormat);
    dsRTFData.Tables[0].Rows[iCurrentRow][0] = sRTFData;
}
Private Sub PutRecord()
    Dim SRTFData As String

    TextControl1.Save(SRTFData, TXTextControl.StringStreamType.RichTextFormat)
    DsRTFData.Tables(0).Rows(ICurrentRow).Item("rtf_item") = SRTFData
End Sub

Other text formats can be used by simply changing the second parameter of the Save method.

Retrieving a Record

The TextControl.Load method is used to display RTF formatted text in Text Control 1. As Text Control 2 just displays the RTF source code without any formatting, we can simply use the Text property to load it.

private void GetRecord()
{
    string sRTFData = dsRTFData.Tables[0].Rows[iCurrentRow].ItemArray[0].ToString() ;

    textControl1.Load(sRTFData, TXTextControl.StringStreamType.RichTextFormat);
    textControl2.Text = sRTFData;
}
Private Sub GetRecord()
    Dim SRTFData As String = DsRTFData.Tables(0).Rows(ICurrentRow).Item("rtf_item")

    TextControl1.Load(SRTFData, TXTextControl.StringStreamType.RichTextFormat)
    TextControl2.Text = SRTFData
End Sub

The text format used in this example is RTF, which has the advantage of being accessible by most word processing programs. If this is not an issue, Text Control's binary format may be a better choice, as it is more compact and will take up less space in a database.