I have a table in SQL that includes different services. Each service have some qualities (It means each row, have different columns in SQL). For example:
s1: id1, a1, b1 (id: int, a: smallint, b: real)
s2: id2, a2, b2
...
I want to have a list of these services in c#.
For some reasons, each column, should have some attributes. For instance:
For a1 form s1, we should have these:
a1.type, a1.typical, a1.min
This is what I did:
public class ClsAdvertisement
{
public string type { get; set; }
public double typical { get; set; }
public double min { get; set; }
}
protected void MyFuction()
{
List<ClsAdvertisement[]> services = new List<ClsAdvertisement[]>();
SqlDataReader dr1 = cmd1.ExecuteReader();
if (dr1.HasRows)
{
while (dr1.Read())
{
ClsAdvertisement[] qualities=new ClsAdvertisement[3];
if (dr1.IsDBNull(dr1.GetOrdinal("id")) == false)
{
qualities[0].typical = double.Parse(dr1["id"].ToString());
qualities[0].type = "id";
qualities[0].min = qualities[0].typical;
}
if (dr1.IsDBNull(dr1.GetOrdinal("a")) == false)
{
qualities[1].typical = double.Parse(dr1["a"].ToString());
qualities[1].type = "a";
qualities[1].min = qualities[1].typical - 1.0;
}
if (dr1.IsDBNull(dr1.GetOrdinal("b")) == false)
{
qualities[2].typical = double.Parse(dr1["b"].ToString());
qualities[2].type = "b";
qualities[2].min = qualities[2].typical - 1.0;
}
services.Add(new ClsAdvertisement[] { qualities[0], qualities[1], qualities[2] });
}
}
if (dr1.IsClosed == false) dr1.Close();
}
Is it a good way for doing this?
If,yes, I get the error
System.NullReferenceException: Object reference not set to an instance of an object
At line:
qualities[0].typical = double.Parse(dr1["id"].ToString());
From my searches, I knew the reason is that there is no ClsAdvertisement[] to set the typical for. But I don`t know how to initialize this?
Please help me. Thanks a lot.
Note that I have read this link :"What is a NullReferenceException, and how do I fix it?". I Couldn`t get my answer from this link. I want to know if my way is good or not, and if yes,how to initialize this, which is not mentioned in that link .