WebJumper Posted November 28, 2003 Posted November 28, 2003 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! Quote
Moderators Robby Posted November 28, 2003 Moderators Posted November 28, 2003 Initialize them to -1 Quote Visit...Bassic Software
WebJumper Posted December 1, 2003 Author Posted December 1, 2003 When I set the null value to -1 my TextBox get's the value because of the databind, this solution will not work! Any other solutions? Quote
Octopus1 Posted December 6, 2003 Posted December 6, 2003 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 Quote
Mothra Posted January 22, 2004 Posted January 22, 2004 (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 January 22, 2004 by Mothra Quote Being smarter than you look is always better than looking smarter than you are.
*Experts* Nerseus Posted January 22, 2004 *Experts* Posted January 22, 2004 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 Quote "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
Octopus1 Posted January 25, 2004 Posted January 25, 2004 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 Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.