-1

As Example,

string str= string.Format(@"<p style=""font-family:Times New Roman, serif;font-size: 12pt;"" >Total Qty {qty} on Date {0} </p>",dr["Date"]);

I want to replace qty later on.It throws exception "Input string was not in a correct format".

Can anyone tell the reason and possible solution?

Amit Bhatt
  • 45
  • 10

3 Answers3

3

I think you should use like this :

string str= string.Format(@"Total Qty {{qty}} on Date {0}","01/01/2017");

to get the output as Total Qty {qty} on Date 01/01/2017. you have not give any notes on the qty, If it is a variable and you wanted to display its value in between { and } then you should use $ instead for @ and this time the code will be like this:

int qty = 10;
string str = string.Format($"Total Qty : {qty} on Date : {{0}}","01/01/2017");

This time you will get the output as Total Qty : 10 on Date : 01/01/2017

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
2

I think you should use $ instead of @ because. In String Interpolation , we simply prefix the string with a $ (much like we use the @ for verbatim strings). Then, we simply surround the expressions we want to interpolate with curly braces (i.e. { and })

int qty = 10;
string str = string.Format($"Total Qty {qty} on Date {{0}}","01/01/2017");
Console.WriteLine(str);

The output is

Total Qty 10 on Date 01/01/2017

The Working fiddle can be seen

Community
  • 1
  • 1
Mohit S
  • 13,723
  • 6
  • 34
  • 69
1

You need to use this format:

$"Total Qty {qty} on Date {{0}}","01/01/2017"

Here is a working example: https://dotnetfiddle.net/UJjJyS

sudheeshix
  • 1,541
  • 2
  • 17
  • 28
  • assuming you dont want the formatted string to be `"Total Qty {qty} on Date 01/01/2017"` but want it as `"Total Qty 10 on Date 01/01/2017"` where qty has a value of 10. – sudheeshix Jan 20 '17 at 05:18