0

I have the following C# code which populates a dropdownlist:

public void PopulateInsurance()
{
    SqlCommand cmd = new SqlCommand("GetInfo", new SqlConnection(ConfigurationManager.AppSettings["connectionstr"]));
    cmd.Connection.Open();

    SqlDataReader ddles = default(SqlDataReader);
    ddles = cmd.ExecuteReader();

    ddlI.Items.Clear();
    ddlI.DataSource = ddles;
    ddlI.DataValueField = "id";
    ddlI.DataTextField = "title";
    ddlI.DataBind();
    //set the default value for the drop down
    ListItem Item = new ListItem();
    Item.Text = "Any";
    Item.Value = "0";
    ddlInsurance.Items.Insert(0, Item);

    cmd.Connection.Close();
    cmd.Connection.Dispose();
}

One of the row in the SQL table has the following values:

id     title
9      Worker'

The dropdownlist shows the title as displayed above (' -> which a single apostrophe')

How can I modify the code so it takes the ' or any other special character and unescapes it to show the character, in this example '.

SearchForKnowledge
  • 3,613
  • 8
  • 45
  • 115
  • 4
    You shouldn't have those characters in your database at all. Your escaping is broken elsewhere. – SLaks Jan 12 '15 at 19:17
  • See [Decoding all HTML Entities](http://stackoverflow.com/questions/8348879/decoding-all-html-entities) and figure out how you can get that to work with the SqlDataReader. – CodeCaster Jan 12 '15 at 19:19
  • 1
    We had a similar issue (someone's last name had an apostrophe). When we stored the data in the database, we escaped it then with a single quote. ex: O''Connor. – Jenn Jan 12 '15 at 20:49

1 Answers1

1

The system.Uri class contains a UnescapeDataString method. Will that solve your problem?

http://msdn.microsoft.com/en-us/library/system.uri.unescapedatastring(v=vs.110).aspx

Spock
  • 4,330
  • 2
  • 14
  • 20