convert null value to 0

  • Thread starter Thread starter Vicky
  • Start date Start date
I don't know if this is the best way but here's one option
int s = Convert.ToInt32(null);
 
You can just set the datacolumn value to 0, for instance

DataTableName.Rows[0][ColumnIndex] = 0;
http://msdn.microsoft.com/library/d.../html/frlrfsystemdatadatacolumnclasstopic.asp

If you mean that when you create a new row that it defaults to 0 instead of
dbnull, then you can set the DefaultValue property of the datacolumn
http://www.knowdotnet.com/articles/types.html
DataColumnName.DefaultValue = 0;

If you are talking about a dataGrid and want DbNull values to be reflected
as 0's instead of the literal null, then add a DataGridTableStyle, afterward
add a DataGridColumnStyle and set the rows DefaultValue property to 0.
http://www.knowdotnet.com/articles/kdngrid.html

Since you mention null instead of DbNull (which is what the value will come
back as) I thought this might be what you were referencing.

--
W.G. Ryan MVP Windows - Embedded

www.devbuzz.com
www.knowdotnet.com
http://www.msmvps.com/williamryan/
 
But i am not making datatable , but opening datatable from a database using
a stored procedure.
Now in this case how to make sure that dbnull is converted to 0 withour
doing any changes in my stored procedure or database
"William Ryan eMVP"
You can just set the datacolumn value to 0, for instance

DataTableName.Rows[0][ColumnIndex] = 0;
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatacolumnclasstopic.asp

If you mean that when you create a new row that it defaults to 0 instead of
dbnull, then you can set the DefaultValue property of the datacolumn
http://www.knowdotnet.com/articles/types.html
DataColumnName.DefaultValue = 0;

If you are talking about a dataGrid and want DbNull values to be reflected
as 0's instead of the literal null, then add a DataGridTableStyle, afterward
add a DataGridColumnStyle and set the rows DefaultValue property to 0.
http://www.knowdotnet.com/articles/kdngrid.html

Since you mention null instead of DbNull (which is what the value will come
back as) I thought this might be what you were referencing.

--
W.G. Ryan MVP Windows - Embedded

www.devbuzz.com
www.knowdotnet.com
http://www.msmvps.com/williamryan/
Vicky said:
What is the best way of converting Null value in datatable numeric
column
to
0.

THanks
 
Hi Vicky,

A simple solution would be iterate in the rows read checking for null and
assigning it a 0 instead.

Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



Vicky said:
But i am not making datatable , but opening datatable from a database using
a stored procedure.
Now in this case how to make sure that dbnull is converted to 0 withour
doing any changes in my stored procedure or database
"William Ryan eMVP"
You can just set the datacolumn value to 0, for instance

DataTableName.Rows[0][ColumnIndex] = 0;
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatacolumnclasstopic.asp

If you mean that when you create a new row that it defaults to 0
instead
of
dbnull, then you can set the DefaultValue property of the datacolumn
http://www.knowdotnet.com/articles/types.html
DataColumnName.DefaultValue = 0;

If you are talking about a dataGrid and want DbNull values to be reflected
as 0's instead of the literal null, then add a DataGridTableStyle, afterward
add a DataGridColumnStyle and set the rows DefaultValue property to 0.
http://www.knowdotnet.com/articles/kdngrid.html

Since you mention null instead of DbNull (which is what the value will come
back as) I thought this might be what you were referencing.

--
W.G. Ryan MVP Windows - Embedded

www.devbuzz.com
www.knowdotnet.com
http://www.msmvps.com/williamryan/
column
 
public int ConvertToInt(object valueFromDb)
{
try
{
return valueFromDb != DBNull.Value ? (int) valueFromDb : 0;
}
catch(InvalidCastException exc)
{
return 0;
}
}


int aNumber = ConvertToInt(whateverComesFromTheDB);




Vicky said:
But i am not making datatable , but opening datatable from a database using
a stored procedure.
Now in this case how to make sure that dbnull is converted to 0 withour
doing any changes in my stored procedure or database
"William Ryan eMVP"
You can just set the datacolumn value to 0, for instance

DataTableName.Rows[0][ColumnIndex] = 0;
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfsystemdatadatacolumnclasstopic.asp

If you mean that when you create a new row that it defaults to 0
instead
of
dbnull, then you can set the DefaultValue property of the datacolumn
http://www.knowdotnet.com/articles/types.html
DataColumnName.DefaultValue = 0;

If you are talking about a dataGrid and want DbNull values to be reflected
as 0's instead of the literal null, then add a DataGridTableStyle, afterward
add a DataGridColumnStyle and set the rows DefaultValue property to 0.
http://www.knowdotnet.com/articles/kdngrid.html

Since you mention null instead of DbNull (which is what the value will come
back as) I thought this might be what you were referencing.

--
W.G. Ryan MVP Windows - Embedded

www.devbuzz.com
www.knowdotnet.com
http://www.msmvps.com/williamryan/
column
 
Back
Top