C# 错误集锦

本文内容主要记录本人学习过程中遇到的错误,其中不乏有一些低级错误,为了牢记以免再次犯错特意记录,不要笑话哈;

1.“System.TypeInitializationException”类型的未经处理的异常在 LoginDAL.dll 中发生其他信息: “sqlHelper.sqlHelper”的类型初始值设定项引发异常;

问题原因:App.config文件中配置的connectionString.name="conn"与代码中读取配置文件ConfigurationManager.ConnectionStrings["connStr"]提取的索引名称不一致;

解决办法:保持一致;

2.阅读器关闭时尝试调用Read无效;

问题原因:①在获取SqlDataReader时创建连接使用了using();②在Reader SqlDataReader前关闭了链接或Reader

解决办法:去掉using,在Reader之前不要关闭Reader;

3.Data binding directly to a store query (DbSet, DbQuery, DbSqlQuery, DbRawSqlQuery) is not supported. Instead populate a DbSet with data, for example by calling Load on the DbSet, and then bind to local data. For WPF bind to DbSet.Local. For WinForms bind to DbSet.Local.ToBindingList(). For ASP.NET WebForms you can bind to the result of calling ToList() on the query or use Model Binding......

private void barBtnQuery_ItemClick(object sender, HuanSi.XtraBars.ItemClickEventArgs e)
{
     using(var db=new DBModel())
     {
          var result = from s in db.Students 
                             select new { s.sName,s.iAge};
           gridControl1.DataSource = result.ToList();//不加ToList()会报错
           gridView1.PopulateColumns();
     }
}

 

转载于:https://www.cnblogs.com/allen0/p/9495950.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值