0

I am trying to form a text representation of a table based off of the result coming in from a SqlDataReader.

while (unitsRdr.Read())
{
    note.AppendFormat("{0,-15}", String.Format("{0}({1})", unitsRdr.GetValue(0), unitsRdr.GetValue(1)));
}

Now what I expect to happen is I should get 4 sets of items, that will have padding on the right hand side equal to 15. Like this

BP(mm\Hg)      HR(bpm)        RR(rpm)        Sa O2(%)       |<-- this would be the end of the string.

However what I am getting is

BP(mm\Hg          )HR(bpm            )RR(rpm            )Sa O2(%              )|<-- this is the end of the string.

It appears to start counting after the ( and the ) is put after the number of spaces.

What is the correct way of doing formatting so the text is like my first example?


For anyone who wants it here is the source table

desc         unit
------------ ----------
BP           mm\Hg     
HR           bpm       
RR           rpm       
Sa O2        %         

1 Answer 1

2

I strongly suspect that the problem is that the value returned by unitsRdr.GetValue(1) is actually longer than you believe. I very much doubt that string.Format is doing that. Try using unitsRdr.GetValue(1).Trim() instead - or just test it with a hard-coded literal value.

Sign up to request clarification or add additional context in comments.

4 Comments

I should delete this question out of embarrassment, I will accept after the 15.
@ScottChamberlain: I definitely wouldn't delete it - instead, I'd edit it to explain what exactly it turned out to be. Was it a fixed-width field in the database? Bad data? Something else? Save it for the next person in the same situation...
The column was char(15) instead of varchar(15) like I was expecting.
@ScottChamberlain: Right - that makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.