HelpNeeder
HelpNeeder

Reputation: 6490

How to avoid new line when reading from MS Access file?

I am trying to read data from MS Access 2007 file and everything seems fine. The problem is that I am storing some values as null and when I am trying to read only few values, the null values create new line in my output.

How can I avoid this, or what other approach should I use while reading from MS Access file?

This is what I am using:

        while (readPersonalData.Read())
        {
            // Count all entries read from the reader.
            countEntries++;

            txtDisplay.Text += "Entry ID: " + readPersonalData.GetValue(0) + Environment.NewLine;
            if (readPersonalData.GetValue(1) != null) 
                txtDisplay.Text += "Type: " + readPersonalData.GetValue(1) + Environment.NewLine;
            if (readPersonalData.GetValue(2) != null) 
                txtDisplay.Text += "URL: " + readPersonalData.GetValue(2) + Environment.NewLine;
            if (readPersonalData.GetValue(3) != null) 
                txtDisplay.Text += "Software Name: " + readPersonalData.GetValue(3) + Environment.NewLine;
            if (readPersonalData.GetValue(4) != null) 
                txtDisplay.Text += "Serial Code: " + readPersonalData.GetValue(4) + Environment.NewLine;
            if (readPersonalData.GetValue(5) != null) 
                txtDisplay.Text += "User Name: " + readPersonalData.GetValue(5) + Environment.NewLine;
            if (readPersonalData.GetValue(6) != null) 
                txtDisplay.Text += "Password: " + readPersonalData.GetValue(6) + Environment.NewLine;
        }

Upvotes: 1

Views: 109

Answers (1)

Davide Piras
Davide Piras

Reputation: 44605

You should check for null, so change the line:

readPersonalData.GetValue(0) + Environment.NewLine;

Into:

if(!readPersonalData.IsDBNull(0))
{
  ...readPersonalData.GetValue(0) + Environment.NewLine;
}

In this way you append text only when value in 0 is not null...

Upvotes: 2

Related Questions