Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi,

 

i have following problem:

I get data from SQL-Server and some int fields contains dbnull. The data is bound to an strong typed Dataset. The DataSet is bound to different controls in my app.

 

If i set no "null value" for the int fields my datarow.intColumn contains in debugger an exeption.

 

If I set the "null value" to 0 i can't differ if the user has type 0 in the TextBox or if i set this 0 value in my strong typed dataset.

 

Any solutions?

 

Thanks, WebJumper!

Posted

hi WebJumper,

I resolve similar problem with values types and null

too. For this situation I write our own C# classes

(from bool to datetime).

Work with my classes is wery good and simple (like

with clasic objects). I use this a specialy in class

properties,

for example when is need accessing to database.

Example:

 

//Define variable

CsInt index;

:

:

//Set value

index = 1532;

OR

index = null;

:

:

if (index != null)

{}

else

{}

:

:

//Create SqlParam

SqlParameter para = new SqlParameter("@index", index

!= null ? index.Value : (object)DBNull.Value);

 

 

 

So, when do you will, I send you my Cs.Dll for your

use.

 

Best regards

  • 1 month later...
Posted (edited)
Initialize them to -1

 

You could also trap the '-1' value when the textbox's text changed event fires...

 

// If the DBNull values are initialized to '-1'
// when the '-1' is put into the textbox you
// just swap it out with an ampty string...
if (textBox1.Text == "-1")
{
    textBox1.Text = "";
}
// This way, when you refer to the value 
// in your code you can look for '-1' but 
// the user will only see an empty box to be filled in...

 

This is just off the top of my head but, should work.

Edited by Mothra
Being smarter than you look is always better than looking smarter than you are.
  • *Experts*
Posted

I don't know that a typed DataSet will work the way you want unless you do something like what Mothra suggests. If the typed dataset sees the column as int and defines the internal variable as an int, then that value CANNOT be Null in any form (null or DBNull). That means you'll need some value that you can translate back to null. Zero is often a good choice, I like -1 too.

 

Since you don't want to see the value in the bound field, you'll need some kind of translation. For a textbox, I'd suggest hooking up the Format and Parse events (part of the DataBindings collection of the Textbox, I believe). It allows translation between the bound data (DataSet) and what the control display (a Text property). Look in the MSDN help for examples of the Format/Parse events.

 

You might experiment with the NullValue property of the DataSet - I've never tried it but it might be usable.

 

If your DataSet is "static" - not changing the typed dataset very often - you could tweak the auto-generated typed dataset code. Change the int to type object, which allows DBNull. Of course, it's no longer "typed", but might get you what you want if it's only for a few int fields.

 

-Nerseus

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

aspcreator@yahoo.com

 

Use value -1 for detecting null value in variable is posible, but in case, that your value interval is <0;+). In this case is value -1 typical indicator. But you have problem in course, when used values is from interval (-;+). Your indicator -1 may by real value of variable. Question is, how I can indicated null value?

Typicaly problem is, when you read data from database (SQL,...).

:

:

private int RealValue = 0;

:

:

//Read data

:

this.RealValue = ds["RealValue"] != DBNull.Value ? int.Parse(ds["RealValue"].ToString()) : -1;

:

:

 

Column in database may by null value. Your private variable RealValue is in interval (-;+) with the inclusion of 0 (zero). And now I ask you, what do you do with them?

 

I use own classes and for define in this case I can write this problem like:

 

:

private CsInt RealValue = null;

:

:

//Read data

:

this.RealValue = ds["RalaValue"] != DBNull.Value ? CsInt.Parse(ds["RalaValue"].ToString()) : null;

:

:

:

//Test for null

:

textBox1.Text = this.RealValue != null ? this.RealValue.Value.ToString() : "";

:

:

Above I show way, how I read data from database. Contraries way, for write to database I use this:

:

:

this.RealValue = textBox1.Text.Length > 0 : CsInt.Parse(textBox1.Text) : null;

:

:

In this case I exactness know, how value is in my variable. I know, that variable is null, this variable isn't use, wasn't initialize.

 

I belive, that absence of reference types (like bool, int, datetime, ....) in C# is in some case big problem and substitution in some case isn't possible.

 

Sorry for my english :o))

Octopus

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...