Click here to Skip to main content
15,886,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a program that retrieves valid email addresses from an Oracle database table and writes them to a text file. When i run the program i am only getting 1 random row instead of up 300000+ rows. When i step through the code i can see that the value returned is repeated on all the rows returned.

This is my code for retrieving :

C#
<pre>
  List<Flex> details = new List<Flex>();
try
            {

                using (OracleConnection connp = new OracleConnection(FCUBSConnection))
                {
                    connp.Open();                 
                    OracleCommand cmd1 = new OracleCommand("SELECT FCPREPROD.STTM_CUST_PERSONAL.CUSTOMER_NO,FCPREPROD.STTM_CUST_PERSONAL.MOBILE_NUMBER,FCPREPROD.STTM_CUST_PERSONAL.LAST_NAME,FCPREPROD.STTM_CUST_PERSONAL.E_MAIL,FCPREPROD.STTM_CUST_PERSONAL.FIRST_NAME,FCPREPROD.STTM_CUSTOMER.LOCAL_BRANCH  FROM FCPREPROD.STTM_CUST_PERSONAL INNER JOIN FCPREPROD.STTM_CUSTOMER ON FCPREPROD.STTM_CUST_PERSONAL.CUSTOMER_NO = FCPREPROD.STTM_CUSTOMER.CUSTOMER_NO", connp);                 
                    OracleDataReader dr1 = cmd1.ExecuteReader();
                    
                    while ( dr1.Read())
                    {
                        var model = new Flex();
                        model.EmailAddress = Convert.ToString(dr1["E_MAIL"]);
                        model.FirstName = Convert.ToString(dr1["FIRST_NAME"]);
                        model.LastName = Convert.ToString(dr1["LAST_NAME"]);
                        model.MobileNo = Convert.ToString(dr1["MOBILE_NUMBER"]);
                        model.CustNo = Convert.ToString(dr1["CUSTOMER_NO"]);
                        details.Add(model);
                    }
                  
                    connp.Close();
                }                

                foreach (var email in details)
                {

                 

                    if (!string.IsNullOrEmpty(email.EmailAddress) && IsValidEmail(email.EmailAddress))
                    {
                     
                        //write recipients to logfile
                        string realPath = @"C:\Users\tshumae\Documents\";
                        string appLog = "SSP_EMAILS";
                        var logPath = realPath + Convert.ToString(appLog) + DateTime.Today.ToString("dd -MM-yy") + ".txt";
                        try
                        {
                            if (!File.Exists(logPath))
                        {
                            File.Create(logPath).Dispose();

                            using (StreamWriter sw = File.AppendText(logPath))
                            {
                                sw.WriteLine(email.EmailAddress + " " + email.LastName);
                                sw.Flush();
                                sw.Close();
                            }
                        }
                    }
                        catch (UnauthorizedAccessException ex)
                    {

                    }

                }
            }

            }
            catch (Exception ex)
            {
                ExceptionLogger.SendErrorToText(ex);
            }



What am i missing ? It appears there is some transformation that is occuring as the DataReader loops through records and adds them to details

What I have tried:

I have tried moving the model out of the while loop as below but i still get 1 row instead :
C#
var model = new Flex();
                    while ( dr1.Read())
                    {
                        
                        model.EmailAddress = Convert.ToString(dr1["E_MAIL"]);
                        model.FirstName = Convert.ToString(dr1["FIRST_NAME"]);
                        model.LastName = Convert.ToString(dr1["LAST_NAME"]);
                        model.MobileNo = Convert.ToString(dr1["MOBILE_NUMBER"]);
                        model.CustNo = Convert.ToString(dr1["CUSTOMER_NO"]);
                        details.Add(model);
                    }
Posted
Updated 29-Oct-20 23:35pm

1 solution

Why? Because you told it to.
You create a single instance of the Flex class then add it multiple times to yoru collection, overwriting the previous data each time roudn the loop.

If I simplify what you have done it may be clearer:
C#
int x;
for (i = 0; i < 5; i++)
   {
   x = i;
   }
Console.WriteLine(x);
Would you expect the print to contain "0, 1, 2, 3, 4" or just "4"?

You've done the same thing - move the constructor inside the loop, and it'll add different values:
C#
while ( dr1.Read())
{
    var model = new Flex();
    model.EmailAddress = Convert.ToString(dr1["E_MAIL"]);
    model.FirstName = Convert.ToString(dr1["FIRST_NAME"]);
    model.LastName = Convert.ToString(dr1["LAST_NAME"]);
    model.MobileNo = Convert.ToString(dr1["MOBILE_NUMBER"]);
    model.CustNo = Convert.ToString(dr1["CUSTOMER_NO"]);
    details.Add(model);
}
 
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