0

I am making a login application. But when I create a new user in my application I get an error when the user already exists in the database. So I want to get my label lblOkToe an error message that the user already exists.

Here is the code that I am using:

    private void btnVoegToe_Click(object sender, EventArgs e)
    {
        // Velden leegmaken

        string Wacht = txtWacht.Text;
        string Login = txtLogin.Text;

        //Parameters Declareren
        SqlParameter Para1 = new SqlParameter();
        SqlParameter Para2 = new SqlParameter();
        Para1.ParameterName = "@Log";
        Para2.ParameterName = "@WW";
        Para1.Value = Login;
        Para2.Value = Wacht;


        // SQL Connectie opzetten
        SqlConnection Conn = new SqlConnection();
        Conn.ConnectionString = @"Integrated Security=true;Initial Catalog=Wachtwoord;Data Source=LAPTOP-PDI9B3LP\SCHOOL";
        Conn.Open();

        SqlCommand cmd = new SqlCommand();
        cmd.Connection = Conn;

        // Alles selecteren van tabel Favorieten
        cmd.CommandText = "insert into gebruik values (@Log, @WW)";

        //Koppelen van SqlParameter met SqlCommand
        cmd.Parameters.Add(Para1);
        cmd.Parameters.Add(Para2);

        int gelukt = cmd.ExecuteNonQuery();

        if (gelukt == 1 && )
        {
            lblOKToe.Text = "De gebruiker" + Login + " is toegevoegd";
            cbKeuze.Items.Add(Login);
            txtLogin.Text = "";
            txtWacht.Text = "";
        }
        // Database connectie sluiten
        Conn.Close();
    }
Umang Loriya
  • 840
  • 8
  • 15
jantje123
  • 1
  • 1
  • 1
    Tip: First run a _SELECT_ query with the passed-on parameters. If a row is returned, then it means the user exists and show warning, else do _INSERT_. – boop_the_snoot Sep 25 '17 at 07:13
  • 1
    Unrelated: You probably don't want to do all this on the GUI-Thread. It will make your App unresponsive if DB I/O should take a little longer. – Fildor Sep 25 '17 at 07:14
  • 1
    Just use try.. catch. A separate select for existence check doesn't guarantee insert will not fail in a multyuser environment. – Serg Sep 25 '17 at 07:16
  • 1
    ALWAYS use [using(...)](https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/using-statement) for types that implement the [IDisposable](https://msdn.microsoft.com/en-us/library/system.idisposable(v=vs.110).aspx) interface – user2321864 Sep 25 '17 at 07:18

1 Answers1

0

It depends on the return result from the dB:

int gelukt = cmd.ExecuteNonQuery();

If gelukt is 1 then the Affected Records is 1 and the Record has been inserted successfully.

If gelukt is 0 then the SQL INSERT failed. Most likely because you have unique constraints or something like that.

So the code would be:

if (gelukt == 1) 
  { 
   lblOKToe.Text = "De gebruiker" + Login + " is toegevoegd"; 
   cbKeuze.Items.Add(Login); 
   txtLogin.Text = ""; 
   txtWacht.Text = ""; 
  }
else //if (gelukt == 0)
  {
    lblOKToe.Text = "Failed to register, use another name.";
  }
Jeremy Thompson
  • 61,933
  • 36
  • 195
  • 321