Solved

Conditional ? problems

Posted on 2003-12-08
5
272 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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

726 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