Solved

Conditional ? problems

Posted on 2003-12-08
5
266 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
Comment Utility
nevermind :D. ill just use the parse method in SqlDateTime.
0
 
LVL 20

Expert Comment

by:TheAvenger
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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.
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now