none
Couldn't send and store String data to database through WCF RRS feed

  • Question

  • Dear all, I have created a Windows 8 app, I have a table in SQL server database to store people's name, " [Name] VARCHAR (50)"

    I have manage to send and save integer values to database, but when i modified my coding to store the string, it does not work, table data is empty. Please help!

    itemDetail.html

    <div>
     <input id="join1" type="text" />
     <button id="joinbtn">insert</button>
     </div>

    itemDetail.js

    var joinButton = document.getElementById('joinbtn');
                // Register Click event
                joinButton.addEventListener("click", joinButtonClick, false);
    
                function joinButtonClick() {
                    // Retrieve element
                    var baseURI2 = "http://localhost:45573/AddService.svc/Join";
                    var jointext = document.getElementById('join1').value;
     
                    WinJS.xhr({
                        type: "POST",
                        url: baseURI2,
                        headers: { "Content-type": "application/json" },
                        data: '{"namet":' + jointext + '}'
                    }).then(function complete(request) {
                        var resdata = request.responseText;
                    }, function error(er) {
                        var err = er.statusText;
                    })
                }

    AddService.svc.cs

    public void Join(string namet)
            {
                string connectionString = System.Configuration.ConfigurationManager.
    ConnectionStrings["Database1ConnectionString1"].ConnectionString;
    
                SqlConnection con = new SqlConnection(connectionString);
                string sql = "INSERT INTO Table2(Name) VALUES (@Name)";
    
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@Name", namet);
                try
                       {
                    con.Open();
                    int numAff = cmd.ExecuteNonQuery();
                }
                    con.Close();
            }

    IAddService.cs

     [OperationContract]
            [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
            void Join(string namet);

    Thank you!


    • Edited by MikeWhitesand Wednesday, August 14, 2013 4:01 PM typo error
    Tuesday, August 13, 2013 4:52 PM

All replies

  • Change your implementation of itemDetail.js like this

    function joinButtonClick() 
    {
    // Retrieve element
    var baseURI2 = "http://localhost:45573/AddService.svc/Join";
    var jointext = document.getElementById('join1').value;
    var data = {
        namet: jointext
    };
    var xhrOptions = {
        url: "http://localhost:45573/AddService.svc/Join",
        headers: { "Content-Type": "application/json" },
        data: JSON.stringify(data)
    };
    WinJS.xhr(xhrOptions).done(
        function (req) {
            // Call completed, find more info on the parameter
        }, function (req) {
            // An error occurred, find more info on the parameter
        });
    }

    Srithar




    • Edited by Srithar Wednesday, August 14, 2013 8:34 AM
    Tuesday, August 13, 2013 7:35 PM
  • Hi MikeWhitesand,

    From your description, you have a web page which use JQuery to invoke a WCF REST service operation for updating database, and you found that the string parameter sent to service didn't get inserted into database,correct?

    Have you got any detailed error message when click the button on page to perform the data updating? Without detailed error info, I'd suggest you first determine where did the error occur. You can first use fiddler or the webbrowser's developer tool bar (which can capture network traffice) to see if the REST service request is correctly sent and arrive server-side. Then, you can try debuggin the service code to see if the string parameter does be correctly passed into the operation's function code. If all the previous steps complete correctly, then the problem should be at the ADO.NET data access code.

    #Fiddler

    http://fiddler2.com/home


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, August 14, 2013 2:14 PM
    Moderator
  • Hi Srithar,

    Thank you for your reply! the code is not working. My app runs fine, but after i entered a word and click the button, my database table data still empty. I am confuse as the code above works fine for storing integer ("string" change to "int")

    Wednesday, August 14, 2013 4:07 PM
  • Hi Steven,

    Thank you for your reply! My app is running fine, no errors. The code above initially is to store integer (successfully store int value to database table), then i modifed the code ("int" change to "string"), and created new table ("Table2") with Name VARCHAR(50). How come the table data is empty after i enter a word and click the button. It works fine for int, but not string. 

    I am very confuse.

    Wednesday, August 14, 2013 4:12 PM
  • The link below is the sample app that i use. Added Database in the App Data folder and I modified the code in AddService.svc.cs in order to insert 2 int to database, the app works fine, until i modify again to add string/text. Please advice.

    http://code.msdn.microsoft.com/windowsapps/How-to-consume-JSON-Web-5db0174b

    Wednesday, August 14, 2013 5:42 PM