简体   繁体   English

C# System.Data.StrongTypingException:列的值'<columnname> ' 在 '<tablename> ' 表是 LINQ 查询中的 DBNull</tablename></columnname>

[英]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.Int32System.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:我的问题:

  1. how can I solve this 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.idTypePartSystem.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' ,所以这是没用的

  1. Isn't there some method to avoid all the definitions of these exeptions for manage Null values in every field and for every data type?是否有一些方法可以避免这些例外的所有定义,以管理每个字段和每个数据类型中的 Null 值?

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.

相关问题 C# SQL 数据适配器 System.Data.StrongTypingException - C# SQL Data Adapter System.Data.StrongTypingException 引发了类型为&#39;System.Data.StrongTypingException&#39;C#的异常 - threw an exception of type 'System.Data.StrongTypingException' C# 在数据表中,第一列在 c# 中显示为 tablename.columnname - In Data table first column displaying with tablename.columnname in c# GEOMETRY引发了类型为&#39;System.Data.StrongTypingException&#39;的异常 - GEOMETRY threw an exception of type 'System.Data.StrongTypingException' 在C#中使用TableName和ColumnName选择范围 - Selecting a Range using TableName & ColumnName in C# 错误:表中列的值是C#业务层的DBNull - ERROR:value for column in the table is DBNull for C# Business Tier LINQ 查询 - sql 相当于 select * 来自表名,其中列名 = 列值 - LINQ Query - sql equivalent of select * from tablename where columnname =columnvalue 通过将Tablename,ColumnName和value作为参数传递来实体的动态Linq SearchFor - Dynamic Linq SearchFor from entities by Passing Tablename,ColumnName and value as parameter c#linq查询具有其他列值的聚合数据 - c# linq query aggregation data with other column value 在Linq C#中按[ColumnName]进行选择计数(ColumnName)分组 - SELECT COUNT (ColumnName) Group By [ColumnName] in Linq C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM