473,378 Members | 1,429 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

(Sql)DateTime and Null value

Howdy all
I know this is a common question as I've searched a lot of this NG on the
topic, however I haven't been able to find an answer.
All I want to do is pass a DateTime value to a SQL Server SP DateTime
parameter, and either pass a web form text field value to it if there is one
or else null, I don't want to start another discussion about structs /
datetimes being able to accept nulls, I just would really like it to
work...!

So here is a snippet of what I've tried:

SqlDateTime dateStartDate = SqlDateTime.Null; //(also tried =
System.Convert.DBNull;

if (tbxStartDate.Text!="")
{
dateStartDate = Convert.ToDateTime(tbxStartDate.Text);
}
/*Also tried the following with no success.
else
{
dateStartDate = DBNull.Value;
}*/
//Set SQL parameters and populate with form values
sqlCommand1.Parameters.Add("@StartDate", SqlDbType.SmallDateTime).Value =
dateStartDate;

If you could give me the answer/solution I'd be most happy.
Thanks
Cheers
Matt
Nov 17 '05 #1
1 4940
Got it here thanks
http://www.c-sharpcorner.com/Code/20...orDateTime.asp
Cheers
Matt

"Matt Jensen" <re***************@microsoft.com> wrote in message
news:Oa**************@TK2MSFTNGP14.phx.gbl...
Howdy all
I know this is a common question as I've searched a lot of this NG on the
topic, however I haven't been able to find an answer.
All I want to do is pass a DateTime value to a SQL Server SP DateTime
parameter, and either pass a web form text field value to it if there is
one or else null, I don't want to start another discussion about structs /
datetimes being able to accept nulls, I just would really like it to
work...!

So here is a snippet of what I've tried:

SqlDateTime dateStartDate = SqlDateTime.Null; //(also tried =
System.Convert.DBNull;

if (tbxStartDate.Text!="")
{
dateStartDate = Convert.ToDateTime(tbxStartDate.Text);
}
/*Also tried the following with no success.
else
{
dateStartDate = DBNull.Value;
}*/
//Set SQL parameters and populate with form values
sqlCommand1.Parameters.Add("@StartDate", SqlDbType.SmallDateTime).Value
= dateStartDate;

If you could give me the answer/solution I'd be most happy.
Thanks
Cheers
Matt

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dishan Fernando | last post by:
Hi my prob is like this.. DECLARE @id int SELECT * FROM Table1 WHERE ID = @ID if @ID is null , I want to select all the records, that ID is null
1
by: v0lcan0 | last post by:
Any help on extracting the time part from the datetime field in SQL database. even though i had entered only the time part in the database when i extract the field it gives me only the date...
7
by: Dan | last post by:
I'm fairly new to C# and I am having some problems understanding int variables and null values. I have created a class that populates the values of it's fields from a DataReader in it's...
3
by: Tommi Bauer | last post by:
I am writing a query in ASP.NET C#, select sum(size) as total_size from my_table where is_valid = 1 However, if there is no data in the database, it returns 1 record with value null. How...
2
by: Kivak Wolf | last post by:
Hey, I have everything working now except for this. I have some fields that I am getting data from, then I am sending them to a SQL database. Now the empty string and VB.NET null are different...
0
by: AlanJSmith | last post by:
I have a a strongly typed dataset created by VS2005 and am referencing it from an objectDataSource. i am using the the objectDataSource in a FormView control. The update fails so I trace the...
1
by: Karch | last post by:
I am doing some experimenting with serialization (for use with Service Broker) and I am having a problem converting from a .NET DateTime (in the client application) to a SqlDbType.VarBinary (as...
8
by: Aussie Rules | last post by:
Hi, I have a result set that sometimes will contain a Null value from the SQL server. I have looked into this and am trying to manage this with the following If Not...
2
by: cloud255 | last post by:
Hey, I was playing around with SQL (SQL Server 2008) toady and noticed something I think is really strange: select 1/null -- returns null Why is this operation allowed? Why is any...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.