Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Guys,

In Sql Server table, i have dates separated by commas.

Eg: "2015-09-01,2015-09-03,2015-09-06,2015-09-10"

I want to use this same string in my javascript code, but when put this
string in alert, i'm getting -ve values, but not this same string.

So, I want to replace , with "," in c#.

What I have tried:

C#
public object SessionScheduleDates
{
  get { return Session != null ? Session.ScheduleDates : string.Empty; }
}

Javascript
<script>
   var ScheduleDates;
    ScheduleDates = (<%= SessionScheduleDates %>);
    alert(ScheduleDates);
</script>

Result:
-34268

Actually looking for:
"2015-09-01,2015-09-03,2015-09-06,2015-09-10"


Can any one please help me?


Thanks
Posted
Updated 6-Sep-16 21:32pm
v2

1 solution

Try:
C#
string input = "2015-09-01,2015-09-03,2015-09-06,2015-09-10";
string output = input.Replace(",", "\",\"");
Except, you probably really want to do this:
C#
string input = "2015-09-01,2015-09-03,2015-09-06,2015-09-10";
string output = "\"" + input.Replace(",", "\",\"") + "\"";
In order to balance up the double quotes:
"2015-09-01","2015-09-03","2015-09-06","2015-09-10"
 
Share this answer
 
Comments
abdul subhan mohammed 7-Sep-16 3:49am    
i tried your code, but i got only last date, "2015-09-10".

public object SessionScheduleDates
{
get
{
string output="";
if (Session != null)
{
string input = Session.ScheduleDates;
output = "\"" + input.Replace(",", "\",\"") + "\"";
}
return output;
}
}
OriginalGriff 7-Sep-16 4:01am    
Check your inputs: when I try it with the data you supplied, I get exactly the string I showed (I copy'n'pasted it from the debugger).
So look at what Session.ScheduleDates returns inside that method, and look at exactly what output gets when it returns.
abdul subhan mohammed 7-Sep-16 4:23am    
Edited:

public object SessionScheduleDates
{
get
{
string output="";
if (Session != null)
{
string input = Session.ScheduleDates;
output = "[" + "\"" + input.Replace(",", "\",\"") + "\"" + "]";
}
return output;
}
}

Javascript
<script>
var ScheduleDates = new Array();
ScheduleDates = (<%= SessionScheduleDates %>);
alert(ScheduleDates);
</script>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900