Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When I execute this code (inside .js) :

jQuery.fn.stickyNotes.createNote = function () {
        var pos_x = 0;
        var pos_y = 0;
        var note_id = jQuery.fn.stickyNotes.notes.length + 1; 
  PageMethods.saveNote(note_id); // remote method



I need to save note_id value inside MSSQL database.

I put this on default.aspx (inside <body> I'm not sure if I'm right), to enable AJAX:
<pre lang="xml"><form id="form2" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
</form>



and for a method in default.aspx.cs
[System.Web.Services.WebMethod]
    public static string saveNote(int note_id)
    {
if (note_id == null || text.Length == 0)
            return String.Empty;
        SqlConnection conn = null;
        try
        {
            string connection = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            conn = new SqlConnection(connection);
            string sql = "INSERT INTO .....";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.Parameters.AddWithValue("ID_Note", note_id);
            // ...
            conn.Open();
            string contNm = Convert.ToString(cmd.ExecuteScalar());
            return contNm;


I have this defined on <head>
<script type="text/javascript" src="script/jquery.stickynotes.js"></script>

By now I have two importante questions:

- I still didnt compiled the code because I get the error that I have two forms with runat=server (I another one two login). How can I solve this problem? Can I put the script manager inside that form?

- If I do that my AJAX dont work. When I click add note nothing happens... previosly it works (it creates a new note/element). If I comment this line PageMethods.saveNote(note_id); it works again... It looks like that my JS stops working. Can you help me please ?

Thanks in advance.
Posted
Updated 21-Dec-10 6:30am
v9

1 solution

You need to set EnablePageMethods:
HTML
<%@ Page Language="C#" %>
<script runat="server">
    [System.Web.Services.WebMethod]
    public static string GetHello()
    {
        return "Hello";
    }
</script>
<html>
<head runat="server">
    <title>Test PageMethods</title>
</head>
<body>
    <script type="text/javascript">
        function DoIt() {
            PageMethods.GetHello(function(result) {
                alert(result);
            });
        }
    </script>
    <form id="form1" runat="server">
        <asp:ScriptManager runat="server" EnablePageMethods="true" />
        <input type="button" onclick="DoIt();" value="Do Stuff" />
    </form>
    <form id="form2"></form>
</body>
</html>
 
Share this answer
 
Comments
Maxdd 7 21-Dec-10 13:08pm    
Thanks, that's a stupid mistake from me.

However I think the function public static string saveNote(int note_id) its not working - executing.

I have the code I posted exactly like that "INSERT INTO..."; I dont even have the DB created, and I get any error, so that means it's not reaching that code.

What am I missing?
AspDotNetDev 21-Dec-10 13:14pm    
I believe you have to create a callback function to handle successes and failures... exceptions might just get swallowed otherwise. Copy my code exactly, because I ran it and confirmed it works. Then try pasing your insert code into the server-side function (using some dummy value since you won't be passing a note_id parameter). Then add the node_id parameter. Break the problem into pieces and you should be able to solve it. I also noted you have a try/catch, but you didn't show all the code, so I don't know how you're handling the exception... you might just be swallowing it.
Maxdd 7 21-Dec-10 13:24pm    
Well I thing the problem is my default.aspx.cs for some reason is not even working.

I placed


protected void Page_Load(object sender, EventArgs e)
{
string strConn;
strConn = System.Web.Configuration.WebConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;
System.Data.SqlClient.SqlConnection Conn = new System.Data.SqlClient.SqlConnection(strConn);
Conn.Open();
}

as I dont even have a connection string (web.config) and I didnt get any error. - what is happening? so strange..
Maxdd 7 21-Dec-10 13:31pm    
Well, its working now (the .cs). So strange.

Time to create the DB and see if the AJAX its working.

Thanks for your help!
AspDotNetDev 21-Dec-10 13:38pm    
You are welcome. If my answer helped you, please remember you can vote on answers and you can "accept" them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900