[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Code help needed - C#

Posted on 2011-03-22
6
Medium Priority
?
253 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 45

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
Veeam and MySQL: How to Perform Backup & Recovery

MySQL and the MariaDB variant are among the most used databases in Linux environments, and many critical applications support their data on them. Watch this recorded webinar to find out how Veeam Backup & Replication allows you to get consistent backups of MySQL databases.

 
LVL 45

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 45

Accepted Solution

by:
AndyAinscow earned 2000 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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

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.
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month19 days, 5 hours left to enroll

834 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