Arnold Layne
asked on
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>");
}
}
}
}
ASKER
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&PhoneNumb er=5551212 &Address=f ake&Dollar Amt=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.Sql Exception (0x80131904): Must declare the scalar variable "@5551212". at System.Data.SqlClient.SqlC onnection. "
I'm glad it's at least bombing now so I can get error messages
"System.Data.SqlClient.Sql
I'm glad it's at least bombing now so I can get error messages
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
cmd.ExecuteNonQuery()