0

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.

8
  • Are you sure your INSERT statement is ok? Commented Feb 6, 2015 at 23:11
  • If you put a breakpoint to catch in your code you'll see the reason. Commented Feb 6, 2015 at 23:11
  • 2
    Are you catching an exception, and then writing it to the console, and then not seeing it? What happens if you replace that with System.Diagnostics.Debug.WriteLine() and check the Output window in VS? Commented Feb 6, 2015 at 23:12
  • Yes @l19. If I return bool instead of void in InsertPoints(), and place that code in html in my Default.aspx (InsertPoints(77, 77) for example), it INSERTs them. I can run a SELECT * FROM DRAW query and sure enough it inserted the points. Commented Feb 6, 2015 at 23:13
  • 2
    Your code is possibly suseptible to a SQL Injection attack, please take the time to make sure all your queries are Parameterized. I would suspect that other methods you have written would allow SQL Injections based on this one example. Commented Feb 6, 2015 at 23:41

3 Answers 3

1

You are trying to send json data to server then you have to declare its content type.

add jquery ajax option for content type.

function insertIntoDB(x, y) {
        try
        {
            var data = {
                "xCoord": x,
                "yCoord": y
            };


            $.ajax({
                type: 'POST',
                url: 'Default.aspx/InsertPoints', 
                data: JSON.stringify(data), 
                contentType: 'application/json',
                success: function (data) {
                    alert("Success!");
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    alert("Fail: " + textStatus);
                }
            });
        }
        catch (e)
        {
            alert(e);
        }
    }
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks JasonMArcher to stringify data.
0

Try creating an old school Web Service(.asmx file) and not a Web Page(.aspx file).

It will even be better if you create a WebApi endpoint. It is not good practice to use web pages for web services

Comments

0

Try change :

  data: "{'xCoord': '" + x + "', 'yCoord': '" + y + "'}",  

to:

  data: {'xCoord': x , 'yCoord':  y },

and try add datatype:

  dataType: "json",
  contentType: 'application/json',

1 Comment

dataType option specifies expected format of data in response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.