|
 |
ado_dotnet thread: Retrieving null values from fields using C#/ADO.NET
Message #1 by "Zackery Breen" <zackery_breen@h...> on Fri, 29 Mar 2002 02:55:53
|
|
I am trying to retrieve a value from a field that can contain a null value and
store it into an integer field using the following C#/ADO.NET code:
int Temperature = (int) paramTemperature.Value;
(paramTemperature is an output parameter from a stored procedure)
But I always get an exception if the value is null. I would like to store the
value into the variable whether it is null or not and then be able to test for
null at a later point in time.
How can I do this?
Thanks
Message #2 by "Aaron Weiker" <aaron@w...> on Thu, 28 Mar 2002 21:52:08 -0500
|
|
What you want to do is check if the value is null before you pull it out.
Use the function IsDbNull to check the value before you retrieve it. Here is
an example of VB.NET code which will port directly to C#
If Not (IsDBNull(SqlCmd.Parameters("@Login").Value)) Then
Me.Login = CStr(SqlCmd.Parameters("@Login").Value)
End If
-Aaron Weiker
----- Original Message -----
From: "Zackery Breen" <zackery_breen@h...>
To: "ADO.NET" <ado_dotnet@p...>
Sent: Friday, March 29, 2002 2:55 AM
Subject: [ado_dotnet] Retrieving null values from fields using C#/ADO.NET
> I am trying to retrieve a value from a field that can contain a null value
and
> store it into an integer field using the following C#/ADO.NET code:
>
> int Temperature = (int) paramTemperature.Value;
>
> (paramTemperature is an output parameter from a stored procedure)
>
> But I always get an exception if the value is null. I would like to store
the
> value into the variable whether it is null or not and then be able to test
for
> null at a later point in time.
>
> How can I do this?
>
> Thanks
>
Message #3 by "Zackery Breen" <zackery_breen@h...> on Fri, 29 Mar 2002 07:49:50
|
|
Thanks,
But what I am trying to do is call the stored procedure, store the returned
values into a struct and then pass the struct off to a different part of the
application for use. I cannot check for null before retrieving the value
because I still wouldn't know if the result where null or not at the point I
intend to use it.
I noticed that the classes in System.Data.SqlClient have a property that
indicates whether the value is null or not. Any idea how i can use these with a
stored procedure that returns output parameters?
Thanks again Aaron
> What you want to do is check if the value is null before you pull it out.
Use the function IsDbNull to check the value before you retrieve it. Here is
an example of VB.NET code which will port directly to C#
If Not (IsDBNull(SqlCmd.Parameters("@Login").Value)) Then
Me.Login = CStr(SqlCmd.Parameters("@Login").Value)
End If
-Aaron Weiker
Message #4 by "Zackery Breen" <zackery_breen@h...> on Fri, 29 Mar 2002 07:54:53
|
|
I meant System.Data.SqlTypes not SqlClient :)
Message #5 by "Aaron Weiker" <aaron@w...> on Fri, 29 Mar 2002 07:22:51 -0500
|
|
I don't think that it would change much if you are putting them in a
structure.
Instead of not setting a value if it is DbNull you can simply set it to Null
in which case C# will be able to understand.
If Not (IsDBNull(SqlCmd.Parameters("@Login").Value)) Then
Me.Login = CStr(SqlCmd.Parameters("@Login").Value)
Else
Me.Login = Null
End If
As far as using the System.Data.SqlTypes.DbNull is concerned, I was never
able to figure out a way to make it work. I could get it to work if it was
null or wasn't null; but never both at the same time.
If you wanted to get around the check at this time, what you could do is
inside your structure, us SqlDataTypes instead of the standard Int32. This
would allow you to set a DbNull value to the structure and then when you are
using the structure you can check to see if it is DbNull then. I think
however that you would end up checking the value more often this way, but
depending on the scenario this could be the better result.
-Aaron Weiker
----- Original Message -----
From: "Zackery Breen" <zackery_breen@h...>
To: "ADO.NET" <ado_dotnet@p...>
Sent: Friday, March 29, 2002 7:49 AM
Subject: [ado_dotnet] Re: Retrieving null values from fields using
C#/ADO.NET
> Thanks,
>
> But what I am trying to do is call the stored procedure, store the
returned
> values into a struct and then pass the struct off to a different part of
the
> application for use. I cannot check for null before retrieving the value
> because I still wouldn't know if the result where null or not at the point
I
> intend to use it.
>
> I noticed that the classes in System.Data.SqlClient have a property that
> indicates whether the value is null or not. Any idea how i can use these
with a
> stored procedure that returns output parameters?
>
> Thanks again Aaron
>
>
> > What you want to do is check if the value is null before you pull it
out.
>
> Use the function IsDbNull to check the value before you retrieve it. Here
is
> an example of VB.NET code which will port directly to C#
>
> If Not (IsDBNull(SqlCmd.Parameters("@Login").Value)) Then
> Me.Login = CStr(SqlCmd.Parameters("@Login").Value)
> End If
>
> -Aaron Weiker
>
>
>
>
Message #6 by "Zackery Breen" <zackery_breen@h...> on Fri, 29 Mar 2002 15:09:56
|
|
I've tried using SqlData types within my struct but always get an invalid cast
exception when i retrieve the value from the parameter:
struct Example
{
public SqlMoney salePrice;
}
....
....
SqlConnection oConnection = new SqlConnection(sConnectionString);
SqlCommand oCommand = new SqlCommand("sp_MyProcedure",
oConnection);
SqlParameter prmSalePrice = new SqlParameter("@SalePrice",
SqlDbType.SmallMoney, 4);
prmSalePrice.Direction = ParameterDirection.OutPut;
prmSalePrice.IsNullable = true;
...
...
// an exception will occur at this point
myExample.salePrice = (SqlMoney) prmSalePrice.Value;
I guess one of the first things i do when I get back to the us is buy a book on
ado.net :)
Thanks again
Zackery Breen
|
|
 |