Prince Antony G
Prince Antony G

Reputation: 932

Unterminated string constant error in asp.net

enter image description here

Got this error while call the function

 static public void DisplayAJAXMessage(Control page, string msg)
{
    string myScript = String.Format("alert('{0}');", msg);

    ScriptManager.RegisterStartupScript(page, page.GetType(), "MyScript", myScript, true);
}

Calling this function:

    string sampledata = "Name                  :zzzzzzzzzzzzzzzz<br>Phone         :00000000000000<br>Country               :India";
        string sample = sampledata.Replace("<br>", "\n");


    MsgBox.DisplayAJAXMessage(this, sample);

I need to display Name,Phone and Country in next line.

Upvotes: 1

Views: 15643

Answers (4)

Roger Medeiros
Roger Medeiros

Reputation: 813

Your Unterminated is not in C# is in Javascript generated code.

Upvotes: 0

Simon Whitehead
Simon Whitehead

Reputation: 65079

Unterminated string constant means you've forgotten to close your string. You can't have an alert that runs over multiple lines. When the script is outputting to the browser, it's actually including the new lines.. not the "\n" like the javascript expects. That means, your alert call is going over multiple lines.. like this:

alert('Name                  :zzzzzzzzzzzzzzzz
       Phone         :00000000000000
       Country               :India');

..which won't work, and will produce the error you're seeing. Try using double backslash to escape the backslash:

string sample = sampledata.Replace("<br>", "\\n");

Upvotes: 9

Marc Gravell
Marc Gravell

Reputation: 1062745

"\n" is a newline for C#, i.e. your js contains:

something('...blah foo
bar ...');

what you actually want is a newline in js:

something('...blah foo\nbar ...');

which you can do with:

string sample = sampledata.Replace("<br>", "\\n");

or:

string sample = sampledata.Replace("<br>", @"\n");

Upvotes: 3

rick schott
rick schott

Reputation: 20617

You need to escape/encode your string being consumed by JavaScript:

Escape Quote in C# for javascript consumption

Upvotes: 1

Related Questions