Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
DATA layer code method
C#
public int AddUsers(Users objUser)
       {
           SqlConnection con = new SqlConnection(ConnString);
           con.Open();
           SqlCommand cmd = new SqlCommand("SpCreateUser", con);
           cmd.CommandType = CommandType.StoredProcedure;
           try
           {
               cmd.Parameters.AddWithValue("@Full_name", objUser.fullname);
               cmd.Parameters.AddWithValue("@Dob", objUser.DOB);
               cmd.Parameters.AddWithValue("@email", objUser.Email);
               cmd.Parameters.AddWithValue("@phoneno", objUser.Phoneno);
               cmd.Parameters.AddWithValue("@loginid", objUser.loginId);
               cmd.Parameters.AddWithValue("@pwd", objUser.Password);
               cmd.ExecuteNonQuery();

               int totalrecordscreated = (int)cmd.ExecuteNonQuery();
               return totalrecordscreated;
           }
           catch (Exception ex)
           {
               throw ex;
           }
           finally
           {
               con.Close();
           }
       }


Business layer Method :
C#
public int AddUsers(Users objUser)
        {
            DataLayer objUserDAL = new DataLayer();
            try
            {              
                return objUserDAL.AddUsers(objUser);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                //objUser = null;
            }
        }


UI layer- register.aspx.cs
C#
protected void btnRegsubmit_Click(object sender, EventArgs e)
{
   Users objUser = new Users();
   objUser.fullname = tbfullname.Text;
   objUser.DOB = tbdob.Text;
   objUser.Email = tbemail.Text;
   objUser.Phoneno = tbphoneno.Text;
   objUser.loginId = tbloginid.Text;
   objUser.Password = tbpwd.Text;

   BAL objBAL = new BAL();

   int records_no = objBAL.AddUsers(objUser);
   Response.Write("No of created records " + records_no.ToString());

}


data:
SQL
Trans_id	Date	Person_id	Payee_details	Amount	Expense_Type
20	2018-08-27	admin	food panda	254	Food
21	2018-08-27	admin	VRl	600	Travel
22	2018-08-14	sam	rewgrg	564	Food
23	2018-08-14	sam	rewgrg	564	Food


having interfaces for DAL and BLL layers. after submitting the data records are inserting two times(duplication.) What logic is wrong here?

What I have tried:

Help me since i am new to coding, not understanding what logic is creating this problem
Posted
Updated 24-Aug-18 6:34am
Comments
Vincent Maverick Durano 24-Aug-18 10:27am    
Did you hit browser refresh or perhaps clicked the button twice? Have you tried debugging your code and set a break point at Button Click's event?
Virendra S from Bangalore, Karnataka 27-Aug-18 6:13am    
No i was running cmd.ExecuteNonQuery(); twice in code, now correctd
[no name] 24-Aug-18 12:35pm    
Why did you ignore the stored procedure (SpCreateUser)?

"One" duplicate does not constitute a "pattern"; you need to do more testing.

1 solution

You are running the query twice, replace this


C#
//first insert
cmd.ExecuteNonQuery();

 //second insert
int totalrecordscreated = (int)cmd.ExecuteNonQuery();
return totalrecordscreated;


For this
C#
int numberOfRecords = cmd.ExecuteNonQuery();
 
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