473,810 Members | 2,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Sending Null Value to Sql Server

I need to be able to send a null value into a Sql Server datetime field. The
follolwing code is a snippet of what I am using. I am parsing a flat file
from the main frame and inserting it into a Sql DB. The "departDate can be a
null value in Sql Server. I cannot figure out how to assign a null value to
a SqlDateTime variable and use that variable as part of a SqlParameter, The
error I keep getting is: SqlDateTime overflow. Must be between 1/1/1753
12:00:00 AM and 12/31/9999 11:59:59 PM. Again, I need for the value to be
null.
SqlDateTime departDate;
string departTime = string.Empty;

string depart = input.Substring (137,8);
string departString = depart.Trim();

if(departString == "")
{
departDate = I WANT TO ASSIGN A NULL VALUE HERE
departTime = string.Empty;
}
else
{
departDate = Convert.ToDateT ime(GetDate(inp ut.Substring(13 7,8)));
departTime = input.Substring (145,4);
}

SqlConnection connection = new SqlConnection() ;

ConnectionMgmt cn = new ConnectionMgmt( );
string connectionStrin g =ConfigurationS ettings.AppSett ings["Connection
String"];
connection = cn.GetConnectio n();
object oRes = new object();
oRes = SqlHelper.Execu teScalar(connec tionString,
CommandType.Sto redProcedure, "insertDailyBat ch_sp",
new SqlParameter("@ depart_date", departDate),
new SqlParameter("@ depart_time", departTime),
int nRes = Convert.ToInt32 (oRes);
return nRes;
--
Robert Hill

Nov 17 '05 #1
3 16608
You need to set the parameter's value to DBNull.Value.

"Robert" <rh******@hotma il.com> wrote in message
news:3C******** *************** ***********@mic rosoft.com...
I need to be able to send a null value into a Sql Server datetime field.
The
follolwing code is a snippet of what I am using. I am parsing a flat file
from the main frame and inserting it into a Sql DB. The "departDate can
be a
null value in Sql Server. I cannot figure out how to assign a null value
to
a SqlDateTime variable and use that variable as part of a SqlParameter,
The
error I keep getting is: SqlDateTime overflow. Must be between 1/1/1753
12:00:00 AM and 12/31/9999 11:59:59 PM. Again, I need for the value to be
null.
SqlDateTime departDate;
string departTime = string.Empty;

string depart = input.Substring (137,8);
string departString = depart.Trim();

if(departString == "")
{
departDate = I WANT TO ASSIGN A NULL VALUE HERE
departTime = string.Empty;
}
else
{
departDate = Convert.ToDateT ime(GetDate(inp ut.Substring(13 7,8)));
departTime = input.Substring (145,4);
}

SqlConnection connection = new SqlConnection() ;

ConnectionMgmt cn = new ConnectionMgmt( );
string connectionStrin g =ConfigurationS ettings.AppSett ings["Connection
String"];
connection = cn.GetConnectio n();
object oRes = new object();
oRes = SqlHelper.Execu teScalar(connec tionString,
CommandType.Sto redProcedure, "insertDailyBat ch_sp",
new SqlParameter("@ depart_date", departDate),
new SqlParameter("@ depart_time", departTime),
int nRes = Convert.ToInt32 (oRes);
return nRes;
--
Robert Hill

Nov 17 '05 #2
You need to assign the value "System.Convert .DBNull" to your SqlParameter.
Nov 17 '05 #3
Thank you. You are exactly right. The issue I am having is that I am in a
loop and I need to assign DBNull.Value to a variable prior to using it in the
SqlParameter, such as:

SomeDataType foo = DBNull.Value;

then

new SqlParameter("@ depart_date", foo),


--
Robert Hill

"Marina" wrote:
You need to set the parameter's value to DBNull.Value.

"Robert" <rh******@hotma il.com> wrote in message
news:3C******** *************** ***********@mic rosoft.com...
I need to be able to send a null value into a Sql Server datetime field.
The
follolwing code is a snippet of what I am using. I am parsing a flat file
from the main frame and inserting it into a Sql DB. The "departDate can
be a
null value in Sql Server. I cannot figure out how to assign a null value
to
a SqlDateTime variable and use that variable as part of a SqlParameter,
The
error I keep getting is: SqlDateTime overflow. Must be between 1/1/1753
12:00:00 AM and 12/31/9999 11:59:59 PM. Again, I need for the value to be
null.
SqlDateTime departDate;
string departTime = string.Empty;

string depart = input.Substring (137,8);
string departString = depart.Trim();

if(departString == "")
{
departDate = I WANT TO ASSIGN A NULL VALUE HERE
departTime = string.Empty;
}
else
{
departDate = Convert.ToDateT ime(GetDate(inp ut.Substring(13 7,8)));
departTime = input.Substring (145,4);
}

SqlConnection connection = new SqlConnection() ;

ConnectionMgmt cn = new ConnectionMgmt( );
string connectionStrin g =ConfigurationS ettings.AppSett ings["Connection
String"];
connection = cn.GetConnectio n();
object oRes = new object();
oRes = SqlHelper.Execu teScalar(connec tionString,
CommandType.Sto redProcedure, "insertDailyBat ch_sp",
new SqlParameter("@ depart_date", departDate),
new SqlParameter("@ depart_time", departTime),
int nRes = Convert.ToInt32 (oRes);
return nRes;
--
Robert Hill


Nov 17 '05 #4

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

Similar topics

3
27760
by: John Morgan | last post by:
In an SQL statement which concatenates several fields I get a null value returned if any one of the fields are null. Is this to be expected? For example : SELECT tblMember.memberAddress + ' ' + tblMember.memberTown + ' ' + tblMember.memberCounty + ' ' + tblMember.memberPostCode + '<br> ' + tblMember.memberCountry + '<br> ' + tblMember.memberInstitution AS
2
5354
by: Dknight | last post by:
Hi, all! I have a problem, how can I check a form data before sending it to server. I have the example below, but first data is sent to server, but after JavaScript checks the data :( I need that JavaSctipt check data and after send it to the server, how can do that? Example: ....
3
7102
by: Robb Gilmore | last post by:
Hello, We have a C#.NET app which is calling a Java webservice. We use the wsdl file exportted from the java webservice to create our web-reference in Visual Studio. We are able to create the parameter classes and call the webservice just fine. Our problem is, within our .Net app, we have some value objects ( like floats, for instance ) which are meant to be null. Since there is no null float, we use float.minvalue to indicate a...
0
1712
by: Benny Raymond | last post by:
reply to: benny@pocketrocks.com if possible: I'm trying to set up a hierarchy system in this database where each row can be related to a previous row. The problem is that when I go to addTreeRow, it doesn't allow me to use Convert.DBNull for the childof column, even though I have nilable set to true. (All parent nodes have to be set to null, since they are not the child of any other row... especially the very first row, which will have...
26
30939
by: Martin R | last post by:
Hi, How to find first not null value in column whitout chacking whole table (if there is a not null value then show me it and stop searching, the table is quite big)? thx, Martin *** Sent via Developersdex http://www.developersdex.com ***
4
2631
by: Learner | last post by:
Hello, I have database field called 'PullAHead' defined as a bit field. Now if user doesn't pick a 'Yes' or 'No' in the front I need to be able to send a null value into the 'PullAHead' field in the database. This is how I am trying to do it.... Dim PullAhead As Integer PullAhead = Nothing If _lblpullahead.Visible = True Then
2
24773
by: BF | last post by:
Hi, I have some tables where I import data in, lots of field have gotten a NULL value which the application can not handle. Now can I replace each NULL value with '' in a columns with: update <tableset = '' where IS NULL But because there are lots of columns this is pretty much work, also there are multiple tables.
0
1190
by: speranza | last post by:
cant pass null value to sp via linq this is my sp code IF @Param1 IS NOT NULL BEGIN SET @Param1=@Param1 END ELSE BEGIN SET @Param1=' IS NULL'
4
2915
by: justice750 | last post by:
Hi All, I am using a FormView control. The allows me to update records in the database. However, when a database field is null I can not update the field on the form. It works fine when the field is not a null value. I am not using any code behind (C#) to bind the data or manipulate the data. I have read that when there is a null value in the database that there is no record in the "dataset". Can anyone show me how to bind a value in the...
0
9722
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10643
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
10121
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...
0
9200
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7664
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
6881
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4333
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.