Click here to Skip to main content
15,868,016 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
HI,
i have designed an application to enter all the details of employee. now trying to update the user infroamtion this is my save button code.

C#
protected void btnsave_Click(object sender, EventArgs e)
    {
        string id = Request.QueryString["id"].ToString();
        ADDInfoBLL objBLL = new ADDInfoBLL();
        AddInfoModel obj = new AddInfoModel();
        obj.FirstName = FirstName.Text.Trim();
        obj.LastName = LastName.Text.Trim();
        obj.ReferenceName1 = ReferenceName1.Text.Trim();
        obj.ReferenceName2 = ReferenceName2.Text.Trim();
        obj.Email1 = Email1.Text.Trim();
        obj.TaxTerms = TaxTerms.Text.Trim();
        obj.StartDate = Convert.ToDateTime(StartDate.Text.Trim());
        obj.EndDate = Convert.ToDateTime(EndDate.Text.Trim());
        obj.ResourceManager = ResourceManager.Text.Trim();
        obj.Customer = Customer.Text.Trim();
        obj.CustomerManagerLastName = CustomerManagerLastName.Text.Trim();
        obj.CustomerManagerFirstName = CustomerManagerFirstName.Text.Trim();
        obj.CustomerManagerEmail = CustomerManagerEmail.Text.Trim();
        obj.CustomerManagerPhone = Convert.ToInt32(CustomerManagerPhone.Text.Trim());
        obj.Password = Password.Text;
        obj.Role = Role.Text;
        obj.Email = Email.Text;
        obj.Phone = Convert.ToInt32(Phone.Text);
        obj.PrimarySkill = primaryskill.Text;
        obj.SecondarySkill = secondaryskill.Text;

        if (C2CDiv.Visible == true)
        {
            obj.C2CComapanyName = C2CCompanyName.Text.Trim();
            obj.C2CContactFirstName = C2CContactFirstName.Text.Trim();
            obj.C2CContactLastName = C2CContactLastName.Text.Trim();
            obj.C2CDuns = C2CDuns.Text.Trim();
            obj.C2CEmail = C2CEmail.Text.Trim();
            obj.C2CPayRate = Convert.ToInt32(C2CPayRate.Text.Trim());
            obj.C2CPhone = Convert.ToInt32(C2CPhone.Text.Trim());
            obj.TaxTerm1099 = string.Empty;
            obj.TaxTerm1099PayRate = 0;
        }
        else if (TaxTermsDiv.Visible == true)
        {
            obj.TaxTerm1099 = RBLTaxTerms.SelectedItem.Text.Trim();
            obj.TaxTerm1099PayRate = Convert.ToInt32(Taxterms1099PayRate.Text.Trim());
            obj.C2CComapanyName = string.Empty;
            obj.C2CContactFirstName = string.Empty;
            obj.C2CContactLastName = string.Empty;
            obj.C2CDuns = string.Empty;
            obj.C2CEmail = string.Empty;
            obj.C2CPayRate = 0;
            obj.C2CPhone = 0;
        }
        else
        {
            obj.C2CComapanyName = string.Empty;
            obj.C2CContactFirstName = string.Empty;
            obj.C2CContactLastName = string.Empty;
            obj.C2CDuns = string.Empty;
            obj.C2CEmail = string.Empty;
            obj.C2CPayRate = 0;
            obj.C2CPhone = 0;
            obj.TaxTerm1099 = string.Empty;
            obj.TaxTerm1099PayRate = 0;


        }

        obj.id =Convert.ToInt32(id);

        objBLL.UserDetailsBLL(obj);
    }
}


BLL:
C#
public class UpdateUserBLL
    {
        UpdateUserDAL objupdateDAL = new UpdateUserDAL();
        public SqlDataReader updateBLL(AddInfoModel update)
        {
            return objupdateDAL.updateDAL(update);
        }
        public int UpdateinfoBLL(AddInfoModel information)
        {
            return objupdateDAL.UpdateinfoDAL(information);
        }
    }
}


DAL:
C#
public  class UpdateUserDAL
    {
       public SqlDataReader updateDAL(AddInfoModel update)
         {
           try
           {
               SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString);
               con.Open();
               SqlCommand cmd = new SqlCommand("Select * FROM Resource where id="+ update.id  + "",con);
               cmd.Parameters.Add("@ID", SqlDbType.Int).Value = Convert.ToInt32(update.id);
               SqlDataReader reader = cmd.ExecuteReader();
               return reader;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }
       public int UpdateinfoDAL(AddInfoModel information)
       {
           SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString);
           con.Open();
           SqlCommand cmd;
           {
               int id;
               try
               {
                   if (con.State == ConnectionState.Open)
                       con.Close();
                   cmd = new SqlCommand("Resource_sp", con);
                   con.Open();
                   cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = information.FirstName;
                   cmd.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = information.LastName;
                   cmd.Parameters.Add("@Password", SqlDbType.NVarChar).Value = information.Password;
                   cmd.Parameters.Add("@Email", SqlDbType.NVarChar).Value = information.Email;
                   cmd.Parameters.Add("@Phone", SqlDbType.Int).Value = Convert.ToInt32(information.Phone);
                   cmd.Parameters.Add("@Role", SqlDbType.NVarChar).Value = information.Role;
                   cmd.Parameters.Add("@PrimarySkill", SqlDbType.NVarChar).Value = information.PrimarySkill;
                   cmd.Parameters.Add("@SecondarySkill", SqlDbType.NVarChar).Value = information.SecondarySkill;
                   cmd.Parameters.Add("@TaxTerms", SqlDbType.NVarChar).Value = information.TaxTerms;
                   cmd.Parameters.Add("@ResourceManager", SqlDbType.NVarChar).Value = information.ResourceManager;
                   cmd.Parameters.Add("@Customer", SqlDbType.NVarChar).Value = information.Customer;
                   cmd.Parameters.Add("@StartDate", SqlDbType.DateTime).Value = information.StartDate;
                   cmd.Parameters.Add("@id", SqlDbType.Int).Value = information.id;
                   cmd.Parameters.Add("@ReferenceName1", SqlDbType.NVarChar).Value = information.ReferenceName1;
                   cmd.Parameters.Add("@ReferenceName2", SqlDbType.NVarChar).Value = information.ReferenceName2;
                   cmd.Parameters.Add("@Email1", SqlDbType.NVarChar).Value = information.Email1;
                   cmd.Parameters.Add("@Email2", SqlDbType.NVarChar).Value = information.Email2;
                   cmd.Parameters.Add("@C2CCompanyName", SqlDbType.NVarChar).Value = information.C2CComapanyName;
                   cmd.Parameters.Add("@C2CContactLastname", SqlDbType.NVarChar).Value = information.C2CContactLastName;
                   cmd.Parameters.Add("@C2CContactFirstName", SqlDbType.NVarChar).Value = information.C2CContactFirstName;
                   cmd.Parameters.Add("@C2CEmail", SqlDbType.NVarChar).Value = information.C2CEmail;
                   cmd.Parameters.Add("@C2CPhone", SqlDbType.NVarChar).Value = information.C2CPhone;
                   cmd.Parameters.Add("@C2CDuns", SqlDbType.NVarChar).Value = information.C2CDuns;
                   cmd.Parameters.Add("@C2CPayRate", SqlDbType.Int).Value = information.C2CPayRate;
                   cmd.Parameters.Add("@TaxTerm1099", SqlDbType.NVarChar).Value = information.TaxTerm1099;
                   cmd.Parameters.Add("@TaxTerm1099PayRate", SqlDbType.NVarChar).Value = information.TaxTerm1099PayRate;
                   cmd.Parameters.Add("@EndDate", SqlDbType.DateTime).Value = information.EndDate;
                   cmd.Parameters.Add("@CustomerManagerFirstName", SqlDbType.NVarChar).Value = information.CustomerManagerFirstName;
                   cmd.Parameters.Add("@CustomerManagerLastName", SqlDbType.NVarChar).Value = information.CustomerManagerLastName;
                   cmd.Parameters.Add("@CustomerManagerPhone", SqlDbType.NVarChar).Value = information.CustomerManagerPhone;
                   cmd.Parameters.Add("@CustomerManagerEmail", SqlDbType.NVarChar).Value = information.CustomerManagerEmail;
                   cmd.CommandType = CommandType.StoredProcedure;

                   SqlParameter result = new SqlParameter("@result", SqlDbType.Int);
                   result.Direction = ParameterDirection.Output;
                   cmd.Parameters.Add(result);
                   SqlParameter ErrorMessage = new SqlParameter("@ErrorMsg", SqlDbType.VarChar, 500);
                   ErrorMessage.Direction = ParameterDirection.Output;
                   cmd.Parameters.Add(ErrorMessage);


                   cmd.ExecuteNonQuery();

                   id = Convert.ToInt32(result.Value.ToString());
               }
               catch (Exception ex)
               {
                   throw ex;
               }
               finally
               {
                   con.Close();
                   con.Dispose();
               }

               return id;
           }
       }
    }


Stored Procedure :

ALTER PROCEDURE [dbo].[Resource_sp]
@ID int=0, 
@OrganisationID int=0,
@FirstName varchar(50),
@LastName varchar(50),
@Password varchar(50),
@PrimarySkill varchar(50)=null,
@SecondarySkill varchar(50)=null,
@Email varchar(50),
@Phone int=0,
@Role varchar(50),
@ResourceManager varchar(50)=null,
@Customer varchar(50)=null,
@StartDate DateTime=null,
@TaxTerms varchar(50)=null,
@ReferenceName1 varchar(50)=null,
@ReferenceName2 varchar(50)=null,
@Email1 varchar(50)=null,
@Email2 varchar(50)=null,
@C2CCompanyName varchar(50)=null,
@C2CContactLastName varchar(50)=null,
@C2CContactFirstName varchar(50)=null,
@C2CEmail varchar(50)=null,
@C2CPhone int=null,
@C2CDuns varchar(50)=null,
@C2CPayRate int=null,
@EndDate DateTime=null,
@TaxTerm1099 varchar(50)=null, 
@TaxTerm1099PayRate int=null,
@CustomerManagerFirstName varchar(50)=null,
@CustomerManagerLastName varchar(50)=null,
@CustomerManagerPhone int=null,
@CustomerManagerEmail varchar(50)=null,
@result int OUTPUT,
@ErrorMsg varchar(500) OUTPUT

AS

BEGIN
	SET @result = 0
if @ID<>0 
Begin
	Update Resource set
	FirstName=@FirstName,
	LastName=@LastName ,
	Password=@Password,
	PrimarySkill=@PrimarySkill ,
	SecondarySkill=@SecondarySkill,
	Email=@Email,
	Phone=@Phone,
	Role=@Role,
	ResourceManager=@ResourceManager,
	Customer=@Customer,
	StartDate=@StartDate,
	TaxTerms=@TaxTerms,
    ReferenceName1=@ReferenceName1,
    ReferenceName2=@ReferenceName2,
    Email1=@Email1,
    Email2=@Email2,
    C2CCompanyName=@C2CCompanyName,
    C2CContactLastName=@C2CContactLastName,
    C2CContactFirstName=@C2CContactFirstName,
    C2CEmail=@C2CEmail,
    C2CPhone=@C2CPhone,
    C2CDuns=@C2CDuns,
    C2CPayRate=@C2CPayRate,
    EndDate=@EndDate,
TaxTerm1099=@TaxTerm1099,
 TaxTerm1099PayRate=@TaxTerm1099PayRate,
    CustomerManagerFirstName=@CustomerManagerFirstName,
    CustomerManagerLastName=@CustomerManagerLastName,
    CustomerManagerPhone=@CustomerManagerPhone,
    CustomerManagerEmail=@CustomerManagerEmail 
	 where [ID]=@ID and OrganisationID=@OrganisationID
SELECT @result = @ID 

END
ELSE
if @ID=0
begin
IF not EXISTS(SELECT * FROM Resource WHERE Email= @Email)
begin
Insert into  Resource
(FirstName,
LastName,
Password,
PrimarySkill,
SecondarySkill,
Email,
Phone,
Role,
ResourceManager,
Customer,
StartDate,
TaxTerms,
ReferenceName1,
ReferenceName2,
Email1,
Email2,
C2CCompanyName,
C2CContactLastName,
C2CContactFirstName,
C2CEmail,
C2CPhone,
C2CDuns,
C2CPayRate,
EndDate,
TaxTerm1099,
TaxTerm1099PayRate,
CustomerManagerFirstName,
CustomerManagerLastName,
CustomerManagerPhone,
CustomerManagerEmail,OrganisationID) 
values 
(@FirstName,
@LastName,
@Password,
@PrimarySkill,
@SecondarySkill,
@Email,
@Phone,
@Role,
@ResourceManager,
@Customer,
@StartDate,
@TaxTerms,
@ReferenceName1,
@ReferenceName2,
@Email1,
@Email2,
@C2CCompanyName,
@C2CContactLastName,
@C2CContactFirstName,
@C2CEmail,
@C2CPhone,
@C2CDuns,
@C2CPayRate,
@EndDate,
@TaxTerm1099,
@TaxTerm1099PayRate,
@CustomerManagerFirstName,
@CustomerManagerLastName,
@CustomerManagerPhone,
@CustomerManagerEmail,@OrganisationID)

-- Insert into  Resource(FirstName,LastName,Password,Email,Phone,Role) values (@FirstName,@LastName,@Password,@Email,@Phone,@Role)
SELECT @result = @@IDENTITY
END

end

END 


what is the problem means i unable to update.
Posted
Updated 29-Jan-13 18:45pm
v7
Comments
AdityaPratapSingh 29-Jan-13 0:47am    
what error message you have got and in which line?
MT_ 29-Jan-13 0:47am    
Without details of what is happening in the class/function where you are actually update, I don't think, you will get any helpful answer. As far as I can see, there is nothing obviously wrong in this part of the code.
Dhritirao's 29-Jan-13 0:52am    
i m not getting error and the existing row in the database is not updating with new data it as it is
You have called like
objBLL.UserDetailsBLL(obj);
in button click.

But according to the code provided by you, there is no such method in BLL.
The two methods written by you in BLL are - 1. updateBLL() and 2. UpdateinfoBLL().

And in this, case I think it should be
objBLL.UpdateinfoBLL(obj);
hotmailkarthi 29-Jan-13 2:41am    
Have you passed primary key of existing row . In your case employee id

Not so much a solution as a strategy ...

You want to make sure it is executing your SP forst - at leat then you will know if the problem is in the SP or not.l

So, you could create a temporary table and insert a row into it at the start of the SP

Inserting anything is fine to start off with - then run teh prog and see if the record is inserted - if not the error is before it even gets there and you can concentrate on the C# code.

If the error is in the SP then try debugging that - just execute it manually outside of the application and see what happens - my suspicion is that it will return an error.
 
Share this answer
 
Comments
Dhritirao's 30-Jan-13 0:57am    
here i m able to insert the row but i m not able edit the existing with new values
_Maxxx_ 30-Jan-13 5:30am    
Do you mean that when you run the SP directly it doesn't update rows?
Dhritirao's 30-Jan-13 5:44am    
yes
It seems to me that your update SQL has

WHERE [ID]=@ID and OrganisationID=@OrganisationID

so if you are not seeing an error, then there is no existing record on the database with a matching ID and OrganisationId.

Personally I would check that this isn't the case - and if it still doesn't make sense, comment out the 'and ' portion of the WHERE (back up your DB first!)
 
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