customer objects from datareader to BindingList<Customer>

H

hazz

The data access layer below returns, well, a mess as you can see on the last
line of this posting. What is the best way to return customer objects via a
datareader from the data layer into my view and bind them to a datagrid
using BindingList. I am in need of an epiphany here. Thank you. -Greg

******************BIND ARRAY OF CUSTOMER OBJECTS TO DATAGRID
************************
private Customer[] c;
c = BankTellerDataAccess.SqlHelper.ExecuteNonQuery();
BindingSource bs = new BindingSource();
BindingList<Customer> bList = new BindingList<Customer>();
// Fill bList with Customers
for (int i = 1; i <= 100; i++)
{
bList.Add(new Customer(c.ID, c.FirstName, c.LastName, c.Address1,);
}
bs.DataSource = bList;
dataGridView1.DataSource = bs;

******************FROM DATA ACCESS LAYER ************************
ArrayList arr = new ArrayList();

---datareader sets ID, FirstName, variables to values from database)

Customer o = new Customer(ID,FirstName, LastName, Address1);
arr.Add(o);
return (Customer[])arr.ToArray(typeof(Customer));
 
F

Frans Bouma [C# MVP]

hazz said:
The data access layer below returns, well, a mess as you can see on
the last line of this posting. What is the best way to return
customer objects via a datareader from the data layer into my view
and bind them to a datagrid using BindingList. I am in need of an
epiphany here. Thank you. -Greg

******************BIND ARRAY OF CUSTOMER OBJECTS TO DATAGRID
************************
private Customer[] c;
c = BankTellerDataAccess.SqlHelper.ExecuteNonQuery();
BindingSource bs = new BindingSource();
BindingList<Customer> bList = new BindingList<Customer>();
// Fill bList with Customers
for (int i = 1; i <= 100; i++)
{
bList.Add(new Customer(c.ID, c.FirstName, c.LastName, c.Address1,);
}
bs.DataSource = bList;
dataGridView1.DataSource = bs;

******************FROM DATA ACCESS LAYER ************************
ArrayList arr = new ArrayList();

---datareader sets ID, FirstName, variables to values from database)

Customer o = new Customer(ID,FirstName, LastName, Address1);
arr.Add(o);
return (Customer[])arr.ToArray(typeof(Customer));

You could try:

public List<Customer> GetCustomers()
{
List<Customer> toReturn = new List<Customer>();
// read datareader into Customer instances and add them to toReturn
// ..

return toReturn;
}

and in your bind routine:

private List<Customer> customers =
BankTellerDataAccess.SqlHelper.ExecuteNonQuery();

BindingSource bs = new BindingSource();
BindingList<Customer> bList = new BindingList<Customer>(customers);
bs.DataSource = bList;
dataGridView1.DataSource = bs;

FB


--
 
H

hazz

Thank you so, so, so much Frans. I am studying that now. It really is a
shift for me so your help is much appreciated. I will reply back with
results. -Greg

Frans Bouma said:
hazz said:
The data access layer below returns, well, a mess as you can see on
the last line of this posting. What is the best way to return
customer objects via a datareader from the data layer into my view
and bind them to a datagrid using BindingList. I am in need of an
epiphany here. Thank you. -Greg

******************BIND ARRAY OF CUSTOMER OBJECTS TO DATAGRID
************************
private Customer[] c;
c = BankTellerDataAccess.SqlHelper.ExecuteNonQuery();
BindingSource bs = new BindingSource();
BindingList<Customer> bList = new BindingList<Customer>();
// Fill bList with Customers
for (int i = 1; i <= 100; i++)
{
bList.Add(new Customer(c.ID, c.FirstName, c.LastName, c.Address1,);
}
bs.DataSource = bList;
dataGridView1.DataSource = bs;

******************FROM DATA ACCESS LAYER ************************
ArrayList arr = new ArrayList();

---datareader sets ID, FirstName, variables to values from database)

Customer o = new Customer(ID,FirstName, LastName, Address1);
arr.Add(o);
return (Customer[])arr.ToArray(typeof(Customer));

You could try:

public List<Customer> GetCustomers()
{
List<Customer> toReturn = new List<Customer>();
// read datareader into Customer instances and add them to toReturn
// ..

return toReturn;
}

and in your bind routine:

private List<Customer> customers =
BankTellerDataAccess.SqlHelper.ExecuteNonQuery();

BindingSource bs = new BindingSource();
BindingList<Customer> bList = new BindingList<Customer>(customers);
bs.DataSource = bList;
dataGridView1.DataSource = bs;

FB


--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
H

hazz

works beautfully Frans! Thank you so much.
My next task is to find out how to edit the Customer items in the grid and
how List<T> handles that on updates or if I use something else.
This is working within the context of the Composite UI app block and will be
using Windows Communication Foundation for access to the database.
Thanks again!

Frans Bouma said:
hazz said:
The data access layer below returns, well, a mess as you can see on
the last line of this posting. What is the best way to return
customer objects via a datareader from the data layer into my view
and bind them to a datagrid using BindingList. I am in need of an
epiphany here. Thank you. -Greg

******************BIND ARRAY OF CUSTOMER OBJECTS TO DATAGRID
************************
private Customer[] c;
c = BankTellerDataAccess.SqlHelper.ExecuteNonQuery();
BindingSource bs = new BindingSource();
BindingList<Customer> bList = new BindingList<Customer>();
// Fill bList with Customers
for (int i = 1; i <= 100; i++)
{
bList.Add(new Customer(c.ID, c.FirstName, c.LastName, c.Address1,);
}
bs.DataSource = bList;
dataGridView1.DataSource = bs;

******************FROM DATA ACCESS LAYER ************************
ArrayList arr = new ArrayList();

---datareader sets ID, FirstName, variables to values from database)

Customer o = new Customer(ID,FirstName, LastName, Address1);
arr.Add(o);
return (Customer[])arr.ToArray(typeof(Customer));

You could try:

public List<Customer> GetCustomers()
{
List<Customer> toReturn = new List<Customer>();
// read datareader into Customer instances and add them to toReturn
// ..

return toReturn;
}

and in your bind routine:

private List<Customer> customers =
BankTellerDataAccess.SqlHelper.ExecuteNonQuery();

BindingSource bs = new BindingSource();
BindingList<Customer> bList = new BindingList<Customer>(customers);
bs.DataSource = bList;
dataGridView1.DataSource = bs;

FB


--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
H

hazz

The List<T> will be fine for its purpose, to create a list view essentially
from which rows will be selected to indicate the specific customer object to
bring up in a detail view. Updates of the model or business object can be
had via different means.

hazz said:
works beautfully Frans! Thank you so much.
My next task is to find out how to edit the Customer items in the grid and
how List<T> handles that on updates or if I use something else.
This is working within the context of the Composite UI app block and will
be using Windows Communication Foundation for access to the database.
Thanks again!

Frans Bouma said:
hazz said:
The data access layer below returns, well, a mess as you can see on
the last line of this posting. What is the best way to return
customer objects via a datareader from the data layer into my view
and bind them to a datagrid using BindingList. I am in need of an
epiphany here. Thank you. -Greg

******************BIND ARRAY OF CUSTOMER OBJECTS TO DATAGRID
************************
private Customer[] c;
c = BankTellerDataAccess.SqlHelper.ExecuteNonQuery();
BindingSource bs = new BindingSource();
BindingList<Customer> bList = new BindingList<Customer>();
// Fill bList with Customers
for (int i = 1; i <= 100; i++)
{
bList.Add(new Customer(c.ID, c.FirstName, c.LastName, c.Address1,);
}
bs.DataSource = bList;
dataGridView1.DataSource = bs;

******************FROM DATA ACCESS LAYER ************************
ArrayList arr = new ArrayList();

---datareader sets ID, FirstName, variables to values from database)

Customer o = new Customer(ID,FirstName, LastName, Address1);
arr.Add(o);
return (Customer[])arr.ToArray(typeof(Customer));

You could try:

public List<Customer> GetCustomers()
{
List<Customer> toReturn = new List<Customer>();
// read datareader into Customer instances and add them to toReturn
// ..

return toReturn;
}

and in your bind routine:

private List<Customer> customers =
BankTellerDataAccess.SqlHelper.ExecuteNonQuery();

BindingSource bs = new BindingSource();
BindingList<Customer> bList = new BindingList<Customer>(customers);
bs.DataSource = bList;
dataGridView1.DataSource = bs;

FB


--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

C# Help Needed 1

Top