Solved

Conditional ? problems

Posted on 2003-12-08
5
271 Views
Last Modified: 2010-04-16
I get an error compiling the follow code saying that theres no implicit conversion between string and sqldatetime, is there away around this without using ifs, i hate having 30000lines for a single function. birthday is obiously a string. thanks

cmd.Parameters["@Birthday"].Value= Birthday !="" ? Birthday : SqlDateTime.Null;
0
Comment
Question by:razziel
  • 3
  • 2
5 Comments
 

Author Comment

by:razziel
ID: 9897934
nevermind :D. ill just use the parse method in SqlDateTime.
0
 
LVL 20

Expert Comment

by:TheAvenger
ID: 9899930
You have to use it (check also ParseExact method) because the parameter knows it is of type DateTime and does not allow string values directly. Be careful with date formats, Parse is very powerful and also a dangerous function.
0
 

Author Comment

by:razziel
ID: 9901039
well the parameter does take strings even tho its a SqlDateTime, but it raises an exception if its not in the right format. I guess both the true result and false results have to be of the same type, i hadnt tested tho. i guess ill have to validate the text just after the user types it on the textbox. if anyone can give me a good sample of how to do this, like those nice validators on webforms, ill give the points. thanks
0
 
LVL 20

Accepted Solution

by:
TheAvenger earned 125 total points
ID: 9902471
Why don't you use directly a DateTimePicker? It has a checkbox to disable it (the same as having an empty string in your case) and can be set to show the date in a lot of different formats and gives you directly the value in an object ot type DateTime. So you would write a code like:

cmd.Parameters["@Birthday"].Value= BirthdayDateTimePicker.Checked ? BirthdayDateTimePicker.Value : SqlDateTime.Null;

You only have to enable the checkbox, i.e. put the DatePicker's ShowCheckBox property to true.
0
 

Author Comment

by:razziel
ID: 9904810
good idea, thanks. i dont know why was i even bothering using a textfield when theres such a nice control. =)

theres your points
0

Featured Post

ScreenConnect 6.0 Free Trial

At ScreenConnect, partner feedback doesn't fall on deaf ears. We collected partner suggestions off of their virtual wish list and transformed them into one game-changing release: ScreenConnect 6.0. Explore all of the extras and enhancements for yourself!

Question has a verified solution.

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

Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

809 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