locked
How to display all the data from SQL database using WCF?

    Question

  • Dear all, 

    I have added 2 new textbox, 1 button in the HTML for user to enter name, address and a click button to save in database.

    I have managed to insert data into SQL database using WCF. Now I have problem displaying all the data into Windows 8 app. (my SQL database is in the same project as well)

    The code below is for my insert data:

    default.html

    <input id="txtName" type="text" />
      <input id="txtAddress" type="text" />
      <button id="insertbtn">insert</button>

    JSONWCFService.svc.cs

    public void Insert(int name, int address)
            {
                string connectionString = System.Configuration.ConfigurationManager.
    ConnectionStrings["Database1ConnectionString1"].ConnectionString;
                SqlConnection con = new SqlConnection(connectionString);
    
                string sql = "INSERT INTO Table1(Name, Address) VALUES (@Name, @Address)";
    
                SqlCommand cmd = new SqlCommand(sql, con);
                cmd.Parameters.AddWithValue("@Name", name);
                cmd.Parameters.AddWithValue("@Address", address);
                try
                {
                    con.Open();
                    int numAff = cmd.ExecuteNonQuery();
    
                }
                catch (SqlException exc)
                {
                }
                finally
                {
                    con.Close();
                }
            }

    IAddService.cs

    public interface IAddService
        {
            [OperationContract]
            [WebInvoke(RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json, BodyStyle = WebMessageBodyStyle.Wrapped)]
            void Insert(string name, string address);
                
        }

    default.js 

    function insertButtonClick() {
    
            var baseURI2 = "http://localhost:45573/AddService.svc/Insert";
            var name = document.getElementById('txtName').value;
            var address = document.getElementById('txtAddress').value;
    
    WinJS.xhr({
                type: "POST",
                url: baseURI2,
                headers: { "Content-type": "application/json" },
               data: '{"name":' + txtName+ ',"address":' + txtAddress+ '}' 
            }).then(function complete(request) {
                var resdata = request.responseText;
            }, function error(er) {
                var err = er.statusText;
            })
        }

    Please show some sample on how to retrieve data from database and display in Windows 8 app.

    Thank you! :)


    • Edited by Finnnn Saturday, April 12, 2014 7:12 AM typo error
    Saturday, April 12, 2014 7:10 AM

Answers

  • highly recommend that you use Entity Framework as a Data Layer so that your life is much easier!

    hope this helps!

    Saturday, April 12, 2014 7:58 AM
  • Hi Finnn,

    Windows Store App is just a Client Side, it cannot directly contact with SQL, by WCF you can transfer some data.

    However to display all the SQL records, you should really programming on your Server side, for instance to use EF mentioned by Shreeharsh, and pass the data to your user by WCF.

    Additionally we have a forum which support EF:https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    Hope helps.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, April 22, 2014 7:57 AM
    Moderator

All replies

  • highly recommend that you use Entity Framework as a Data Layer so that your life is much easier!

    hope this helps!

    Saturday, April 12, 2014 7:58 AM
  • Hi Finnn,

    Windows Store App is just a Client Side, it cannot directly contact with SQL, by WCF you can transfer some data.

    However to display all the SQL records, you should really programming on your Server side, for instance to use EF mentioned by Shreeharsh, and pass the data to your user by WCF.

    Additionally we have a forum which support EF:https://social.msdn.microsoft.com/Forums/en-US/home?forum=adodotnetentityframework

    Hope helps.

    --James


    <THE CONTENT IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, WHETHER EXPRESS OR IMPLIED>
    Thanks
    MSDN Community Support

    Please remember to "Mark as Answer" the responses that resolved your issue. It is a common way to recognize those who have helped you, and makes it easier for other visitors to find the resolution later.

    Tuesday, April 22, 2014 7:57 AM
    Moderator