user612041
user612041

Reputation: 215

Database to Rich Text Box Problem

I am having some trouble in binding my data from my Access database into my rich text box on my Visual C# form. Here is my code:

private void Form2_Load(object sender, EventArgs e)

        {
            string connectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\\Documents and Settings\\Harvey\\Desktop\\Test.accdb");
            OleDbConnection conGet = new OleDbConnection(connectionString);
            OleDbCommand cmdGet = new OleDbCommand();

            try
            {
                //open connection
                conGet.Open();

                cmdGet.CommandType = CommandType.Text;
                cmdGet.Connection = conGet;
                cmdGet.CommandText = "SELECT * FROM Paragraph";

                richTextBox.Rtf = cmdGet.ExecuteScalar().ToString();

                conGet.Close();

                MessageBox.Show("Data loaded from Database");
            }
            catch (Exception ex)
            {
                //display generic error message back to user
                MessageBox.Show(ex.Message);
            }
            finally
            {
                //check if connection is still open then attempt to close it
                if (conGet.State == ConnectionState.Open)
                {
                    conGet.Close();
                }
            }
        }

When I press the button to load up the form that will contain the rich text box, I get a pop up box saying 'File Format Not Valid' Essentially in my database, there is 1 column that has data in it (1 word per row in that column)

The code I have above was taken from the Internet and other people have had success in using it, i'm just now sure what's going wrong

Upvotes: 0

Views: 4642

Answers (4)

Joel Coehoorn
Joel Coehoorn

Reputation: 415810

in my database, there is 1 column that has data in it (1 word per row in that column)

So you need many rows? Than you shouldn't use .ExecuteScalar(). Do this:

private void Form2_Load(object sender, EventArgs e)
{
    string connectionString = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source= C:\\Documents and Settings\\Harvey\\Desktop\\Test.accdb");
    using (OleDbConnection conGet = new OleDbConnection(connectionString))
    using (OleDbCommand cmdGet = new OleDbCommand())
    try
    {
        cmdGet.CommandType = CommandType.Text;
        cmdGet.Connection = conGet;
        cmdGet.CommandText = "SELECT * FROM Paragraph";

        StringBuilder paragraph = new StringBuilder();

        //open connection
        conGet.Open() 
        using (OleDbDataReader rdr = cmdGet.ExecuteReader())
        {
            while (rdr.Read())
            {
               parapraph.Append(rdr.GetString(0)).Append(" ");
            }
            rdr.Close();
        }

        richTextBox.Rtf = paragraph.ToString();
        // OR...
        richTextBox.Text = paragraph.ToString();

    }
    catch (Exception ex)
    {
        //display generic error message back to user
        MessageBox.Show(ex.Message);
    }
}

Notice that I moved a few other things around as well - and don't worry, I do close your connection properly, even though you can't see it.

Upvotes: 1

digEmAll
digEmAll

Reputation: 57210

You can't add any text to a RichTextBo.Rtf property, it has to be in the RTF Format.

Try this for example:

richTextBox.Rtf = "Hello world";

it will throw the the same exception.

You can use the RichTextBox.Text property instead, that can accept a plain text.

Upvotes: 3

Mitja Bonca
Mitja Bonca

Reputation: 4546

Are you sure you want to select all columns from the dataBase table? Like this:

cmdGet.CommandText = "SELECT * FROM Paragraph";

I would say this is not correct, you only need to get one column where the text is saved, like:

cmdGet.CommandText = "SELECT MyTextColumn FROM Paragraph"; //Maybe even som Where comparison!!

And it would be better to use Text property of the richTextBox to fill into it and ToStirng method on Scalar:

this.richTextBox1.Text = cmd.ExecuteScalar.Tostring();

Upvotes: 0

tom502
tom502

Reputation: 701

Try using the Text property instead as it's just a string.

richTextBox.Text = cmdGet.ExecuteScalar().ToString();

Upvotes: 0

Related Questions