Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Nullable object must have a value.

Reply
Thread Tools

Nullable object must have a value.

 
 
scparker
Guest
Posts: n/a
 
      01-16-2007
I have yet to find a satisfactory solution to this problem. It involves
VB.NET 2.0 and datetime issues.

I have a form that asks for a Date to be submitted in dd/mm/yyyy
format. When this is submitted it then is then dealt with as follows:

------------------------------------

Public Sub Update(ByVal sender As Object, ByVal e As System.EventArgs)
Handles ConfirmEdit.Click

'*** Declare
Dim RequestedDate As Nullable(Of Date)

'*** Populate
RequestedDate=ERequestedDate.Text

Result1 = DataTable.Update(Id, RequestedDate, ConfirmedDate)

End Sub

--------------------------------------

The DataTable.Update represents the datatable used and the method is
calling a stored procedure that updates a particular record based on an
id given to it.

Now, the above code is a simplification - but the principle si the
same. The ConfirmedDate may have nothing in it - and causes the
"Nullable object must have a value. " error.

The DataTable allows for DBNull.
---------------------------------------------
DataType=System.DateTime
AllowDBNull=True
DefaultValue=<DBNull>
---------------------------------------------

The Stored Procedure allows for Nulls:
---------------------------------------------
CREATE PROCEDURE [dbo].[Update]
(@Id int,
@RequestedDate datetime = null,
@ConfirmedDate datetime = null)
AS
BEGIN
~~~~~~~
END
GO
---------------------------------------------

The Database allows For Nulls:
---------------------------------------------
DataType=datetime (
Null=True
DefaultValue=Null
---------------------------------------------


But yet the update will not work when the ConfirmedDate value has
nothing in it. Does anybody have any ideas how to get round this?

 
Reply With Quote
 
 
 
 
Brian Williams
Guest
Posts: n/a
 
      01-16-2007
When you have an empty date have you tried to pass DBNull.Value to the
Stored Procededure instead of a null date?

Regards,
Brian K. Williams


"scparker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
>I have yet to find a satisfactory solution to this problem. It involves
> VB.NET 2.0 and datetime issues.
>
> I have a form that asks for a Date to be submitted in dd/mm/yyyy
> format. When this is submitted it then is then dealt with as follows:
>
> ------------------------------------
>
> Public Sub Update(ByVal sender As Object, ByVal e As System.EventArgs)
> Handles ConfirmEdit.Click
>
> '*** Declare
> Dim RequestedDate As Nullable(Of Date)
>
> '*** Populate
> RequestedDate=ERequestedDate.Text
>
> Result1 = DataTable.Update(Id, RequestedDate, ConfirmedDate)
>
> End Sub
>
> --------------------------------------
>
> The DataTable.Update represents the datatable used and the method is
> calling a stored procedure that updates a particular record based on an
> id given to it.
>
> Now, the above code is a simplification - but the principle si the
> same. The ConfirmedDate may have nothing in it - and causes the
> "Nullable object must have a value. " error.
>
> The DataTable allows for DBNull.
> ---------------------------------------------
> DataType=System.DateTime
> AllowDBNull=True
> DefaultValue=<DBNull>
> ---------------------------------------------
>
> The Stored Procedure allows for Nulls:
> ---------------------------------------------
> CREATE PROCEDURE [dbo].[Update]
> (@Id int,
> @RequestedDate datetime = null,
> @ConfirmedDate datetime = null)
> AS
> BEGIN
> ~~~~~~~
> END
> GO
> ---------------------------------------------
>
> The Database allows For Nulls:
> ---------------------------------------------
> DataType=datetime (
> Null=True
> DefaultValue=Null
> ---------------------------------------------
>
>
> But yet the update will not work when the ConfirmedDate value has
> nothing in it. Does anybody have any ideas how to get round this?
>



 
Reply With Quote
 
 
 
 
scparker
Guest
Posts: n/a
 
      01-17-2007

Brian Williams wrote:
> When you have an empty date have you tried to pass DBNull.Value to the
> Stored Procededure instead of a null date?
>
> Regards,
> Brian K. Williams
>
>


I have tried to see how I would be able to dictate the DBNull.Value in
my code but every attempt has failed so far. Given that I am using a
stored procedure, how would I be able to dictate the ConfirmedDate
value to be of DBNull.Value?

Sincerely,


Stephen

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      01-17-2007
"scparker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...

> Brian Williams wrote:
>> When you have an empty date have you tried to pass DBNull.Value to the
>> Stored Procededure instead of a null date?
>>
>> Regards,
>> Brian K. Williams
>>
>>

>
> I have tried to see how I would be able to dictate the DBNull.Value in
> my code but every attempt has failed so far. Given that I am using a
> stored procedure, how would I be able to dictate the ConfirmedDate
> value to be of DBNull.Value?


DateTime? dtmDate;
SqlParameter objSqlParameter;

objSqlParameter = new SqlParameter("@pdtmDate", SqlDbType.DateTime);
objSqlParameter.IsNullable = true;
objSqlParameter.Value = dtmDate ?? (object)DBNull.Value;


 
Reply With Quote
 
scparker
Guest
Posts: n/a
 
      01-17-2007
Mark Rae wrote:
> "scparker" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
>
> > Brian Williams wrote:
> >> When you have an empty date have you tried to pass DBNull.Value to the
> >> Stored Procededure instead of a null date?
> >>
> >> Regards,
> >> Brian K. Williams
> >>
> >>

> >
> > I have tried to see how I would be able to dictate the DBNull.Value in
> > my code but every attempt has failed so far. Given that I am using a
> > stored procedure, how would I be able to dictate the ConfirmedDate
> > value to be of DBNull.Value?

>
> DateTime? dtmDate;
> SqlParameter objSqlParameter;
>
> objSqlParameter = new SqlParameter("@pdtmDate", SqlDbType.DateTime);
> objSqlParameter.IsNullable = true;
> objSqlParameter.Value = dtmDate ?? (object)DBNull.Value;



I am sorry - that does not make sense to me at all. Could you consider
the code that I published to begin with and adapt your scripting that
you have done there to be applicable to what you have written? I have
tried putting your code in but Visual Studio 2005 does not like it at
all.

 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      01-17-2007
"scparker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...

> I am sorry - that does not make sense to me at all. Could you consider
> the code that I published to begin with and adapt your scripting that
> you have done there to be applicable to what you have written? I have
> tried putting your code in but Visual Studio 2005 does not like it at
> all.


Apologies - your OP had disappeared from my newsreader...

Try this:

If RequestedDate Is Null Then
RequestedDate=DbNull.Value
Else
RequestedDate=ERequestedDate.Text
End If

I haven't tested this, as I never use VB.NET...


 
Reply With Quote
 
Brian Williams
Guest
Posts: n/a
 
      01-17-2007
I don't know VB.Net verry well, but I don't see in your code example where
you add your sql parameters.

This is an example of how I do it in C#:
if(contactDTO.LockoutDateTime == ConstantsDB.NullDateTime)
cmd.AddInParameter(ConstantsDB.Contact.LockoutDate Time, DbType.DateTime,
DBNull.Value);
else
cmd.AddInParameter(ConstantsDB.Contact.LockoutDate Time, DbType.DateTime,
contactDTO.LockoutDateTime);

Regards,
Brian K. Williams


"scparker" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
>
> Brian Williams wrote:
>> When you have an empty date have you tried to pass DBNull.Value to the
>> Stored Procededure instead of a null date?
>>
>> Regards,
>> Brian K. Williams
>>
>>

>
> I have tried to see how I would be able to dictate the DBNull.Value in
> my code but every attempt has failed so far. Given that I am using a
> stored procedure, how would I be able to dictate the ConfirmedDate
> value to be of DBNull.Value?
>
> Sincerely,
>
>
> Stephen
>



 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      01-17-2007
"Mark Rae" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...

> Try this:
>
> If RequestedDate Is Null Then
> RequestedDate=DbNull.Value
> Else
> RequestedDate=ERequestedDate.Text
> End If
>


Actually, on reflection, I think this may be better...

If RequestedDate Is Null Then
Result1 = DataTable.Update(Id, DbNull.Value, ConfirmedDate)
Else
Result1 = DataTable.Update(Id, RequestedDate, ConfirmedDate)
End If




 
Reply With Quote
 
Brian Williams
Guest
Posts: n/a
 
      01-17-2007
I also don't do VB.Net but I think this will work, the only thing I think I
can add is...

If RequestedDate is a DateTime object then it will not be initialized as
null rather 1/1/0001 12:00:00 AM.
So you should test it like this:

If RequestedDate Is DateTime.MinValue Then

Regards
Brian K. Williams


"Mark Rae" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> "Mark Rae" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>
>> Try this:
>>
>> If RequestedDate Is Null Then
>> RequestedDate=DbNull.Value
>> Else
>> RequestedDate=ERequestedDate.Text
>> End If
>>

>
> Actually, on reflection, I think this may be better...
>
> If RequestedDate Is Null Then
> Result1 = DataTable.Update(Id, DbNull.Value, ConfirmedDate)
> Else
> Result1 = DataTable.Update(Id, RequestedDate, ConfirmedDate)
> End If
>
>
>
>



 
Reply With Quote
 
Mark Rae
Guest
Posts: n/a
 
      01-17-2007
"Brian Williams" <(E-Mail Removed)> wrote in message
news:%(E-Mail Removed)...

>I also don't do VB.Net but I think this will work, the only thing I think I
>can add is...
>
> If RequestedDate is a DateTime object then it will not be initialized as
> null rather 1/1/0001 12:00:00 AM.


Is that the case if the variable is declared like this:

Dim RequestedDate As Nullable(Of Date)


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Nullable object must have a value =?Utf-8?B?emlubw==?= ASP .Net 5 04-16-2007 03:42 AM
Nullable object types Nemisis ASP .Net 3 08-21-2006 12:58 PM
Property 'Keycode' is non-nullable Jacques Wentworth ASP .Net 0 03-11-2005 11:08 AM
DateTime not nullable problem Kevin Yu ASP .Net 5 01-29-2005 10:38 AM
nullable sqlparameter buran ASP .Net 0 07-17-2003 08:21 AM



Advertisments