When clicking the check button if the value of the textbox contains in a table column

1

How can I make a textbox check if it contains this value inside the table in the DataBase column when I click the button?

I made this code, but I did not succeed:

SqlConnection conn = new SqlConnection(WebConfigurationManager.ConnectionStrings["conString"].ConnectionString);

 private void button2_Click(object sender, EventArgs e)
    {
            try
        {
            SqlCommand cmd = new SqlCommand($"SELECT * FROM Alunos WHERE Numero ={txtNome.Text.Text}", conn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            int j = ds.Tables[0].Rows.Count;
            if (j == 0)
            {
                MessageBox.Show("word" + txtNome.Text + "already exists!");
                ds.Clear();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
}
    
asked by anonymous 25.09.2017 / 13:35

1 answer

1

The problem in the code is in your count

int j = ds.Tables[0].Rows.Count;
if (j == 0)
{
  MessageBox.Show("word" + txtNome.Text + "already exists!");
  ds.Clear();
}

This means that the message will be displayed only if the number of values is 0, that is, if it does not exist.

Just change

if (j == 0)

for

if (j > 0)

So if any value exists (more than one line), the message will be displayed!

    
26.09.2017 / 14:01