-3

I'm pretty new in C# and i couldn't find an answer for this. I'm getting the values of a column named NUMERO_CTA from my DB in an array within valores variable. But I have to compare this array of values with a textbox to check if one of the values contained in valores matches with the value of the textbox named txtCuentaDestino. How can I compare a textbox.text with an array?. Thanks in advanced! Here's my code:

 DataTable tbl = ds.Tables[0];
     for (int i = 0; i < tbl.Rows.Count; i++)
     {
        DataRow myRow = tbl.Rows[i];
        valores = new string[] {myRow["NUMERO_CTA"].ToString()};  
     }

     if (ds.Tables[0].Rows.Count == 0)
     {
        GuardaCuenta();
        return false;
     }
     else if (txtCuentaDestino.Text == resultado)
     {
        return true;
     }
     else
     {
        return false;
     }
  • i can't understand anything, every variable is different. – Abdullah Aug 21 '15 at 21:29
  • 5
    I really don't understand this `i couldn't find an answer for this`. Every newcomer thinks his problem is unique. No it has been asked millions of times before. – Eser Aug 21 '15 at 21:31
  • At first: try to translate you variable names into english, as it's pretty hard for non-native speakers of your language to understand your thoughts in variable naming. Second: where do you assign "resultado"? Third: Why do you make an if-statement in your last 7 rows, although a simple `else return (txtCuentaDestino.Text == resultado);` would suffice? – St0fF Aug 21 '15 at 21:44
  • @Eser - it's possible the OP doesn't know the terms to use for searching. i'm sure it's much easier to search for "find string in array of strings in c#" versus posting a question here. i tend to give the benefit of the doubt because i, too, have been in the position where i didn't know the proper terms. – devlin carnate Aug 21 '15 at 21:44
  • @devlincarnate just google your search string "find string in array of strings in c#" and open the first link. My point is, poeple stopped using their brains and searches for answers with an exact fit. – Eser Aug 21 '15 at 21:49
  • @Eser : and my point is that some people don't know they should search for "find string in array of strings in c#". terminology leads one to answers, but if you don't yet know the terms, it may be difficult to know the right words to use. – devlin carnate Aug 21 '15 at 22:06
  • possible duplicate of [Using C# to check if string contains a string in string array](http://stackoverflow.com/questions/2912476/using-c-sharp-to-check-if-string-contains-a-string-in-string-array) – h3n Aug 21 '15 at 22:59

3 Answers3

3

You can't compare an array of strings and a string. You want to compare the string from the textbox to each string in your array, one at a time.

In this case if you want to check if there is a string in the array which matches your textbox string you can use the linq method Contains.

Ex.

if (arrayOfStrings.Contains(singleString))
{
    // Do something
}
jwde
  • 642
  • 4
  • 13
1

you need to iterate over the valores array and make the comparison.

foreach(string s in valores) 
{
    if(s == txtCuentaDestino.Text)
    {
       //do something magical
    }
}
devlin carnate
  • 8,309
  • 7
  • 48
  • 82
1

Thanks for the answers:

Both helped me a lot! Here is the result:

    for (int i = 0; i < tbl.Rows.Count; i++)
        {
            DataRow myRow = tbl.Rows[i];
            valores = new string[] { myRow["NUMERO_CTA"].ToString() };

            foreach (string x in valores)
            {
                if (x.Contains(cuentaDestino))
                {
                    f_Script("alerta", "<script>alert('Cuenta a crear ya Existe.');window.location.href = 'RW_CuentasBancarias.aspx';</script>");
                    contador = 1;
                }
            }

            if (contador == 1)
            {
                break;
            }
        }

        if(contador != 1){
        GuardaCuenta();
        f_Script("alerta", "<script>alert('Su cuenta ha sido creada.');window.location.href = 'RW_Solicitud_Reembolso.aspx';</script>");
        }