milani_lucie
asked on
Code help needed - C#
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
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;
}
Person.cs
I suspect you want to look at a JOIN statement in the SQL help.
ASKER
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
p.Orders.AddRange(listot);
is giving error. Can you modify my code and make it working ?
Thanks
What is p.Orders ?
What error do you get ?
What error do you get ?
ASKER
"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
p.Orders.AddRange(listot);
p.Orders.Add(o); // Not working
"Orders" is child object of "Person". You can check the Person.cs file which i have attached.
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
First explain your problem, then only anyone can help you.
Regards
Ankit