1
Answer

Formating the string before displaying in ASP.NET

eliza sahoo

eliza sahoo

14y
2.1k
1
In Some cases while displaying a large number it will be nice if we can format the number to a more readable format
Like : Reputation Point : 537456
Can be more readable if we can write it as Reputation Point : 537,456
ASP.NET provide features like String.Format(format,arg0) to format arguments into different forms.
For above solution you can implement

Response.Write(String.Format("{0:#,###}", 123456789));
Which will print 123,456,789

{0:#,###} ? Known as the format string where "{ ,}"are compulsory to mentation.
The first part before ':' represent the argument number & it will be an integer.
The second part after ':' represent the format that you want your argument to be converted.
Answers (1)
3
Nainil
NA 660 0 15y

.NET:   
   string
userString = textBox1.Text;
   string replacedString = userString.Replace("'", "''");
 
SQL:
   replacedString = Replace(userString,"'","''")
 
Hope this helps.
Accepted
1
Nainil
NA 660 0 15y

Hi Giorgio,
    Whenever you have single quotes, replace them with 2 single quotes. That should work.
0
Gustavo
NA 1.8k 386.6k 15y

Nainil:
 
Yes, thank you.
0
Gustavo
NA 1.8k 386.6k 15y

Nainil:
 
Can you give me a sample code of the 2 types?
 
0
Nainil
NA 660 0 15y

Giorgio,
   You can either use .NET string.Replace() method to replace any single quotes with 2 single qoutes or you can use T-SQL's Replace function. Since the value being inserted is passed in as a SqlParameter.Value property, you can run the Replace function through it and ensure that any string being passed to the stored procedure is clean. Hope this helps.
0
Gustavo
NA 1.8k 386.6k 15y

Nainil:
 
Yes, if it was a string. But it will be some text that the user entered. Is there a way to do it automatically when I put it into the sql command to save it?