Solved

Code help needed - C#

Posted on 2011-03-22
6
243 Views
Last Modified: 2013-12-17
Hi,

I have Person (Master) table and Orders (Child) table. Please find the attachments of the code. Can you please make necessary code changes to  "Load()" method ?

Thanks
/// <summary>
    /// Load all records from database
    /// </summary>
    /// <returns></returns>
    public List<Person> Load()
    {
        SqlConnection conn = new SqlConnection(connStr);
        conn.Open();
        SqlCommand cmd = new SqlCommand("LoadAll", conn);
        cmd.CommandType = CommandType.StoredProcedure;
        List<Person> list = new List<Person>();
        try
        {
            SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
            DataTable dt = new DataTable();
            dt.Load(dr);
            foreach (DataRow row in dt.Rows)
            {
                Person p = new Person();
                p.PersonID = int.Parse(row["PersonID"].ToString());
                p.FirstName = row["FirstName"].ToString();
                p.LastName = row["LastName"].ToString();
                p.Age = int.Parse(row["Age"].ToString());
                SqlConnection conn1 = new SqlConnection(connStr);
                conn1.Open();
                SqlCommand cmd1 = new SqlCommand("LoadOrder", conn1);
                cmd1.CommandType = CommandType.StoredProcedure;
                List<OrderType> listot = new List<OrderType>();
                try
                {
                    SqlDataReader dr1 = cmd1.ExecuteReader(CommandBehavior.CloseConnection);
                    DataTable dt1 = new DataTable();
                    dt1.Load(dr1);
                    foreach (DataRow row1 in dt1.Rows)
                    {
                        OrderType o = new OrderType();
                        o.OrderID = int.Parse(row1["OrderID"].ToString());
                        o.OrderName = row1["OrderName"].ToString();
                        o.ShipVia = row1["ShipVia"].ToString();
                        listot.Add(o);
                    }
                    p.Orders.AddRange(listot);
                }
                catch
                {
                    throw;
                }
                finally
                {
                    cmd1.Dispose();
                    conn1.Close();
                    conn1.Dispose();
                }
                list.Add(p);
            }
        }
        catch
        {
            throw;
        }
        finally
        {
            cmd.Dispose();
            conn.Close();
            conn.Dispose();
        }
        return list;
    }

Open in new window

Person.cs
0
Comment
Question by:milani_lucie
  • 3
  • 2
6 Comments
 

Expert Comment

by:gkuhrd2001
ID: 35196127
HI,

First explain your problem, then only anyone can help you.
Regards
Ankit
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35196193
I suspect you want to look at a JOIN statement in the SQL help.
0
 

Author Comment

by:milani_lucie
ID: 35198576
I want to create objects dynamically - i am constructing the Person object from the database. I need to fill the child objects also. The above code gives us an idea how we can construct the Person object and its related objects from the database data. Here my issue is:

p.Orders.AddRange(listot);  

is giving error. Can you modify my code and make it working ?

Thanks
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 44

Expert Comment

by:AndyAinscow
ID: 35198703
What is p.Orders ?
What error do you get ?
0
 

Author Comment

by:milani_lucie
ID: 35199075
"Object reference not set to an instance of an object."   - ERROR

p.Orders.AddRange(listot);   // Not working
p.Orders.Add(o);  // Not working

"Orders" is child object of "Person". You can check the Person.cs file which i have attached.

Thanks
0
 
LVL 44

Accepted Solution

by:
AndyAinscow earned 500 total points
ID: 35199117
private List<OrderType> ordersField;

You don't seem to create a variable of that type with 'new' anywhere before you use it.

private List<OrderType> ordersField = new List<OrderType>();

or similar.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

778 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question