Share via

c# returning list issue

mion shion 241 Reputation points
2022-04-25T04:32:57.647+00:00

Morning All thanks for reading,

ok i cant seem to find out why this is returning just 1 result when it goes though the while loop 3 times as its got 3 values in the database and it does return the last value but trying to find out how to loop through and add all the items to the list then at the end return the list code i have,

       public static List<transactiondata> trans { get; set; }





        public void gettransactions()
        {



            try
            {
                builder.Add("Provider", "Microsoft.Jet.OLEDB.4.0");
                builder.Add("Data Source", @"C:\Retcon\ARCHIVE\TR20201105");
                builder.Add("Persist Security Info", "False");
                builder.Add("Extended properties", "Paradox 7.x; HDR=YES");
                connection = new OleDbConnection(builder.ToString());
                command = new OleDbCommand("SELECT DISTINCT TILL, RECTYPE, RECEIPT, OPID, NAME, CODE, QTY, VALUE, CARDNUM, TYPE,  DATE, TIME from TR01201105082227", connection); // we start by hacking into the generic sql code and force certin lines ( genric get all SELECT * FROM SHIFTEXPORT ) 
                //reading
                connection.Open();
                using (var reader = command.ExecuteReader())
                    if (reader.HasRows)
                        while (reader.Read())


                            transactions.trans = GetTransactiondata(reader.GetValue(0).ToString(), reader.GetValue(1).ToString(), reader.GetValue(2).ToString(), reader.GetValue(3).ToString(), reader.GetValue(4).ToString(), reader.GetValue(5).ToString(), reader.GetValue(6).ToString(), reader.GetValue(7).ToString(), reader.GetValue(8).ToString(), reader.GetValue(9).ToString(), reader.GetValue(10).ToString(), reader.GetValue(11).ToString());
                            //GetTransactiondata(reader.GetValue(0).ToString(), reader.GetValue(1).ToString(), reader.GetValue(2).ToString(), reader.GetValue(3).ToString(), reader.GetValue(4).ToString(), reader.GetValue(5).ToString(), reader.GetValue(6).ToString(), reader.GetValue(7).ToString(), reader.GetValue(8).ToString(), reader.GetValue(9).ToString(), reader.GetValue(10).ToString(), reader.GetValue(11).ToString());

                //writing
                //command = new OleDbCommand("DELETE * FROM Planes", connection);
                //command.ExecuteNonQuery();
                //connection.Close();
            }
            catch (Exception ex)
            {
                var connection = new OleDbConnection();
                connection.Close();
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }


        public List<transactiondata> GetTransactiondata(string till , string rectype , string receipt, string opid , string name , string code , string qty , string value , string cardnum , string type , string date , string time)
        {
            var list = new List<transactiondata>();
            list.Add(new transactiondata()
            {
                Till = till ,
                rectype = rectype ,
                receipt = receipt ,
                opid = opid ,
                name = name ,
                code = code ,
                qty = qty ,
                value = value ,
                cardnum = cardnum ,
                type = type ,
                date = date ,
                time = time ,


            });
            return list;
        }

so it will go though the while loop 3 times but each time it seems to overite the values added to the list and only return 1 set of values in the list

any help would be much appreiated

Developer technologies | VB
0 comments No comments

Answer accepted by question author

Viorel 127K Reputation points
2022-04-25T05:24:09.333+00:00

Try something like this:

 using (var reader = command.ExecuteReader())
   while (reader.Read())
      transactions.trans.Add( GetTransactiondata(reader.GetValue(0).ToString( ) . . . . ) );

. . . .

transactiondata GetTransactiondata(string till , string rectype , string receipt, string opid , string name , string code , string qty , string value , string cardnum , string type , string date , string time)
{
    return new transactiondata
         {
             Till = till ,
             rectype = rectype ,
             receipt = receipt ,
             opid = opid ,
             name = name ,
             code = code ,
             qty = qty ,
             value = value ,
             cardnum = cardnum ,
             type = type ,
             date = date ,
             time = time ,
         };
}

Maybe you can use numbers instead of some strings.

Was this answer helpful?

1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.