ASP.NET Update Query Not Working

karthik80c
karthik80c used Ask the Experts™
on
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
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Developer
Top Expert 2015
Commented:
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

Author

Commented:
Hi Lokesh ,

code reflects its done but data is not updated
Lokesh B RDeveloper
Top Expert 2015

Commented:
Hi,

Make sure you are updating the correct database.
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Naitik GamitSoftware Developer
Top Expert 2015

Commented:
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 Developer
Top Expert 2015
Commented:
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

Author

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?

Author

Commented:
Am using correct database and table Lokesh
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?

Author

Commented:
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.

Author

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
Hi Karthik,

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

Best Regards
Vitor MontalvãoIT Engineer
Distinguished Expert 2017
Commented:
Can you confirm that @id exists in the table? (WHERE [Ld_id] = @id)
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

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial