SQL Update problem

Hi, I have an aspx page that takes query string requests generated by JQuery Ajax. In Chrome's dev console, I can see the GET being successfully sent by the Ajax code to the page, with a 200OK returned, and the data that was sent was the correct data as per the actual get request the console shows me. So it never bombs, but the data never updates the database. So my problem is most likely my UPDATE statement. Even when I manually invoke the aspx page to take action and manually enter the correct query string values, it does nothing. Can anybody help me fix this?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class edit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        String phone = Request["PhoneNumber"];
        String address = Request["Address"];
        String dollarAmt = Request["DollarAmt"];
        String ID = Request["ID"];
        decimal dollars = 0;
        int IdNum = 0;
        if (address == null) { address = ""; }
        if (ID != null) { IdNum = int.Parse(ID); }
        if (dollarAmt != null) { dollars = decimal.Parse(dollarAmt); }
        if (phone != null)
        {
            try
            {
                SqlConnection myConnection = new SqlConnection("server=localhost;" +
                                               "Trusted_Connection=yes;" +
                                               "database=myData; " +
                                               "connection timeout=30");
                myConnection.Open();
                try
                {
                    if (address == null) { address = ""; }
                    if (ID != null) { IdNum = int.Parse(ID); }
                    if (dollarAmt != null) { dollars = decimal.Parse(dollarAmt); }
                    SqlCommand cmd = new SqlCommand("UPDATE Orders SET PhoneNumber = @" + phone + ", Address = @" + address + " DollarAmt = @" + dollars + " WHERE  ID = @" + IdNum, myConnection);
                }
                catch (Exception err)
                {
                    Response.Write("<p>" + err + "</p>");
                    myConnection.Close();
                }
            }
            catch (Exception err)
            {
                Response.Write("<p>" + err + "</p>");
            }
        }
    }
}

Open in new window

LVL 9
BobHavertyComhAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

Snarf0001Commented:
You're never actually sending the command to the database.

cmd.ExecuteNonQuery()
BobHavertyComhAuthor Commented:
Hi Snarf0001. Well you are certainly right about that, and I added that, but it still doesn't work, and I think I still have a problem with my UPDATE statement. Here is what I am passing manually "edit.aspx?ID=89&PhoneNumber=5551212&Address=fake&DollarAmt=5.00". That should update the database but it does not. Now that I have added the execute statement, I'm able to get a better idea of the real problem. Here's the message now that it is finally attempting to do something. It's flagging the phone value for some reason and bombing.

"System.Data.SqlClient.SqlException (0x80131904): Must declare the scalar variable "@5551212". at System.Data.SqlClient.SqlConnection."

I'm glad it's at least bombing now so I can get error messages
Snarf0001Commented:
The update statement is a bit off.  Using the parameters "@" is definitely the way to go, as it prevents injection, but you have to put the actual name only with the @ symbol, and then add the parameter values to the command:

SqlCommand cmd = new SqlCommand("UPDATE Orders SET PhoneNumber = @phone, Address = @address, DollarAmt = @dollars WHERE  ID = @IdNum", myConnection);
cmd.Parameters.AddWithValue("@phone", phone);
cmd.Parameters.AddWithValue("@address", address);
cmd.Parameters.AddWithValue("@dollars", dollars);
cmd.Parameters.AddWithValue("@IdNum", IdNum);
cmd.ExecuteNonQuery();

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
BobHavertyComhAuthor Commented:
Yup, that's it. I was feeding the variable values right into the statement, rather than putting a parameter in the statement and setting the parameters afterwards. I didn't get it at first, but now I do and it's simple. Works perfect. Thanks.
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
Microsoft SQL Server

From novice to tech pro — start learning today.