Click here to Skip to main content
15,846,342 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi there.

Help me please with the following problem. I have a DataRow r, filled with data from my database. Now I want to fill an MVC model
C#
public class Customer
{
    private int id;
    private string name;
    private string region;
    private int phone;

    public int Id {get, set}
    public string Name {get, set}
    public string Region {get, set}
    public int Phone {get, set}
}


as shown below

C#
var c = new Customer {
   Id = Convert.ToInt32(r[0]),
   Name = r[1].ToString(),
   Region = r[2].ToString(),
   Phone = Convert.ToInt32(r[3])
   }


The problem is that r[3] corresponds to optional data, and if there is NULL in my database, an error with Convert occurs. What can I use to make Phone to be just 0 in the case when r[3] is NULL ?
Posted

The problem with storing it in your class as value of 0 is that if you save it back to the database, you will possibly overwrite the NULL value with 0.

A better option is to use the Nullable<int> data type, which allows representation of a missing value, or the phone number.

Also, phone numbers are better stored as strings. They are more complex than just a number. Some have important leading 0's, some may be international, some may be bigger than what an integer can represent.
 
Share this answer
 
Comments
brombenzol123 3-Nov-14 2:35am    
I exchanged int with string and think that was really bad idea to use int for cells containitg nulls. Thank you for useful advices.
NeverJustHere 3-Nov-14 2:40am    
There is nothing wrong with using int for data that can be null.

The difference is the string type natively supports null, int does not. The string type is a little unusual in .net in this regard.

The int type can be wrapped around a null manager by using nullable<int>. This would be the correct way of representing any number that may not exist, rather than converting them all to strings.
brombenzol123 3-Nov-14 2:53am    
As far as I understand I have to put a sign ? to int's in my model definition as below. But unfortunately it still gives InvalidCastException. Maybe I have to modify my Cast method somehow?

public class Customer
{
private int id;
private string name;
private string region;
private int? phone;

public int Id {get, set}
public string Name {get, set}
public string Region {get, set}
public int? Phone {get, set}
}
NeverJustHere 3-Nov-14 3:05am    
Unfortunately, there is no automatic conversion from database values to Nullable int, so you need to code this yourself.

Use something like (from memory, so may need fixing):
NullableInt = dbField.IsNull ? Nothing : dbField.GetInt()
brombenzol123 3-Nov-14 5:32am    
I understand. Thanks for helping.
You should be able to use Convert.IsDBNull

var c = new Customer {
        Id = Convert.ToInt32(r[0]),
        Name = r[1].ToString(),
        Region = r[2].ToString(),
        Phone = Convert.IsDBNull(r[3]) ? 0 : Convert.ToInt32(r[3])
        }
 
Share this answer
 
Comments
brombenzol123 3-Nov-14 2:37am    
I decided to exchange int with string so the problem automatically disappears. But thank you for advice, that's can be useful in future.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900