Link to home
Start Free TrialLog in
Avatar of karthik80c
karthik80cFlag for United States of America

asked on

ASP.NET Update Query Not Working

Hi ASP Experts,

Am doing a simple update query in csharp and code alerts its updated the data  but its really not updated the value .

 string id = Request.QueryString["id"];
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        SqlCommand cmd = new SqlCommand("UPDATE [dbo].[Lead] SET [LeadOwner] = @LeadOwner, [LeadOwnerId] = @LeadOwnerId, [Company] = @Company, [FirstName] = @FirstName, [LastName] = @LastName, [Email] = @Email, [Phone] = @Phone, [Mobile] = @Mobile, [Website] = @Website, [LeadSource] = @LeadSource, [LeadStatus] = @LeadStatus,  [ModifiedBy] = @ModifiedBy, [ModifiedById] = @ModifiedById, [ModifiedTime] = @ModifiedTime, [Street] = @Street, [PostalCode] = @PostalCode, [Description] = @Description, [LastActivityTime] = @LastActivityTime, [TakenBy] = @TakenBy, [Subject] = @Subject, [Suburb] = @Suburb, [States] = @States, [PropertyType] = @PropertyType, [AgentContacted] = @AgentContacted WHERE [Ld_id] = @id", con);
        con.Open();
                    cmd.Parameters.AddWithValue("@id", id);
                    cmd.Parameters.AddWithValue("@LeadOwner", LeadOwner.Text);
                    cmd.Parameters.AddWithValue("@LeadOwnerId", DBNull.Value);
                    cmd.Parameters.AddWithValue("@Company", Company.Text);
                    cmd.Parameters.AddWithValue("@FirstName", FirstName.Text);
                    cmd.Parameters.AddWithValue("@LastName", LastName.Text);
                    cmd.Parameters.AddWithValue("@Email", Email.Text);
                    cmd.Parameters.AddWithValue("@Phone", Phone.Text);
                    cmd.Parameters.AddWithValue("@Mobile", Mobile.Text);
                    cmd.Parameters.AddWithValue("@Website", Website.Text);
                    cmd.Parameters.AddWithValue("@LeadSource", LeadSource.Text);
                    cmd.Parameters.AddWithValue("@LeadStatus", LeadStatus.Text);
                    cmd.Parameters.AddWithValue("@ModifiedBy",DBNull.Value);
                    cmd.Parameters.AddWithValue("@ModifiedById", DBNull.Value);
                    cmd.Parameters.AddWithValue("@ModifiedTime", DateTime.Now.ToString("h:mm:ss tt"));
                    cmd.Parameters.AddWithValue("@Street", Street.Text);
                    cmd.Parameters.AddWithValue("@PostalCode", PostalCode.Text);
                    cmd.Parameters.AddWithValue("@Description", Description.Text);
                    cmd.Parameters.AddWithValue("@LastActivityTime", DateTime.Now.ToString("h:mm:ss tt"));
                    cmd.Parameters.AddWithValue("@TakenBy", Taken.Text);
                    cmd.Parameters.AddWithValue("@Subject", Subject.Text);
                    cmd.Parameters.AddWithValue("@Suburb", Suburb.Text);
                    cmd.Parameters.AddWithValue("@States", States.Text);
                    cmd.Parameters.AddWithValue("@PropertyType", PropertyType.Text);
                    cmd.Parameters.AddWithValue("@AgentContacted", AgentContacted.Text);

                    int a = cmd.ExecuteNonQuery();
                    if (a == 0)
                    {
                        Response.Write("sorry");
                        con.Close();

                    }
                    else
                    {
                        Response.Write("done");
                        con.Close();

                    }

Open in new window


Thanks in Advance
ASKER CERTIFIED SOLUTION
Avatar of Lokesh B R
Lokesh B R
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of karthik80c

ASKER

Hi Lokesh ,

code reflects its done but data is not updated
Hi,

Make sure you are updating the correct database.
Use try , catch and add break point and debug...........find why not update the data.

I think in one of column value not updated check with datatype and updating value.
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thank you for your suggestion experts  and one more thing i forgot to tell .In my form some times contains null to update is that conditions would causes this error?
Am using correct database and table Lokesh
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi MohitPandit,

 Am checking correct  database and table but Still no Success on update
is there any trigger implemented on table, which overrides UPDATE?
Apart, kindly use SQL Profiler to trace your query as well from C# to database.
Hi MohitPandit

i got the causes of the error its because  the value am giving in text box remains unchanged this causes the issue . So could you tell how to slove this
Hi Karthik,

The text box values entered by end-user OR you are entering from any API, could you confirm?

Best Regards
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Alternate, could you confirm the values must only be bound for the first time of page load?

Actually, I wanted to ask about "IsPostback" at page load level.
So, in page load you must check the condition whether the page IsPostback or not.

Best Regards