为什么没有数据插入到我的数据库中 [英] Why there is no data insert to my database

查看:63
本文介绍了为什么没有数据插入到我的数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的编码如下.不知道为什么数据没有插入到我的SQL数据库中.

My coding is as below.Dunno why the data did not insert to my SQL database.

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
    }

推荐答案

我发现了这个示例

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

只需像这样编辑完整的代码即可:

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:

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>


这篇关于为什么没有数据插入到我的数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆