Solved

Code help needed - C#

Posted on 2011-03-22
6
242 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

947 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now