How to deal with integer that can be null in sql server?

T

TomislaW

I have integers in my database that can be null. I read them with
SqlDataReader and show them to Repeater. Since integer can not be null in
c#, I set null integer to -1. My project is 3-tier and it is very
complicated to check in data tier if it is null set -1, then in business
tier if it is -1 do not calculate with him and then in presentation tier "do
not show if it is -1". Is there any better solution for that?
 
P

Paul E Collins

TomislaW said:
I have integers in my database that can be null.
[...]
Since integer can not be null in c#, I set null integer to -1.

Use SqlInt32 (in System.Data.SqlTypes) and SqlInt32.Null.

P.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

No really, no matter what you do you will have to check for a "null" it
does not matter if the "null" is -1 or null.

You would have to do the same thing even if a null value was available, you
would have to check by null before any operation.

nothing that a simple if can't solve. :)

Cheers,
 
T

TomislaW

I decide to use SqlDataReader and asp:Repeater because of speed.

If I checking asp:Repeater on ItemDataBound I loose that speed, where to
check for nulls?

Is it better solution to build asp:Table in code behind?
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

I decide to use SqlDataReader and asp:Repeater because of speed.


This has nothing to do with the subject at hand the problem will exist no
matter what you use to show your data.
If I checking asp:Repeater on ItemDataBound I loose that speed, where to
check for nulls?

You could define a method that return the string you want to use to show
null, below is an example of such a construction from a datagrid

<ItemTemplate>
<asp:Label ID="lbl_inc_identification" Text=<%# FormatID(
Container.DataItem("inc_identification") )%> Runat="server"></asp:Label>
</ItemTemplate>

in the code behind:

protected string FormatID( object o)
{
if ( ( int)o == -1 )
return "N/A";
return o.ToString();
}
Is it better solution to build asp:Table in code behind?

Absolutly not !


Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top