Solved

Code help needed - C#

Posted on 2011-03-22
6
241 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
Comment Utility
HI,

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

Expert Comment

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

Author Comment

by:milani_lucie
Comment Utility
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 44

Expert Comment

by:AndyAinscow
Comment Utility
What is p.Orders ?
What error do you get ?
0
 

Author Comment

by:milani_lucie
Comment Utility
"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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 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

17 Experts available now in Live!

Get 1:1 Help Now