Click here to Skip to main content
15,904,416 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys am having a logical error here. Want to add the informaion to database,It catches the duplicate primary key record but still it adds the record anyway

Below is the event for adding the information to database

C#
private void btnInsert_Click(object sender, EventArgs e)
      {

          char placeholder = '-';
          string textStrng;
          string var = txtFName.Text;

          if (txtFName.Text=="")
          {
              MessageBox.Show("Please enter your name.",
             "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
              txtFName.Focus();
              txtFName.BackColor = Color.Moccasin;
          }
          else if (txtPassword.Text != txtRepassword.Text)
          {
              MessageBox.Show("Password missmatch",
              "Error Password", MessageBoxButtons.OK, MessageBoxIcon.Stop);
              txtPassword.Focus();
              txtPassword.BackColor = Color.Moccasin;
              txtRepassword.BackColor = Color.Moccasin;
          }
          else
          {

                   int sequence = 0;


                  try
                  {

                      newVar = var.Remove(3);
                      textStrng = newVar.ToUpper().ToString() + placeholder + 0 + sequence+1;
                      txtUserID.Text = textStrng.ToString();

                      users = new ClsUsers(Convert.ToString(txtFName.Text), Convert.ToString(txtLName.Text), Convert.ToString(txtUserID.Text),
                      Convert.ToString(txtEmail.Text), Convert.ToString(txtUsername.Text), Convert.ToString(txtPassword.Text),
                      Convert.ToString(txtRightsName.Text), Convert.ToString(txtDescription.Text));

                      bl.InsertUser(users);
                      sequence++;
                  }
                  catch (Exception exp)
                  {
                      MessageBox.Show(exp.Message, "Error Message", MessageBoxButtons.OK, MessageBoxIcon.Error);
                      txtFName.Focus();
                      txtFName.BackColor = Color.Moccasin;
                  }


              MessageBox.Show("Added succsessfuly.\n\n" + "UserID: " + txtUserID.Text + "\nName: " + txtFName.Text + "\nSurname: " + txtLName.Text + "\nEmail: " + txtEmail.Text,
                  "Insert", MessageBoxButtons.OK, MessageBoxIcon.Information);

              clearTextBoxes();
              txtFName.Focus();

          }


      }


Below is the method to insert

C#
#region Insert

       public void InsertUser(ClsUsers users)
       {
           using (SqlConnection conn = new SqlConnection(ConnString))
           {
               SqlCommand cmd = new SqlCommand("procInsertUserInfo", conn);
               cmd.CommandType = CommandType.StoredProcedure;

               cmd.Parameters.Add(new SqlParameter("@fName", SqlDbType.NVarChar, 20));
               cmd.Parameters["@fName"].Value = users.FName;

               cmd.Parameters.Add(new SqlParameter("@lname", SqlDbType.NVarChar, 50));
               cmd.Parameters["@lname"].Value = users.SName;

               cmd.Parameters.Add(new SqlParameter("@userID", SqlDbType.NVarChar, 7));
               cmd.Parameters["@userID"].Value = users.UserID;

               cmd.Parameters.Add(new SqlParameter("@email", SqlDbType.NVarChar, 50));
               cmd.Parameters["@email"].Value = users.Email;

               cmd.Parameters.Add(new SqlParameter("@username", SqlDbType.NVarChar, 20));
               cmd.Parameters["@username"].Value = users.Username;

               cmd.Parameters.Add(new SqlParameter("@password", SqlDbType.NVarChar, 25));
               cmd.Parameters["@password"].Value = users.Password;

               cmd.Parameters.Add(new SqlParameter("@name", SqlDbType.NVarChar, 20));
               cmd.Parameters["@name"].Value = users.RightName;

               cmd.Parameters.Add(new SqlParameter("@desc", SqlDbType.NVarChar, 100));
               cmd.Parameters["@desc"].Value = users.Desc;


               try
               {

                   conn.Open();
                   cmd.ExecuteNonQuery();
               }
               catch (Exception exp)
               {
                   throw new ApplicationException(exp.Message);
               }
           }//End using
       }//End Method
Posted
Updated 26-Mar-11 5:23am
v2
Comments
MarqW 26-Mar-11 11:09am    
The code you've provided isn't very helpful. You'd need to post the code to bl.InsertUser() instead.
Your code listed does not even check for existing users before calling bl.InsertUser

I dont think your error would be a duplicate primary key error - if that error occurs, there is no way a record could be inserted into an SQL Server database.

Run through your code using debug points and perhaps you will get to the root cause of the error.
 
Share this answer
 
v2
Comments
Аslam Iqbal 26-Mar-11 13:45pm    
If it is true, his table doesn't have any primary key. my 5
C#
private void button1_Click(object sender, EventArgs e)
        {


                        if (txtuname.Text.Equals(""))
            {
                MessageBox.Show("Please Enter User Name", "User Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtuname.Focus();
                return;
            }
            if (txtpword.Text.Equals(""))
            {
                MessageBox.Show("Please Enter Password", "User Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtpword.Focus();
                return;
            }
            if (txtcpassword.Text.Equals(""))
            {
                MessageBox.Show("Please Enter Confirm Password", "User Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtcpassword.Focus();
                return;
            }
            if (txtpword.Text != txtcpassword.Text)
            {
                MessageBox.Show("Please Check Confirm Password", "User Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtcpassword.Focus();
                return;
            }
            //if (cbentry.Checked == false && cbview.Checked == false && cbsearch.Checked == false && cbreport.Checked == false && cbadmin.Checked == false)
            //{
            //    MessageBox.Show("Please Set User Privilage", "User Creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
            //    cbentry.Focus();
            //    return;
            //}
            sq = "select uname from admin where uname='" + txtuname.Text + "' ";
            cmd = new SqlCommand(sq, con);
            rd = cmd.ExecuteReader();
            if (rd.Read() == true)
            {
                rd.Close();
                MessageBox.Show("Already Exist This User", "User creation", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else if (rd.Read() != true)
            {
                rd.Close();
                sq = "insert into admin values('" + txtuname.Text + "','" + txtpword.Text + "')";
                cmd = new SqlCommand(sq, con);
                cmd.ExecuteNonQuery();
                MessageBox.Show("User Saved", "User craetion", MessageBoxButtons.OK, MessageBoxIcon.Information);
                txtuname.Focus();
                txtcpassword.Text = "";
                txtpword.Text = "";
                txtuname.Text = "";
            }

        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900