473,395 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

convert null value to 0

What is the best way of converting Null value in datatable numeric column to
0.

THanks
Nov 16 '05 #1
5 23518
I don't know if this is the best way but here's one option
int s = Convert.ToInt32(null);


Nov 16 '05 #2
You can just set the datacolumn value to 0, for instance

DataTableName.Rows[0][ColumnIndex] = 0;
http://msdn.microsoft.com/library/de...classtopic.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" <vi***@hh.com> wrote in message
news:eG**************@TK2MSFTNGP11.phx.gbl...
What is the best way of converting Null value in datatable numeric column to 0.

THanks

Nov 16 '05 #3
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"
<do********@comcast.nospam.net> wrote in message
news:uE**************@TK2MSFTNGP09.phx.gbl...
You can just set the datacolumn value to 0, for instance

DataTableName.Rows[0][ColumnIndex] = 0;
http://msdn.microsoft.com/library/de...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" <vi***@hh.com> wrote in message
news:eG**************@TK2MSFTNGP11.phx.gbl...
What is the best way of converting Null value in datatable numeric
column to
0.

THanks


Nov 16 '05 #4
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" <vi***@hh.com> wrote in message
news:uH**************@tk2msftngp13.phx.gbl...
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"
<do********@comcast.nospam.net> wrote in message
news:uE**************@TK2MSFTNGP09.phx.gbl...
You can just set the datacolumn value to 0, for instance

DataTableName.Rows[0][ColumnIndex] = 0;

http://msdn.microsoft.com/library/de...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" <vi***@hh.com> wrote in message
news:eG**************@TK2MSFTNGP11.phx.gbl...
What is the best way of converting Null value in datatable numeric

column
to
0.

THanks



Nov 16 '05 #5
public int ConvertToInt(object valueFromDb)
{
try
{
return valueFromDb != DBNull.Value ? (int) valueFromDb : 0;
}
catch(InvalidCastException exc)
{
return 0;
}
}
int aNumber = ConvertToInt(whateverComesFromTheDB);


"Vicky" <vi***@hh.com> schrieb im Newsbeitrag
news:uH**************@tk2msftngp13.phx.gbl...
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"
<do********@comcast.nospam.net> wrote in message
news:uE**************@TK2MSFTNGP09.phx.gbl...
You can just set the datacolumn value to 0, for instance

DataTableName.Rows[0][ColumnIndex] = 0;

http://msdn.microsoft.com/library/de...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" <vi***@hh.com> wrote in message
news:eG**************@TK2MSFTNGP11.phx.gbl...
What is the best way of converting Null value in datatable numeric

column
to
0.

THanks



Nov 16 '05 #6

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

Similar topics

4
by: DiggidyMack69 | last post by:
I have a sql resultset called columns. When I convert an integer column to string that is a NULL it converts to zero. How can I get it to convert to null?? String x; x =...
7
by: whatluo | last post by:
Hi, all I'm now working on a program which will convert dec number to hex and oct and bin respectively, I've checked the clc but with no luck, so can anybody give me a hit how to make this done...
3
by: MJB | last post by:
I'm getting an IStream back from function xmlHttp.responsestream. I would like to convert this to a System.IO.Stream in order to work with it in my application. Has anyone encountered this and...
22
by: Christoph Boget | last post by:
I am getting an error (a few among many) for the following lines of code: retval.BrokerName = (( curRow == System.DBNull.Value ) ? SqlString.Null : (string)curRow ); retval.BrokerGroupId = ((...
12
by: Brian Henry | last post by:
first question... I have a flat file which unfortinuatly has columns seperated by nulls instead of spaces (a higher up company created it this way for us) is there anyway to do a readline with this...
5
by: manmit.walia | last post by:
Hello All, I am stuck on a conversion problem. I am trying to convert my application which is written in VB.NET to C# because the project I am working on currently is being written in C#. I tried...
5
by: Learner | last post by:
Hello, Here is the code snippet I got strucked at. I am unable to convert the below line of code to its equavalent vb.net code. could some one please help me with this? static public...
7
by: Mike Howard | last post by:
I'm trying to convert the following (simplified) VB.Net code to C#, that makes use of some externally developed COM code written in VB6. VB.Net Code Sub Main() Dim oAPP As Object Dim oApp2...
1
by: priyakollu | last post by:
i have developed code in jsp but the problem is i need to convert this to asp.........pleasehelp me regarding this im sending code which i've developed....with file names.......... code.jsp <%@...
11
by: lenygold via DBMonster.com | last post by:
I am tryieng to convert our time consuming recursive queries too very efficient queries based on nested set model. The only problem is to convert an adjacency list model into a nested set model,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
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,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...
0
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,...

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.