Click here to Skip to main content
15,888,286 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello everyone,

i am trying to display record on gridview by binding List to it, it the last record is overwriting other records! this is my code:
C#
public class BVNNotEnrolledAllBranches
    {
        public string CustomerName { get; set; }
        public string AccountNumber { get; set; }
        public string CIF { get; set; }
        public string PhoneNumber { get; set; }
        public string BranchName { get; set; }
        public string BVN { get; set; }
        public string Email { get; set; }
    }


C#
using (OracleConnection connection = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
               {
                   DataTable dt = new DataTable();
                   OracleCommand command = new OracleCommand(select, connection);
                   command.CommandTimeout = 900000000;
                   DataSet dataset = new DataSet();
                   OracleDataAdapter adapter = new OracleDataAdapter(command);
                   adapter.Fill(dataset);
                   dt = dataset.Tables[0];
                   List<BVNNotEnrolledAllBranches> gridRecords = new List<BVNNotEnrolledAllBranches>();
                   BVNNotEnrolledAllBranches bvnFeilds = new BVNNotEnrolledAllBranches();
                   lblNoRecord.Text = "Total record(s): " + dt.Rows.Count.ToString();
                   lblNoRecord.ForeColor = System.Drawing.Color.Green;
                   foreach (System.Data.DataRow dr in dt.Rows)
                   {
                       //TransactionModel txn = new TransactionModel();
                       bvnFeilds.CustomerName = (dr["Customer_name"].ToString());
                       bvnFeilds.AccountNumber = (dr["Account_number"].ToString());
                       bvnFeilds.CIF = (dr["CIF_ID"].ToString());
                       bvnFeilds.PhoneNumber = (dr["Phone_Number"].ToString());
                       bvnFeilds.BranchName = (dr["BRANCH_NAME"].ToString());
                       bvnFeilds.BVN = (dr["BVN"].ToString());
                       bvnFeilds.Email = (dr["Email"].ToString());
                       //bvnFeilds.Status = (dr["Status"].ToString());

                       gridRecords.Add(bvnFeilds);
                   }

                   GridView1.DataSource = gridRecords;
                   GridView1.DataBind();
               }



Please is there something i missed out? Thanks in advance.
Posted
Comments
Member 10427298 17-Apr-15 7:11am    
Would show me the GridView1 definition on your .aspx page?
Member 10427298 17-Apr-15 7:14am    
In the mean time I have noticed that
BVNNotEnrolledAllBranches bvnFeilds = new BVNNotEnrolledAllBranches();
was instantiated out side the foreach. So putting this in the foreach statement will solve the problem. And your problem was only assign values to one object but every row in a grid view is an object by it self.

You're almost there.

bvnFeilds (bvnFields?) is not re-instantiated within the loop. You have one single instance that gets rewritten every loop. I have corrected below

C#
 using (OracleConnection connection = new OracleConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
                {
                    DataTable dt = new DataTable();
                    OracleCommand command = new OracleCommand(select, connection);
                    command.CommandTimeout = 900000000;
                    DataSet dataset = new DataSet();
                    OracleDataAdapter adapter = new OracleDataAdapter(command);
                    adapter.Fill(dataset);
                    dt = dataset.Tables[0];
                    List<bvnnotenrolledallbranches> gridRecords = new List<bvnnotenrolledallbranches>();
                    
                    lblNoRecord.Text = "Total record(s): " + dt.Rows.Count.ToString();
                    lblNoRecord.ForeColor = System.Drawing.Color.Green;
                    foreach (System.Data.DataRow dr in dt.Rows)
                    {
                        //Make a new one each pass
                        BVNNotEnrolledAllBranches bvnFeilds = new BVNNotEnrolledAllBranches();
                        //TransactionModel txn = new TransactionModel();
                        bvnFeilds.CustomerName = (dr["Customer_name"].ToString());
                        bvnFeilds.AccountNumber = (dr["Account_number"].ToString());
                        bvnFeilds.CIF = (dr["CIF_ID"].ToString());
                        bvnFeilds.PhoneNumber = (dr["Phone_Number"].ToString());
                        bvnFeilds.BranchName = (dr["BRANCH_NAME"].ToString());
                        bvnFeilds.BVN = (dr["BVN"].ToString());
                        bvnFeilds.Email = (dr["Email"].ToString());
                        //bvnFeilds.Status = (dr["Status"].ToString());

                        gridRecords.Add(bvnFeilds);
                    }
 
                    GridView1.DataSource = gridRecords;
                    GridView1.DataBind();
                }
</bvnnotenrolledallbranches></bvnnotenrolledallbranches>
 
Share this answer
 
Comments
Uwakpeter 17-Apr-15 7:22am    
Thanks Andy, it works!
Initialize bvnFeilds inside the loop.
C#
foreach (System.Data.DataRow dr in dt.Rows)
{
   bvnFeilds = new BVNNotEnrolledAllBranches();

That's all !

Hope, it helps :)
 
Share this answer
 
v2
Comments
Uwakpeter 17-Apr-15 7:23am    
Thanks Suvendu, it helps!
Suvendu Shekhar Giri 17-Apr-15 7:29am    
Glad that it helped :)
C#
<pre lang="cs">BVNNotEnrolledAllBranches bvnFeilds = new BVNNotEnrolledAllBranches();


was instantiated out side the foreach. So putting this in the foreach statement will solve the problem.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900