Solved

Code help needed - C#

Posted on 2011-03-22
6
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

Question has a verified solution.

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

Suggested Solutions

Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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