?
Solved

Session Variable to SQL Parameter

Posted on 2008-11-12
5
Medium Priority
?
329 Views
Last Modified: 2012-05-05
Hello,
In ASP.NET 2.0
I have a date value saved as a Session variable
ie:
Dim dtToday As DateTime = DateTime.Today
Session("dtCurr") = dtToday.ToString

Then trying to get that value into a Select Statement
ie:
SelectCommand = "SELECT [EventTitle], [EventStartDate], [EventStartTime], [EventEndTime] From [Events] Where ([EventStartDate]) = CONVERT(datetime,@EventStartDate)"

<SelectParameters>
<asp:Parameter DefaultValue="Session(&quot;dtCurr;&quot;)" Name="EventStartDate" Type="String" />
</SelectParameters>
...
Getting a syntax error: converting datetime from character string. Have tried a number of permutations, but to no avail.
0
Comment
Question by:el_scotty
  • 2
  • 2
5 Comments
 
LVL 6

Expert Comment

by:matthewrhoades
ID: 22942504
Is there a reason you used the type "String" as opposed to type "DateTime".  It might help you to cut the two conversions out of the picture (1 conversion being storing a date as a string, 2 being the conversion back to DateTime)
0
 
LVL 1

Author Comment

by:el_scotty
ID: 22942616
Well that was left from one permutation - I just changed it back to:
Session("dtCurr") = dtToday

Select Statement to: "Where ([EventStartDate]) = @EventStartDate"

and

Parameter value to: Type='DateTime'

But Still getting System.FormatException:
String Not recognized as a Valid dateTime. unknown word at index 0

0
 
LVL 6

Expert Comment

by:matthewrhoades
ID: 22942650
If it is set up with Try ... Catch... End Try...  you should catch the exception and message box the value of the variable so you can see the format of the date string.  Either way it would be best to see what that variable looks like when it throws the error, it may be simple when you see it.  IF you can get the value that @EventStartDate throws, post it.  You may need to reorder it (I think there some native date formatting functions you can use)
0
 
LVL 37

Accepted Solution

by:
samtran0331 earned 2000 total points
ID: 22942732
Your code here:
<SelectParameters>
<asp:Parameter DefaultValue="Session(&quot;dtCurr;&quot;)" Name="EventStartDate" Type="String" />
</SelectParameters>

The value is literally the text: Session("dtCurr")
(Not the value of the session, but that actual text as above)...

Try using a session parameter:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sessionparameter.aspx
(scroll down to see the example)
0
 
LVL 1

Author Closing Comment

by:el_scotty
ID: 31516046
Thank You.
SessionParameter does the trick.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question