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
karthik80cAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Lokesh B RDeveloperCommented:
Hi,

ExecuteNonQuery returns the number of rows affected.

So try the following to see whether it is updated or not.

int a = cmd.ExecuteNonQuery();
            if (a > 0)
            {
                Response.Write("Done..!!!");
                con.Close();
            }
            else
            {
                Response.Write("Not Updated");
                con.Close();
            }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
karthik80cAuthor Commented:
Hi Lokesh ,

code reflects its done but data is not updated
Lokesh B RDeveloperCommented:
Hi,

Make sure you are updating the correct database.
Your Guide to Achieving IT Business Success

The IT Service Excellence Tool Kit has best practices to keep your clients happy and business booming. Inside, you’ll find everything you need to increase client satisfaction and retention, become more competitive, and increase your overall success.

Naitik GamitSoftware DeveloperCommented:
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.
Naitik GamitSoftware DeveloperCommented:
Also try to write SqlCommand  as,
SqlCommand cmd = new SqlCommand("UPDATE 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);

Open in new window


Add CommandType
command.CommandType = CommandType.Text;

Open in new window

karthik80cAuthor Commented:
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?
karthik80cAuthor Commented:
Am using correct database and table Lokesh
MohitPanditCommented:
In case, it is showing "Done" message in response, that means it is updating the table. But still, below points

1. Checking in correct database
2. Is there any trigger implemented over 'Lead table' which overruled update?
karthik80cAuthor Commented:
Hi MohitPandit,

 Am checking correct  database and table but Still no Success on update
MohitPanditCommented:
is there any trigger implemented on table, which overrides UPDATE?
MohitPanditCommented:
Apart, kindly use SQL Profiler to trace your query as well from C# to database.
karthik80cAuthor Commented:
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
MohitPanditCommented:
Hi Karthik,

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

Best Regards
Vitor MontalvãoMSSQL Senior EngineerCommented:
Can you confirm that @id exists in the table? (WHERE [Ld_id] = @id)
MohitPanditCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.