Click here to Skip to main content
15,914,350 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello every body.

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:
C#
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" in 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.
Posted
Updated 7-Oct-14 21:27pm
v2

I believe, you're getting the error because the Column you're referencing to, the ID column, is null. As stated in your code,

dr1["id"]


You're using the assignment operator, so the error would be generated through right hand side, where you can see, the data is being extracted from dr1 (which is SQL data holder) and the item you're fetching is ID.

NullReferenceException is generated when the value being passed is null. So, you need to make sure that the value is being passed to a function or operator which requires value.

As you're saying, you think
ClsAdvertisement[]
is not present, if that was the case, Visual Studio would have passed the Line (that is currently displaying the error) and then threw an error on that particular line and would have said, "ClsAdvertisement[] not found for the type [your_type_here]. Are you missing a reference?" Something like this. In your case, this is not th error.

Learn more about null errors in code execution from my blog post: What is a null error in code Execution[^]
 
Share this answer
 
you are getting error since the object quanties has three items and all are null.

you can use List instead of array Like this.
C#
public class ClsAdvertisement
{
  public string type { get; set; }
  public double typical { get; set; }
  public double min { get; set; }
}
public class ClsService
{
  public  List<clsadvertisement>>}

protected void MyFuction()
{
  List<clsservice> services = new List<clsservice>();
  List<clsadvertisement> service=new List<clsadvertisement>();
  
  service.Add(new ClsAdvertisement()
   {
     typical=1,
     type = "id",
     min = 1
   });
  service.Add(new ClsAdvertisement()
   {
     typical = 2,
     type = "1",
     min = 2
   });
  service.Add(new ClsAdvertisement()
   {
     typical = 3,
     type = "b",
     min = 3
   });
   services.Add(new ClsService()
       {
          service = service
       });
}


By this you can add as many as items.
 
Share this answer
 
v2
Thanks for your answers.
About the first solution, My data is not null. Checking if it is not null is some thing we can do, but here my data is not null.
And, about the second solution, I want to fill data in id.typical and id.min, from my table in SQL.
However, Thanks a lot both of you.
I could get my answer with the following codes:
C#
List<clsadvertisement[]> services = new List<clsadvertisement[]>();
       using (IDataReader dr1 = cmd1.ExecuteReader())
       {
           while (dr1.Read())
           {
               ClsAdvertisement[] qualities = { new ClsAdvertisement(), new ClsAdvertisement(), new ClsAdvertisement() };

               if (!dr1.IsDBNull(dr1.GetOrdinal("id")))
               {
                   qualities[0].typical = double.Parse(dr1["id"].ToString());
                   qualities[0].type = "id";
                   qualities[0].min = qualities[0].typical;
               }
               if (!dr1.IsDBNull(dr1.GetOrdinal("a")))
               {
                   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")))
               {
                   qualities[2].typical = double.Parse(dr1["b"].ToString());
                   qualities[2].type = "b";
                   qualities[2].min = qualities[2].typical - 1.0;
               }

               services.Add(qualities);
           }
       }


Or, I could do just the following:

C#
if (dr1.IsDBNull(dr1.GetOrdinal("id")) == false)
  {
     qualities[0] = new ClsAdvertisement();
     qualities[0].typical = double.Parse(dr1["id"].ToString());
     qualities[0].type = "id";
     qualities[0].min = qualities[0].typical;
  }



The problem is most likely that I didn't instantiate the object in question before populating its properties.

Thanks for all of you.
Good Luck
 
Share this answer
 

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