0

Hi guys this is one project im working on but i found a problem when in the main, im calling the "SQL" method. Acording to my knowledge it supposed to go read al the array save the info and then pass that info to be used on "main" But when i watch the length of the array it has the exact amount(4) i ask to read(idk if im making a mistake with some code) but when it goes to the main it changes to 1

   public string SQL(string strSQL, string[] Regreso)    
 {
            string strError;
            string strRegistro;
            Array.Resize(ref Regreso, 1);          
            if (gStrCN == "")
            {
         return "No login";
            }
            SqlConnection DB = new SqlConnection();

            try
            { 
                DB.ConnectionString = gStrCN;
                DB.Open();
                SqlCommand cmd = new SqlCommand(strSQL, DB);
                SqlDataReader REGISTRO = cmd.ExecuteReader();
                
                while (REGISTRO.Read())
                {                   
                    strRegistro = "";                   
                    for (int i = 0; i < REGISTRO.FieldCount; i++)
                    {
                        
                        strRegistro = strRegistro + REGISTRO.GetValue(i) + "¨";
                    }
                    
                    Regreso[Regreso.Length - 1] = strRegistro;                    
                    Array.Resize(ref Regreso, Regreso.Length + 1);
                }

                REGISTRO.Close();                
                Array.Resize(ref Regreso, Regreso.Length - 1); // here his size its  4
                return "";
            }
            catch (Exception ex)
            {
                strError = ex.Message;
                return strError;
            }
            finally
            {
                DB.Close();
            }
}
 

The problem its here when i try to run the "Main" and i found that when i call the Variable "Regreso" as paramter it only came with 1 data instead of 4. The rest of the code it does exactly what i want, but i don't know why Regreso only return with 1 value

         static void Main(string[] args)
    {
        string strError;
        string strSQL;
        string strLinea;
        int i;
        string[] Regreso = { " " };

        ClsBD BD = new ClsBD();
    strError = BD.Login("DESKTOP", "PRUEBAS", "ID", "PASS");
    
    if (strError == "")
        {
            strSQL = "select cli_codigo,cli_descripcion,cli_rif from mae_cc_clientes WHERE CLI_CODIGO <='100'";
            strError = BD.SQL(strSQL, Regreso); //<-- here is where only has 1 value
            
        if (strError == "")
            {
                for (i = 0; i < Regreso.Length; i++)
                {
                   strLinea = Regreso[i];
                    MessageBox.Show(strLinea, "Registro: " + i + 1, MessageBoxButtons.OK);
                }
                //MessageBox.Show("XXXX", "Error", MessageBoxButtons.OK);
            }
            else 
            {
                MessageBox.Show("XXXX " + strError, "Error", MessageBoxButtons.OK);
            }
        }
        else
            {
            MessageBox.Show("XXX error: " + strError, "titulo", MessageBoxButtons.OK);
        }
    }
  • 3
    Why are you using `Array.Resize` at all? This code is very hard to read and simply wrong, leaking connections and readers. If you want to add results to a list just do that - create a List and add each result to it. Then return that list – Panagiotis Kanavos Feb 24 '22 at 18:01
  • 1
    *All* tutorials, samples and examples show creating connections and readers in a using block. That ensures the connections and readers are closed as soon as possible. `using` works even in cases where `finally` is skipped – Panagiotis Kanavos Feb 24 '22 at 18:04
  • Another serious problem is returning a string error instead of just letting the exception propagate. This is as unsafe as it gets. Return values are all too easy to ignore. You gain absolutely nothing by returning an error string only to print it. You do make the code a lot more complex though. You could simplify both methods a *lot* if you simply used a single `try/catch` block at the outer level and displayed the error there. Even better, log the entire exception text returned by `Exception.ToString(). The full exception contains any inner exceptions and the stack trace – Panagiotis Kanavos Feb 24 '22 at 18:11
  • The standard "pass by ref/value" duplicate has very detailed explanation of the behavior shown in the code . @PanagiotisKanavos suggestion to use List definitely would solve the problem and be more expected by people reading the code. Side note: please review [mre] guidance on posting code - the code shown in this question contains way more than necessary to demonstrate the problem (which should be just line with `Resize` call inside otherwise empty method) – Alexei Levenkov Feb 24 '22 at 18:12

0 Answers0