I have an HTML canvas on my page. When I click the canvas, a simple dot paints. I pass the coordinates into my InsertIntoDB function to enter the x and y coordinates into a MS SQL Server database, but it's not happening.
I can, however, place the C# call into an HTML
element and get it to work fine, so long as I use dummy xCoord and yCoord numbers and return something (not void).
My code is actually popping up the "Success!" alert, but when I run a SQL query, there was no INSERT. Here is the Code from Default.aspx:
function insertIntoDB(x, y) {
try
{
$.ajax({
type: 'POST',
url: 'Default.aspx/InsertPoints',
data: "{'xCoord': '" + x + "', 'yCoord': '" + y + "'}",
// '{"xCoord": "' + x + '", "yCoord": "' + y + '"}',
success: function (data) {
alert("Success!");
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
alert("Fail: " + textStatus);
}
});
}
catch (e)
{
alert(e);
}
}
Here is the C# code in Default.aspx.cs:
[WebMethod]
public static void InsertPoints(int xCoord, int yCoord) {
string myConn = "Data Source=MYDATABASEINFO;Initial Catalog=DATABASENAME;Integrated Security=False;User ID=USERID;Password=MYPASSWORD";
SqlConnection myConnection = new SqlConnection(myConn);
try
{
myConnection.Open();
SqlCommand myCommand= new SqlCommand("INSERT INTO DRAW (xCoord, yCoord) " +
"Values (" + xCoord + ", " + yCoord + ");", myConnection);
myCommand.ExecuteNonQuery();
myConnection.Close();
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
}
Thanks for your help.