Click here to Skip to main content
15,881,866 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello friends,
Just look at code i have written
UserBLL
public class UserBLL
{
	public UserBLL()
	{
		
	}
    public Int32 saveUserDetails(clsUser objUser)
    {
        UserDAO objADO = new UserDAO();
        return objADO.saveUserDetails(objUser);
    }
}


UserDAO
    public int saveUserDetails(clsUser objUser)
    {
        SqlCommand objCmd = new SqlCommand("usp_InsertNewUser");
        object result;
        try
        {
            objCmd.Connection = objCon;
            objCmd.CommandType = CommandType.StoredProcedure;
            objCmd.Parameters.AddWithValue("@username", objUser.username.ToString());
            objCmd.Parameters.AddWithValue("@pwd", objUser.pwd.ToString());
            objCmd.Parameters.AddWithValue("@fname", objUser.firstname.ToString());
            objCmd.Parameters.AddWithValue("@lname", objUser.lastname.ToString());
            objCmd.Parameters.AddWithValue("@contact", objUser.contact.ToString());
            
            if (objCon.State == ConnectionState.Closed)
            {
                objCon.Open();
            }
            result = objCmd.ExecuteScalar();
        }
        catch (Exception ex)
        {
            return -1;
        }
        finally
        {
            objCmd.Dispose();
        }
        return Convert.ToInt32(result);
    }


Thing is that, every thing going fine but records are added twice. Any idea where I'm going wrong. .??

Is this line be problem
return objADO.saveUserDetails(objUser);
Posted
Updated 31-Mar-11 21:35pm
v2
Comments
Wild-Programmer 1-Apr-11 3:44am    
Strange, seems like the function is getting called from somewhere else too.
Albin Abel 1-Apr-11 3:45am    
Doesn't seems any error here. May be your stored procedure?
dhage.prashant01 1-Apr-11 3:48am    
wait it was error in code behind file, I have used below code

UserBLL objUserBLL = new UserBLL();
if (objUserBLL.saveUserDetails(objUser) < 0)
{
}

Can anyone tell why objUserBLL.saveUserDetails(objUser) was getting twice?
sekharkaza 1-Apr-11 4:42am    
Prashant try checking for postback , it generally happens when (!IsPostBack) is not checked for.
Wild-Programmer 1-Apr-11 4:22am    
Maybe it is celebrating April fools day today :-D thats why got executed twice.

1 solution

Where are you calling objUserBLL.saveUserDetails(objUser) if it is in Page_Load then you need to check IsPostBack to determine if the call is the first page load or a postback.

Other than checking where objUserBLL.saveUserDetails(objUser) is being called there are a couple of other things you can do as usernames should be unique;
1) In your stored procedure usp_InsertNewUser do a check on the username and only insert the information if the username does not already exist in the database.
2) Place a unique constraint on the username field within the database.
 
Share this answer
 
Comments
dhage.prashant01 5-Apr-11 3:06am    
I had written that code in Wizard1_NextButtonClick event
Now i have update code as follows
Int32 result = objUserBLL.saveUserDetails(objUser);
if ( result < 0)
{
}
Now it works fine, but was trying to know y in that condition 2 records was inserted.

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