convert DBNull to SqlInt32.Null

  • Thread starter Thread starter Vanja Andreev
  • Start date Start date
V

Vanja Andreev

have soure something like this

private SqlInt32 _Licni_ID = SqlInt32.Null;
pomosna = new DataSet();
....
_MaticenLekar=Convert.ToInt32(pomosna.Tables[0].Rows[0]["MaticenLekar"]);

The application works OK except when "MaticenLekar" returns null when
i get error "Object cannot be cast from DBNull to other types."
i tryed all types of convert and non of them worked

So my question is how to convert System.DBNull.Value to
System.Data.SqlTypes.SqlInt32.Null?
 
Run code like this:

if(pomosna.Tables[0].Rows[0]["MaticenLekar"] == DbNull.Value)
_MaticenLekar=SQLInt32.Null;
else
_MaticenLekar=Convert.ToInt32(pomosna.Tables[0].Rows[0]["MaticenLekar"]);

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
 
The pure .Net way would be

if (pomosna.Tables[0].Rows[0].IsNull("MaticenLekar"))
_MaticenLekar = SQLInt32.Null;
else
_MaticenLekar = Convert.ToInt32(pomosna.Tables[0].Rows[0]["MaticenLekar"]);

:)

Mythran


Cowboy (Gregory A. Beamer) said:
Run code like this:

if(pomosna.Tables[0].Rows[0]["MaticenLekar"] == DbNull.Value)
_MaticenLekar=SQLInt32.Null;
else
_MaticenLekar=Convert.ToInt32(pomosna.Tables[0].Rows[0]["MaticenLekar"]);

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
Vanja Andreev said:
have soure something like this

private SqlInt32 _Licni_ID = SqlInt32.Null;
pomosna = new DataSet();
...
_MaticenLekar=Convert.ToInt32(pomosna.Tables[0].Rows[0]["MaticenLekar"]);

The application works OK except when "MaticenLekar" returns null when
i get error "Object cannot be cast from DBNull to other types."
i tryed all types of convert and non of them worked

So my question is how to convert System.DBNull.Value to
System.Data.SqlTypes.SqlInt32.Null?
 
In most cases, I would use a Strongly Typed dataset, which would be more
like so:

if(!pomosna.TableName.IsColumnNameDbNull())
{
}

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
Mythran said:
The pure .Net way would be

if (pomosna.Tables[0].Rows[0].IsNull("MaticenLekar"))
_MaticenLekar = SQLInt32.Null;
else
_MaticenLekar = Convert.ToInt32(pomosna.Tables[0].Rows[0]["MaticenLekar"]);

:)

Mythran


message news:[email protected]...
Run code like this:

if(pomosna.Tables[0].Rows[0]["MaticenLekar"] == DbNull.Value)
_MaticenLekar=SQLInt32.Null;
else
_MaticenLekar=Convert.ToInt32(pomosna.Tables[0].Rows[0]["MaticenLekar"]);

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
Vanja Andreev said:
have soure something like this

private SqlInt32 _Licni_ID = SqlInt32.Null;
pomosna = new DataSet();
...
_MaticenLekar=Convert.ToInt32(pomosna.Tables[0].Rows[0]["MaticenLekar"]);

The application works OK except when "MaticenLekar" returns null when
i get error "Object cannot be cast from DBNull to other types."
i tryed all types of convert and non of them worked

So my question is how to convert System.DBNull.Value to
System.Data.SqlTypes.SqlInt32.Null?
 
Yes, that is how I currently do it...just making the statement for the current
scenario though, since he/she/it is not using strongly typed datasets :)

Mythran


Cowboy (Gregory A. Beamer) said:
In most cases, I would use a Strongly Typed dataset, which would be more
like so:

if(!pomosna.TableName.IsColumnNameDbNull())
{
}

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
Mythran said:
The pure .Net way would be

if (pomosna.Tables[0].Rows[0].IsNull("MaticenLekar"))
_MaticenLekar = SQLInt32.Null;
else
_MaticenLekar = Convert.ToInt32(pomosna.Tables[0].Rows[0]["MaticenLekar"]);

:)

Mythran


message news:[email protected]...
Run code like this:

if(pomosna.Tables[0].Rows[0]["MaticenLekar"] == DbNull.Value)
_MaticenLekar=SQLInt32.Null;
else
_MaticenLekar=Convert.ToInt32(pomosna.Tables[0].Rows[0]["MaticenLekar"]);

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
have soure something like this

private SqlInt32 _Licni_ID = SqlInt32.Null;
pomosna = new DataSet();
...
_MaticenLekar=Convert.ToInt32(pomosna.Tables[0].Rows[0]["MaticenLekar"]);

The application works OK except when "MaticenLekar" returns null when
i get error "Object cannot be cast from DBNull to other types."
i tryed all types of convert and non of them worked

So my question is how to convert System.DBNull.Value to
System.Data.SqlTypes.SqlInt32.Null?
 
I think tha the simplest way would be
_MaticenLekar=(pomosna.Tables[0].Rows[0]["MaticenLekar"]==
System.DBNull.Value)?SqlInt32.Null:Convert.ToInt32(pomosna.Tables[0].Rows[0]["MaticenLekar"]);
but i tryed more to simplify the code becasue i should do this mabye
on thousand places in all code.
Thanks anyway.
Mythran said:
The pure .Net way would be

if (pomosna.Tables[0].Rows[0].IsNull("MaticenLekar"))
_MaticenLekar = SQLInt32.Null;
else
_MaticenLekar = Convert.ToInt32(pomosna.Tables[0].Rows[0]["MaticenLekar"]);

:)

Mythran


Cowboy (Gregory A. Beamer) said:
Run code like this:

if(pomosna.Tables[0].Rows[0]["MaticenLekar"] == DbNull.Value)
_MaticenLekar=SQLInt32.Null;
else
_MaticenLekar=Convert.ToInt32(pomosna.Tables[0].Rows[0]["MaticenLekar"]);

--
Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

************************************************
Think Outside the Box!
************************************************
Vanja Andreev said:
have soure something like this

private SqlInt32 _Licni_ID = SqlInt32.Null;
pomosna = new DataSet();
...
_MaticenLekar=Convert.ToInt32(pomosna.Tables[0].Rows[0]["MaticenLekar"]);

The application works OK except when "MaticenLekar" returns null when
i get error "Object cannot be cast from DBNull to other types."
i tryed all types of convert and non of them worked

So my question is how to convert System.DBNull.Value to
System.Data.SqlTypes.SqlInt32.Null?
 
Back
Top