简体   繁体   中英

Issue with SQL Server database in Registration form Visual Studio

This is my code:

 private void button1_Click(object sender, EventArgs e)
 {
            SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Dido\Documents\DataCars.mdf;Integrated Security=True;Connect Timeout=30");
            SqlCommand cmd = new SqlCommand("insert into Login values('" + textBox1.Text + "','" + textBox2.Text + "')", con);

            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();

            if (!String.IsNullOrEmpty(textBox1.Text) || !String.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("Your registration was successfull!");
                Login frm1 = new Login();
                Global.GlobalVar = textBox1.Text;
                frm1.Show();
                this.Hide();
            }
            else
            {
                MessageBox.Show("Please insert some text...");
            }
        }

When I'm trying to register an user with username and password - it said it is successful, but the only thing that is added in the database is an empty row. When I just click on "Register" button, without writing something, the whole thing breaks up and this error comes:

Violation of PRIMARY KEY constraint 'PK_ Login _536C85E5BD4FE4C6'. Cannot insert duplicate key in object 'dbo.Login'. The duplicate key value is ().

Problem : you are only checking for null and Empty but You are not checking for Whitespace .

Solution : you need to also check for Whitespace . if you use String.IsNullOrWhiteSPace() it will check for Null,Empty and Whitespace.

Try This:

if(!String.IsNullOrWhiteSpace(textBox1.Text) || !String.IsNullOrWhiteSpace(textBox2.Text))

Suggestion :

the best way to confirm wether INSERT is successfull or not is checking the return value of the ExecuteNonQuery() method.

int Status=cmd.ExecuteNonQuery();
if(STatus>0)
MessageBox.Show("Your registration was successfull!");
else
MessageBox.Show("Please insert some text...");

Complete Code:

private void button1_Click(object sender, EventArgs e)
{
     if(!String.IsNullOrWhiteSpace(textBox1.Text) || !String.IsNullOrWhiteSpace(textBox2.Text))
     {
        SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Dido\Documents\DataCars.mdf;Integrated Security=True;Connect Timeout=30");
        SqlCommand cmd = new SqlCommand("insert into Login values('" + textBox1.Text + "','" + textBox2.Text + "')", con);

        con.Open();
        int Status=cmd.ExecuteNonQuery();
        con.Close();
        if(Status>0)
        {
            MessageBox.Show("Your registration was successfull!");
            Login frm1 = new Login();
            Global.GlobalVar = textBox1.Text;
            frm1.Show();
            this.Hide();
        }
        else
        {
            MessageBox.Show("no records updated!");
        }
      }
      else
      {
            MessageBox.Show("Please insert some text...");
      }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM