1
How can I check the textbox if it contains this value within the table in the Database column when clicking the button?
I made this code, but I didn’t 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);
}
}
You tested the code?
– Francisco
The code is incomplete, can you give us more information? Your question is in the query? if yes, you can tell us more about its structure?
– Luiz Santos
I put the whole code.
– user92401
@Viniciusbittencourt The Code showed some error?
– Leo Longhi
Nothing happens when I click the button.
– user92401
I put a value I didn’t have in the Column and Messagebox appeared. How do I do it backwards?
– user92401
You’re not opening your connection (
conn.Open();
) and your check is reversed. You have to check if there is any word, ie if therowcount
is greater than 0.– Sorack