Home > Mobile >  My while is only returning the first result
My while is only returning the first result

Time:11-30

I'm using a while to try to show all the rows in the database table that contain the card number equal to the one entered, it's not working, it's just reading the first line.

private static string getExtrato(string query)
{
    using (var cn = new SqlConnection("Data Source=MAD-PC-023\\SQLEXPRESS;Database=bank;Trusted_Connection=True;"))
    {
        cn.Open();
        using (var cmd = new SqlCommand() { Connection = cn, CommandText = query })
        {
            var reader = cmd.ExecuteReader();
            while (reader.Read() == true)
            {
                    if (reader.GetString(1) == null)
                    {
                        return "\n  O cartão nº "   reader.GetString(0)   " levantou: "   reader.GetString(2)   " euros "   " às: "   reader.GetDateTime(3);
                    }
                    else
                    {
                        return "\n  O cartão nº "   reader.GetString(0)   " depositou: "   reader.GetString(1)   " euros "   " às: "   reader.GetDateTime(3);
                    }
            }
            return "";
        }
    }
}

private static string extratoOperacao(string numeroCartao)
{
    return getExtrato($@"SELECT CardNumber, Deposit, Withdraw, DataHora FROM MoveInfo WHERE CardNumber = '{numeroCartao}'");
}

I don't know what I have to change to work

CodePudding user response:

Based on a comment above you indicate that this method should return:

a collection of string values

In that case the return type is clearly incorrect:

private static string getExtrato(string query)

A string is not a collection of string values, in the same way that an apple is not a basket of apples. Return a collection instead:

private static IEnumerable<string> getExtrato(string query)

Then in your method you would:

  1. Define a collection
  2. Aggregate values into that collection
  3. Return the collection

Which might look something like this:

var result = new List<string>();
while (reader.Read() == true)
{
    if (reader.GetString(1) == null)
    {
        result.Add("\n  O cartão nº "   reader.GetString(0)   " levantou: "   reader.GetString(2)   " euros "   " às: "   reader.GetDateTime(3));
    }
    else
    {
        result.Add("\n  O cartão nº "   reader.GetString(0)   " depositou: "   reader.GetString(1)   " euros "   " às: "   reader.GetDateTime(3));
    }
}
return result;

CodePudding user response:

Because in the "if" part you are always returning a value. Let me rewrite the code to clarify your mistake.

        while (reader.Read() == true)
        {
                if (condition is met)
                {
                    return "something";
                }
                else //If condition is not met
                {
                    return "something else"
                }
        }

Maybe you want to remove the "else" part, but -I have not too much experience using C#- you need to close the connection and other things before return a value.

  • Related