[英]C# System.Data.StrongTypingException: Value for column '<columnName>' in '<tableName>' table is DBNull in LINQ query
I need to manage 3 databases with many tables and NULL values that can be almost everywhere.我需要管理 3 个数据库,其中包含许多表和几乎无处不在的 NULL 值。 For some cases I've found a solution, but now I need help.
对于某些情况,我找到了解决方案,但现在我需要帮助。
The code so far:到目前为止的代码:
public DataTable queryResults()
{
stockDataSet stockDS = new stockDataSet();
extensionStockDataSet extensionStockDS = new extensionStockDataSet(); administrationDataSet administrationDS = new administrationDataSet();
stockDataSetTableAdapters.sparePartsTableAdapter sparePartsAdapter = new stockDataSet TableAdapters.sparePartsTableAdapter();
sparePartsAdapter.Fill(stockDS.spareParts);
administrationDataSetTableAdapters.usersTableAdapter usersAdapter = new administrationDataSetTableAdapters.usersTableAdapter();
usersAdapter.Fill(administrationDS.users);
extensionStockDataSetTableAdapters.dataPartsTableAdapter dataPartsAdapter = new extensionStockDataSetTableAdapters.dataPartsTableAdapter();
dataPartsAdapter.Fill(extensionStockDS.dataParts);
var query = from sp in stockDS.spareParts
join ad in administrationDS.users
on sp.idUser equals ad.idUser
join ext in extensionStockDS.dataParts
on sp.idStock equals ext.idStock
into joined
from j in joined.DefaultIfEmpty()
where sp.idTypePart != 7
orderby sp.idUser
select new { namePart = sp.namePart,
idPartInExtStock = (j == null ? 0 : j.idPart),
notes = sp.notes,
parNo = ad.parno,
descr = ma.description,
idBrand = (j == null || j.idBrand == null ? "NOT AVAILABLE”: j.idBrand),
idModel = (j == null || j.idModel == null ? " NOT AVAILABLE " : j.idModel),
idVersion = (j == null || j.idVersion == null ? "NOT AVAILABLE" : j.idVersion)
};
DataTable results = new DataTable();
results = LINQToDataTable(query); //converts LINQ results to DataTable with System.Reflection
return results;
}
Going double click on every xsd DataSet file in Explore Soluzion panel in Visual Studio -> select a field of any table -> Properties panel -> NullValue -> (Null)
I can allow the Null
value for every String, that's what I've done for Going
double click on every xsd DataSet file in Explore Soluzion panel in Visual Studio -> select a field of any table -> Properties panel -> NullValue -> (Null)
I can allow the Null
value for every String, that's what I've为完成
notes = sp.notes
parNo = ad.parno
descr = ma.description
This can't be done every data type, eg for System.Int32
and System.DateTime
types: if you try to set for them NullValue -> (Null)
or NullValue -> (Empty)
, it returns the message:这不能对每种数据类型都执行,例如对于
System.Int32
和System.DateTime
类型:如果您尝试为它们设置NullValue -> (Null)
或NullValue -> (Empty)
,它会返回消息:
Property value not valid
So, for idPartInExtStock
that is a System.Int32 type, I've found this solution:因此,对于 System.Int32 类型的
idPartInExtStock
,我找到了这个解决方案:
idPartInExtStock = (j == null ? 0 : j.idPart)
that is working, and I've implemented it (just for try) also for String fields这是有效的,我也为字符串字段实现了它(仅供尝试)
idBrand = (j == null || j.idBrand == null ? "NOT AVAILABLE”: j.idBrand),
idModel = (j == null || j.idModel == null ? " NOT AVAILABLE " : j.idModel),
idVersion = (j == null || j.idVersion == null ? "NOT AVAILABLE" : j.idVersion)
Now I'm getting another Null problem with the line:现在我遇到了另一个 Null 问题:
where sp.idTypePart != 7
it returns at runtime the folliwing error from the stockDataSet.Designer.cs
:它在运行时从
stockDataSet.Designer.cs
返回以下错误:
System.Data.StrongTypingException: Value for column idTypePart in spareParts table is DBNull
Unfortunately I can't change values in the databases, I need to manage them as they are.不幸的是,我无法更改数据库中的值,我需要按原样管理它们。
My questions:我的问题:
System.Data.StrongTypingException: Value of column idTypePart in spareParts table is DBNull
, considering that sp.idTypePart
is System.Int32
type, the cast on it doesn't work and is not possible to set the (Null)
or (Empty)
value for it?System.Data.StrongTypingException: Value of column idTypePart in spareParts table is DBNull
,考虑到sp.idTypePart
是System.Int32
类型,它的强制转换不起作用并且无法设置(Null)
还是(Empty)
值呢? So far I've tried:到目前为止,我已经尝试过:
1A. 1A。
(int?)sp.idTypePart != 7
==> it says 'The cast is redundant'
, so this is useless (int?)sp.idTypePart != 7
==> 它说'The cast is redundant'
,所以这是没用的
1B. 1B。
sp?.idTypePart != 7
==> compiles, runs, but returns again System.Data.StrongTypingException: Value for column idTypePart in spareParts table is DBNull
sp?.idTypePart != 7
==> 编译,运行,但再次返回System.Data.StrongTypingException: Value for column idTypePart in spareParts table is DBNull
1C. 1C。
where (sp.idTypePart.= 7 && sp.idTypePart != null)
==> returns the warning 'expression value is always true'
, so this is useless where (sp.idTypePart.= 7 && sp.idTypePart != null)
==> 返回警告'expression value is always true'
,所以这是没用的
Ok, for now the problem seems solved.好的,现在问题似乎解决了。 I mean the "read data" part, I can extract the information correctly and success my query.
我的意思是“读取数据”部分,我可以正确提取信息并成功查询。
What I've done is simply going:我所做的只是:
double click on xsd stockDataSet in Explore Solution panel -> select idTypePart field in spareParts table Properties panel ->在“探索解决方案”面板中双击 xsd stockDataSet -> select idTypePart 字段在备用部件表属性面板中->
so所以
(1.)
: Datatype
-> System.String
(instead of the default System.Int32
preset) (1.)
: Datatype
-> System.String
(而不是默认的System.Int32
预设)
and then接着
(2.)
: NullValue
-> (Null)
(2.)
: NullValue
-> (Null)
I've done this even before yesterday, but for some reason that I can't figure out this wouldn't work at time.我什至在昨天之前就已经这样做了,但由于某种原因,我无法弄清楚这在当时是行不通的。
Anyway now the reading problem is solved, and this answer is for that question.无论如何,现在阅读问题已经解决了,这个答案就是针对那个问题的。
I wonder about what will happen when I'll write data, since this field in the database is an int, while in the dataset is a string.我想知道当我写数据时会发生什么,因为数据库中的这个字段是一个 int,而数据集中是一个字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.