473,672 Members | 2,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(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.Nul l; //(also tried =
System.Convert. DBNull;

if (tbxStartDate.T ext!="")
{
dateStartDate = Convert.ToDateT ime(tbxStartDat e.Text);
}
/*Also tried the following with no success.
else
{
dateStartDate = DBNull.Value;
}*/
//Set SQL parameters and populate with form values
sqlCommand1.Par ameters.Add("@S tartDate", SqlDbType.Small DateTime).Value =
dateStartDate;

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

"Matt Jensen" <re************ ***@microsoft.c om> wrote in message
news:Oa******** ******@TK2MSFTN GP14.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.Nul l; //(also tried =
System.Convert. DBNull;

if (tbxStartDate.T ext!="")
{
dateStartDate = Convert.ToDateT ime(tbxStartDat e.Text);
}
/*Also tried the following with no success.
else
{
dateStartDate = DBNull.Value;
}*/
//Set SQL parameters and populate with form values
sqlCommand1.Par ameters.Add("@S tartDate", SqlDbType.Small DateTime).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
7297
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
17170
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 part. i’m using Vb.net datagrid as a front end. any assistance appreciated!! :?: --
7
40026
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 constructor. Those fields are modified during various methods in the class. One method updates the database with the values of the class fields. I have everything working fine for string fields but am running into problems with integers and null values.
3
5350
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 come it doesn't return 0 instead? because I am using "sum", if SQL can't find any record.. isn't it suppose to return 0? In my table setup, the default value for the "size" field is 0 and doesn't allow NULL.
2
1584
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 than the SQL Null (type: "System.DBNull"). So, i need some way of cenverting the values into a "System.DBNull" object and then send them to the database. Any ideas? BTW: The parameter called: "ConvertEmptyStringToNull" does not work. Microsoft...
0
1181
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 sql and find this : @DateOfBirth=''1951-10-17 00:00:00:000'' this is failing because of the double single quotes and works ok in profiler
1
2630
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 passed to the stored procedure VARBINARY(MAX)) and then finally back to a valid DATETIME in Sql Server. It seems I have tried everything, but I always get an error when trying to convert the hex value representation of the byte array to a valid...
8
1459
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 DBNull.Value.Equals(dRow("path")) Then advCell.Text = dRow("path") Else advCell.Text = "No Path"
2
2221
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 mathematical operation allowed on NULL? NULL as I know it in programming is an undefined value. In math, when dividing by 0 you get an undefined value. Thus NULL = x/0 ?
0
8932
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8832
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8611
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8685
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6240
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4230
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4421
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2067
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1819
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.