.NET Forum / Languages / C# / May 2007
Testing for DBNulls
|
|
Thread rating:  |
Fred Chateau - 24 May 2007 20:07 GMT I can't seem to find a test for DBNulls. Whatever I try doesn't work.
for (int x = 0; x < dataSet.Identity.Rows.Count; x++) { DataRow dataRow = dataSet.Tables["POI_Entity"].NewRow();
if (!dataSet.Identity[x].Chain_ID.Equals(DBNull.Value)) dataRow["Chain_Id"] = dataSet.Identity[x].Chain_ID; else dataRow["Chain_Id"] = 0;
dataSet.Tables["POI_Entity"].Rows.Add(dataRow); }
I've also tried
(!Convert.IsDBNull(dataSet.Identity[x].Chain_ID)
That doesn't work either.
 Signature Regards,
Fred Chateau http://hotelmotelnow.com
macleod - 24 May 2007 20:21 GMT try if (dataSet.Identity[x].Chain_ID != null)
>I can't seem to find a test for DBNulls. Whatever I try doesn't work. > [quoted text clipped - 15 lines] > > That doesn't work either. Nicholas Paldino [.NET/C# MVP] - 24 May 2007 20:22 GMT Fred,
Have you set a point in the debugger and looked at what the value of:
dataSet.Identity[x].Chain_ID
Actually is?
 Signature - Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com
>I can't seem to find a test for DBNulls. Whatever I try doesn't work. > [quoted text clipped - 15 lines] > > That doesn't work either. Fred Chateau - 24 May 2007 20:33 GMT Well, I can't believe I found it but I did. I had to edit the code generated by XSD.
[System.Diagnostics.DebuggerNonUserCodeAttribute()] public string Chain_ID { get { try { if (!Convert.IsDBNull(this[this.tableIdentity.Chain_IDColumn])) <-- Added code return ((string) (this[this.tableIdentity.Chain_IDColumn])); else return "0"; <-- Added code } catch (System.InvalidCastException e) { throw new System.Data.StrongTypingException("The value for column \'Chain_ID\' in table \'Identity\' is DBNull.", e); } } set { this[this.tableIdentity.Chain_IDColumn] = value; } }
 Signature Regards,
Fred Chateau http://hotelmotelnow.com
> Fred, > [quoted text clipped - 23 lines] >> >> That doesn't work either. Nicholas Paldino [.NET/C# MVP] - 24 May 2007 20:47 GMT Fred,
Well, I wouldn't do that. What I was recommending was to determine what the value that was being returned was through the debugger in your program, and then using that knowledge to change the code.
Generally, I wouldn't change the designer code, because it will not respect your changes if you make changes through the designer.
 Signature - Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com
> Well, I can't believe I found it but I did. I had to edit the code > generated by XSD. [quoted text clipped - 46 lines] >>> >>> That doesn't work either. Fred Chateau - 24 May 2007 20:57 GMT I'm not sure how I could do anything else. It tries to cast the XML Node value to a string and if the cast fails, it throws an Invalid Cast Exception, then sends out the message: The value for column <columnName> in table <tableName> is DBNull.
It should pass the DBNull along so I can test for it, but it never gets that far. You can't modify your code when you don't even have access to the variable.
Do you think there's a way to catch the exception? Even if there is, isn't that bad practice to use an exception to determine a decision branch in the code?
 Signature Regards,
Fred Chateau http://hotelmotelnow.com
-- Regards,
Fred Chateau http://hotelmotelnow.com
> Fred, > [quoted text clipped - 55 lines] >>>> >>>> That doesn't work either. Nicholas Paldino [.NET/C# MVP] - 24 May 2007 22:30 GMT Fred,
This is a typed data set, right? You should be able to highlight the column in the designer and indicate that you want it to return DBNull when null is encountered for that column.
 Signature - Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com
> I'm not sure how I could do anything else. It tries to cast the XML Node > value to a string and if the cast fails, it throws an Invalid Cast [quoted text clipped - 69 lines] >>>>> >>>>> That doesn't work either. Fred Chateau - 25 May 2007 00:13 GMT Well, that worked too.
The choices are empty, null and throw exception. I set the value to return an empty string and then tested for the empty string.
Thanks again, Nicholas.
 Signature Regards,
Fred Chateau http://hotelmotelnow.com
> Fred, > [quoted text clipped - 75 lines] >>>>>> >>>>>> That doesn't work either. Nicholas Paldino [.NET/C# MVP] - 25 May 2007 00:19 GMT Fred,
Just a note, in this case, I would opt to return null, since there is a difference between null and an empty string in both .NET and DB-land.
 Signature - Nicholas Paldino [.NET/C# MVP] - mvp@spam.guard.caspershouse.com
> Well, that worked too. > [quoted text clipped - 82 lines] >>>>>>> >>>>>>> That doesn't work either. Larry Smith - 25 May 2007 00:43 GMT > Well, that worked too. > > The choices are empty, null and throw exception. I set the value to return > an empty string and then tested for the empty string. Sorry, I sent my last email off prematurely (on another thread). For strongly-typed datasets life is much easier. Take a look at the "whatever.Designer.cs" file under your ".xsd" file. Here's a quickly-written analogue of your original post. It's what you should be doing but it might be a little rough since I don't have your actual file to work with (and I'm doing this from memory).
foreach (IdentityRow identyRow in dataSet.Identity) { POI_EntityRow newRow = dataSet.POI_Entity.NewPOI_EntityRow();
if (!identityRow.IsChain_IDNull) { newRow.Chain_Id = idenityRow.Chain_ID; } else { newRow.Chain_Id = 0; }
dataSet.POI_Entity.AddPOI_EntityRow(newRow); }
Fred Chateau - 25 May 2007 05:42 GMT Larry, I set the designer back to "Throw Exception" and used your method of testing for DBNulls. It works well, and like Nicholas said, it's better to handle the problem in your own code than changing code in designers, or even designer settings.
I think this is the way DBNull was meant to be used, that is, testing for it like you would test for object nulls and responding in an appropriate manner.
 Signature Regards,
Fred Chateau http://hotelmotelnow.com
>> Well, that worked too. >> [quoted text clipped - 23 lines] > dataSet.POI_Entity.AddPOI_EntityRow(newRow); > } Larry Smith - 25 May 2007 11:57 GMT > Larry, I set the designer back to "Throw Exception" and used your method > of testing for DBNulls. It works well, and like Nicholas said, it's better [quoted text clipped - 4 lines] > it like you would test for object nulls and responding in an appropriate > manner. Yes, but also note how much easier it is to work in general using strongly-typed datasets. There's a class for each table and a corresponding row class where each field (column) is exposed as a property IOW, you no longer have to work with a "DataTable" or "DataRow" directly anymore (most of the time anyway). Other properties/functions in "whatever.Designer.cs" greatly simplify the entire "DataSet" experience. You can also easily extend each table and row class by redeclaring it in "whatever.cs" using the "partial" keyword (for VS2005). This allows you to add your own custom functions which is the way I handle things. Good luck.
Göran Andersson - 24 May 2007 20:26 GMT > I can't seem to find a test for DBNulls. Whatever I try doesn't work. > [quoted text clipped - 15 lines] > > That doesn't work either. What kind of object does your variable "dataSet" reference?
If you have a DataRow, it has the method IsNull that you can use to check for DbNull values.
If you have a data reader, it has the method IsDbNull that you can use to check for DbNull values.
 Signature Göran Andersson _____ http://www.guffa.com
Larry Smith - 25 May 2007 00:07 GMT > for (int x = 0; x < dataSet.Identity.Rows.Count; x++) > { [quoted text clipped - 13 lines] > > That doesn't work either. foreach (IdentityRow identtyRow in dataSet.Identity) {
if (identityRow.IsChain_IDNull) { } else { }
dataSet.POI_Entity.New }
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|