Click here to Skip to main content
15,887,135 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
My coding is as below.Dunno why the data did not insert to my SQL database.
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Reg.Mobile.Reg.ServerReference;
using System.Data.Services.Client;
using System.Windows.Data;

namespace Reg.Mobile
{
    public partial class Registration : PhoneApplicationPage
    {
        private static readonly Uri Reg_Uri =
       new Uri("http://localhost/Reg.Server/ApplicationData.svc/");
        private DataServiceCollection<reg.serverreference.login>
           logins;
        private Reg.ServerReference.ApplicationData applicationdata;

        public Registration()
        {
            InitializeComponent();
            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
        }
        public class UserInfo
        {
            string uname;
            string pwd;
            string email;
            int mno;
            public string Username
            {
                get { return uname; }
                set { uname = value; }
            }
            public string Password
            {
                get { return pwd; }
                set { pwd = value; }
            }
            public string EmailAdd
            {
                get { return email; }
                set { email = value; }
            }

            public int Mobile
            {
                get { return mno; }
                set { mno = value; }
            }
        }
        private void button1_Click(object sender, RoutedEventArgs e)
        {
            if (textBox1.Text == "") { MessageBox.Show("Plz. enter the username"); }
            if (textBox2.Text == "") { MessageBox.Show("Plz. enter the Password"); }
            if (textBox3.Text == "") { MessageBox.Show("Plz. enter the e-mail add"); }
            if (textBox4.Text == "") { MessageBox.Show("Plz. enter the mobile number"); }
            // Write to the Isolated Storage

            string strId;
            if (NavigationContext.QueryString.TryGetValue("Id", out strId))
            {
                if (strId != "-1") // An existing Product
                {

                }
                else
                {
                    Reg.ServerReference.Login objLogins = new Reg.ServerReference.Login();
                    // Add the new Product to the Data Service Context
                    applicationdata.AddToLogins(objLogins);
                    // Set the context of the UI to the new Product
                    this.ContentPanel.DataContext = objLogins;
                    textBox1.UpdateBinding();
                    textBox2.UpdateBinding();
                    textBox3.UpdateBinding();
                    textBox4.UpdateBinding();
                    applicationdata.BeginSaveChanges(
               SaveChangesOptions.Batch, OnChangesSaved, applicationdata);
                }
            }
        }

        #region OnChangesSaved
        private void OnChangesSaved(IAsyncResult result)
        {
            // Use the Dispatcher to ensure that the 
            // asynchronous call returns in the correct thread.
            Dispatcher.BeginInvoke(() =>
            {
                // Cast result to the ApplicationDataContext
                applicationdata = result.AsyncState as Reg.ServerReference.ApplicationData ;
                try
                {
                    // Complete the save changes operation
                    applicationdata.EndSaveChanges(result);
                }
                catch (Exception ex)
                {
                    // Display the error from the response.
                    MessageBox.Show(string.Format("An error has occurred: {0}", ex.Message));
                }
                finally
                {
                    // Go back to main page
                    NavigationService.GoBack();
                }
            });
        }
        #endregion


        private List<userinfo> GeneratePersonData()
        {
            List<userinfo> data = new List<userinfo>();
            UserInfo ui = new UserInfo();
            ui.Username = textBox1.Text;
            ui.Password = textBox2.Text;
            ui.EmailAdd = textBox3.Text;
            ui.Mobile = Convert.ToInt32(textBox4.Text);
            data.Add(ui);
            return data;
        }
    }
        #region Extensions
        public static class Extensions
        {
            #region UpdateBinding
            public static void UpdateBinding(this TextBox textBox)
            {
     
                BindingExpression bindingExpression =
                        textBox.GetBindingExpression(TextBox.TextProperty);
                if (bindingExpression != null)
                {
                    bindingExpression.UpdateSource();
                }
            }
            #endregion
        }
        #endregion
    }
Posted
Updated 29-May-12 18:30pm
v2
Comments
Herman<T>.Instance 31-May-12 4:48am    
what have you tried? Did you debug your code? Is there any exception?

1 solution

I found out this example

http://lightswitchhelpwebsite.com/Blog/tabid/61/EntryId/135/Visual-Studio-LightSwitch-and-Windows-Phone-7-OData-Full-CRUD-Example.aspx

just edit the full code according like this:

C#
public partial class EditProduct : PhoneApplicationPage
    {
        // Url to the OData Service in Visual Studio LighTSwitch
        private static Uri LightSwitchApplicationUri =
            new Uri("http://localhost/flowershop/ApplicationData.svc/");

        // The Data Service Context that encapsulates operations executed against the oData source
        private ApplicationData ApplicationDataContext;

        // The DataService Collection that will contain the Products 
        private DataServiceCollection<flowershopservice.flowershopproduct> dsFlowerShopProducts;

        public EditProduct()
        {
            InitializeComponent();

            // Initialize the Data Service Context
            ApplicationDataContext = new ApplicationData(LightSwitchApplicationUri);

            // Pass the user name and password for the LightSwitch account to be used
            // All security and business logic in LightSwitch will be executed using this account
            ApplicationDataContext.Credentials = new NetworkCredential("MaryDoe", "password#1");
        }

        #region OnNavigatedTo
        protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            // Thsi method is called when a user navigates to this page
            base.OnNavigatedTo(e);

            string strId;

            // Is an Id passed?
            if (NavigationContext.QueryString.TryGetValue("Id", out strId))
            {
                if (strId != "-1") // An existing Product
                {
                    // Convert Id to a integer
                    int intId = Convert.ToInt32(strId);

                    // Query only the Product matching the Id
                    var FlowerShopProductsQuery =
                        from FlowerShopProducts in ApplicationDataContext.FlowerShopProducts
                        where FlowerShopProducts.Id == intId
                        select FlowerShopProducts;

                    // Start the process to load the selected Product
                    // Initialize the DataService Collection
                    dsFlowerShopProducts =
                        new DataServiceCollection<flowershopproduct>(ApplicationDataContext);

                    // Wire up the dsFlowerShopProducts_LoadCompleted method
                    dsFlowerShopProducts.LoadCompleted +=
                        new EventHandler<loadcompletedeventargs>(dsFlowerShopProducts_LoadCompleted);

                    // Start the request to retrieve the data
                    dsFlowerShopProducts.LoadAsync(FlowerShopProductsQuery);
                }
                else // A new Product
                {
                    // Create a new Product
                    FlowerShopProduct objFlowerShopProduct = new FlowerShopProduct();

                    // Add the new Product to the Data Service Context
                    ApplicationDataContext.AddToFlowerShopProducts(objFlowerShopProduct);

                    // Set the context of the UI to the new Product
                    this.ContentPanel.DataContext = objFlowerShopProduct;
                }
            }
        }
        #endregion

</loadcompletedeventargs></flowershopproduct></flowershopservice.flowershopproduct>
 
Share this answer
 

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