Solved

Conditional ? problems

Posted on 2003-12-08
5
273 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…

717 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