2012-03-30 54 views
1

我不断收到此错误,“当前值'String.Empty'类型与期望的'System.Boolean'类型不兼容,当我尝试循环来自Azure表的一大堆实体,我只是使用Azure的新手,所以这可能非常容易,我得到的错误。尝试通过Azure表中的实体循环时出错

我的代码:

private void registerButton_Click(object sender, RoutedEventArgs e) 
    { 
     CloudStorageAccount storageAccount = CloudStorageAccount.Parse(RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString")); 

     // Create the table client 
     CloudTableClient tableClient = storageAccount.CreateCloudTableClient(); 

     // Get the data service context 
     TableServiceContext serviceContext = tableClient.GetDataServiceContext(); 

     // Create a new customer entity 
     user = new UserDetailsEntity(); 

     //Setting the fields of the new userEntity 
     user.username = usernameText.Text; 
     user.password = passwordText.Text; 
     user.subscriptionID = subText.Text; 
     user.subscriptionName = subscriptionNameText.Text; 
     user.thumbprint = thumbprintText.Text; 
     user.email = emailText.Text; 
     user.phoneNumber = "3530" + numberText.Text; 


     int rowCount = 1; 


     CloudTableQuery<UserDetailsEntity> Query = (from en in serviceContext.CreateQuery<UserDetailsEntity>("userdetails") 
                  select en).AsTableServiceQuery<UserDetailsEntity>(); 

     //error occurs in the next line 
     foreach (UserDetailsEntity ent in Query) 
     { 

      rowCount++; 
     } 

     user.RowKey = rowCount.ToString(); 





      // Add the new customer to the people table 
      serviceContext.AddObject("userdetails", user); 

      // Submit the operation to the table service 
      serviceContext.SaveChangesWithRetries(); 
      //Set the variables so they can be retrieved when the next screen loads 
      Application.Current.Properties["username"] = usernameText.Text; 
      Application.Current.Properties["password"] = passwordText.Text; 

      Window1 userHome = new Window1(); 
      this.Close(); //to close Password window 
      userHome.Show(); //to show Main form 
     } 

回答

2

没有更多的代码,我不能告诉你到底哪里出了问题,但例外是相当自明。您正尝试将布尔属性设置为字符串的值。

如果在您的foreach中发生错误,如代码注释中所述,那么我会检查您的UserDetailsEntity对象是如何设置的。可能有一个属性设置为布尔值,但是您的数据将以String.Empty形式返回。你在foreach中得到这个的原因是因为你的LINQ查询是IQueryable类型的,所以它不会实际执行并填充你的对象,直到你实际访问数据(通过你的foreach)*。所以,你可以在你的UserDetailsEntity属性中添加断点,以查看它是否属于这种情况。 *请记住,这是N + 1问题,您在循环的每次迭代中都要调用数据库。您可以通过调用.ToList()来解决此问题,以便立即将所有数据加载到查询中......如果这对您是个问题,那就是。

+0

干杯的答复,错误是发生上线 的foreach(在查询UserDetailsEntity ENT) 这就是为什么我很困惑,为什么错误是发生 – StevenR 2012-03-30 13:32:41

+0

@StevenR对不起,我错过了,我有更新了我的答案,以帮助你更好地理解这一点 – 2012-03-30 13:47:37

+0

非常感谢,我会尽力做你的建议 – StevenR 2012-03-30 14:27:40

相关问题