General Grey
General Grey

Reputation: 3688

inserting a & into a label

EDIT#2

I changed the Title name because it no longer represented the question I am asking. It turns out my problem was displaying the & symbol in a c# winforms label.

Enter "&" symbol into a text Label in Windows Forms?

That is the answer to the question which turns out is a duplicate

This was the original

I am currently using c# and sql. I have no problem writing to the database, when I check the field using MS SQL Server Manager, I can see the & symbol in the varchar field. but when I retrieve it using c# it seems to be omitted.

myTable.myColumn =" Me & You";

string testString="";
SQLConnection con = new SQLConnection(...)
SQLCommand command=new SQLCommand("Select Top 1 * from myTable",con)
SQLDataReader reader= command.ExecuteQuery()
while (reader.read())
{
    testString=reader["myColumn"].ToString();
    MessageBox.Show(testString);
}

output = Me You

I wrote this on the fly, it likely is not syntactically correct. Is this normal output or do I likely have a mistake somewhere?

EDIT #1 so it turns out that the MessageBox does display the &. The object this loads into holds the & symbol as well. Even the label that is displaying the symbol holds the & symbol in its .Text property eg. myLabel.Text == "Me & You". but the display to the screen still says Me you. so the Problem has nothing to do with SQL, but simply to do with Winforms, I will Likely delete this question shortly, unless someone else has experienced this and can offer some insight to speed my troubleshooting

Upvotes: 0

Views: 199

Answers (3)

Ian Boyd
Ian Boyd

Reputation: 256841

My guess is that the & is being used as a special character when you use a WinForms Label.

Where:

Me & You

becomes

enter image description here

And

Me && You

becomes

enter image description here

But that doesn't apply to a MessageBox.Show


If it's a label:

  • Escape it with another ampersand (&&)

  • Set UseMnemonic for that label to false, then all ampersands for that label will be taken literally and you don't need to double them (you lose the underlining/access key features though)

Upvotes: 2

Vikram Jain
Vikram Jain

Reputation: 5588

myTable.myColumn =" Me & You";

string testString="";
SQLConnection con = new SQLConnection(...)
SQLCommand command=new SQLCommand("Select Top 1 * from myTable",con)
SQLDataReader reader= command.ExecuteQuery()
while (reader.read())
{
    testString=reader["myColumn"].ToString().Replace("&","&&");
    MessageBox.Show(testString);
}

Upvotes: 1

Aghilas Yakoub
Aghilas Yakoub

Reputation: 28990

To use characters that have special meaning to the operating system, such as "and" ampersand (&) or semicolon (;), enclose the character in quotation marks ("). Separator columns can be n 'any 8-bit character.

Upvotes: 0

Related Questions