Good morning,
I am working as Windows Forms and user controls within C #, which I am working with an application that acts as a clock watch.
This first screen acts, like the main screen. Which all users have access to now.
Inside the login, they write their username and when pressing register they must make a comparison with the registered users within the DB.
The code I use is the following:
private void button1_Click(object sender, EventArgs e)
{
SqlCommand agregar2 = new SqlCommand(string.Format("Select Usuario From Usuarios where Usuario = '"+textBox1.Text+"'", cadena));
if ( textBox1.Text == agregar2.CommandText)
{
SqlCommand agregar = new SqlCommand("insert into Registros_ values (@nombre, @Fecha, @Hora)", cadena);
cadena.Open();
try
{
label2.Text = DateTime.Now.ToString("yyyy-MM-dd");
label1.Text = DateTime.Now.ToString("HH:mm:ss");
agregar.Parameters.AddWithValue("@nombre", textBox1.Text);
agregar.Parameters.AddWithValue("@fecha", label2.Text);
agregar.Parameters.AddWithValue("@Hora", label1.Text);
agregar.ExecuteNonQuery();
MessageBox.Show("Correcto");
}
catch (Exception ex)
{
MessageBox.Show("No se pudo insertar" + ex);
}
finally
{
textBox1.Clear();
cadena.Close();
}
}
else
{
MessageBox.Show("No existe en la base de datos o Usuario Incorrecto");
MessageBox.Show(agregar2.ToString());
MessageBox.Show(textBox1.Text);
}
}
At the moment of making the query, it takes all the value of the query:
How can I just take the value of: DaniLop21, because apparently I'm taking the whole string "Select User From Users where User = 'DaniLop21'" and when comparing it obviously does not coindise with the value of Textbox1 = DaniLop21 .
Greetings and thanks.