将数据从WP8插入Azure数据库后的MoveNext()异常(移动服务)

时间:2013-02-19 21:10:34

标签: azure windows-phone-8 azure-mobile-services

我的问题与Windows Azure / Windows Phone 8 services

有关

点击按钮后,使用refresh_button_Tap事件我收到了MessageBox消息:

  

“System.NullReferenceException:对象引用未设置为   对象的实例。在   pic.MainPage.d__0.MoveNext()“

然后我点击确定,新记录显示在Azure Database

public class UsersTableItem
    {
        public int Id { get; set; }

        [DataMember(Name = "Login")]
        public string Login { get; set; }

        [DataMember(Name = "Password")]
        public string Password { get; set; }

    }

    public partial class MainPage : PhoneApplicationPage
    {
        // MobileServiceCollectionView implements ICollectionView (useful for databinding to lists) and 
        // is integrated with your Mobile Service to make it easy to bind your data to the ListView
        //private MobileServiceCollectionView<PicturesTableItem> items;

        private IMobileServiceTable<PicturesTableItem> picturesTable = App.MobileService.GetTable<PicturesTableItem>();

        private MobileServiceCollectionView<UsersTableItem> items;

        private IMobileServiceTable<UsersTableItem> usersTable = App.MobileService.GetTable<UsersTableItem>();

        // Constructor
        public MainPage()
        {
            InitializeComponent();

        }

        private async void InsertUsersTableItem(UsersTableItem UsersTableItem)
        {
            // This code inserts a new PicturesTableItem into the database. When the operation completes
            // and Mobile Services has assigned an Id, the item is added to the CollectionView
            try
            {

                await usersTable.InsertAsync(UsersTableItem);
                items.Add(UsersTableItem);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.ToString());
            }
        }

        private void refresh_button_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            var usersTableItem = new UsersTableItem {Login = "LOL", Password = "9E32581C5C0D680FFC7D95C370D3260B" };
            InsertUsersTableItem(usersTableItem);
        }
    }

Azure SQL

USE [pic_db]
GO

/**** Object:  Table [pic].[UsersTableItem]    Script Date: 2013-02-19 22:02:47 ****/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [pic].[UsersTableItem](
       [Id] [int] IDENTITY(1,1) NOT NULL,
       [Login] [nvarchar](max) NOT NULL,
       [Password] [nvarchar](max) NOT NULL,
CONSTRAINT [PK_UsersTableItem] PRIMARY KEY CLUSTERED
(
       [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)

GO

如何解决这个问题?

2 个答案:

答案 0 :(得分:1)

从您的代码中,我没有看到您初始化items集合,因此items.Add(UsersTableItem);似乎是罪魁祸首,它发生在InsertAsync之后,因此您的数据会确实很好地到达了数据库。

答案 1 :(得分:0)

首先,谢谢Jim O'Neil。

但还有更多。这是来自Hyper-V和我的Wifi的网络连接问题。我的Hyper-V模拟器连接到互联网,延迟1分钟。首先应用程序运行并在一段时间后同步时钟。但仍然没有联系。一分钟后连接真的可用。

由于:related topic

今天我再次从Azure下载示例TodoItems项目。他们使用类似的东西来初始化项目集合。

private void RefreshTodoItems()
        {
            // This code refreshes the entries in the list view be querying the TodoItems table.
            // REMOVED the filter on completed items, so they're all dislpayed
            items = todoTable
                //.Where(todoItem => todoItem.Complete == false)
                .ToCollectionView();
            ListItems.ItemsSource = items;
        }

protected override async void OnNavigatedTo(NavigationEventArgs e)
        {
            RefreshTodoItems();
        }

如果模拟器无法连接,则RefreshTodoItems将无法正常工作。所以我的问题也出现在Azure的测试示例项目中。我不仅是一个有Wifi / Hyper-V模拟器问题的人。我应该购买Windows Phone 8设备,否则微软应该解决这个问题。