SQLServer int字段.价值有时为空.DataAdapter填充数据集确定,可以在DatagridView中显示数据.
尝试从数据集以编程方式检索数据时,数据集字段检索代码会引发StronglyTypedException错误.
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
public int curr_reading {
get {
try {
return ((int)(this[this.tableHistory.curr_readingColumn]));
}
catch (global::System.InvalidCastException e) {
throw new global::System.Data.StrongTypingException("The value for column \'curr_reading\' in table \'History\' is DBNull.", e);
}
Run Code Online (Sandbox Code Playgroud)
通过在get访问器中检查DBNull并返回null但是......当数据集结构被修改(仍在开发)时,我的更改(不出所料)就消失了.
处理这种情况的最佳方法是什么?似乎我在数据集级别处理它时遇到困难.是否有某种属性可以告诉自动代码生成器将更改保留在原位?
嘿伙计们,我数据库中的一个表有一个字段"描述",允许'Nulls'.这在ASP.NET中呈现页面时会出现问题,因为我想确保我将字符串解码为数据库中的字符串,因为我正在编写它.当然,描述中有一些NULL.我希望保留的字段.
所以我的ASP.NET页面有这个代码
<asp:TextBox ID="DescInfo" Text='<%# HttpUtility.HtmlDecode((string)Eval("Description")) %>' />
Run Code Online (Sandbox Code Playgroud)
所以当我渲染页面时,我会得到:
Unable to cast object of type 'System.DBNull' to type 'System.String'.
Run Code Online (Sandbox Code Playgroud)
我看待它的方式,我有两个选择:
要么
任何人都有更好的想法吗?
Linq的新手,如果这是基本的,请道歉.此查询抛出错误{"无法将DBNull.Value强制转换为'System.Int64'.请枚举结果时使用可空类型."}.
private void AddLevels(long rootid)
{
var results = from row in data.AsEnumerable()
where row.Field<long>("ParentID") == rootid
select row;
foreach (DataRow row in results)
{
//do stuff
}
}
Run Code Online (Sandbox Code Playgroud)
列ParentID确实接受空值 - 我需要单独处理这些吗?
EDIT2:下面的实际解决方案仍然使用Linq.
编辑:我通过废除Linq并仅使用DataTable.Select语句解决了这个问题.如果有人对性能差异有所了解我会感兴趣.
我有问题比较空值与oracle 11g odp.net驱动程序.它适用于oracle 10g odp.net驱动程序.如果数据库中的列为null,则在datarow中,其字符串值为null.
此代码失败:
int parentId = row[PARENTID] != DBNull.Value ? int.Parse(row[PARENTID].ToString()) : 0;
Run Code Online (Sandbox Code Playgroud)
谢谢
我有一个可以为空的DateTime字段"BirthDate",现有代码以这种方式处理这个问题
info.BirthDate = (DateTime?)reader["Birthdate"];
Run Code Online (Sandbox Code Playgroud)
这会导致"无效转换"错误,并且会中断.好吧,我理解这是因为nulls的返回方式与sql不同,并且类型为"DBNull"
结果证明是这样的
if (reader["Birthdate"] != DBNull.Value)
{
info.Birthdate = (DateTime)reader["Birthdate"];
}
Run Code Online (Sandbox Code Playgroud)
有人可以解释为什么这个有效吗?..我特别迷失在DBNull的.Value部分.如果IS返回作为的DBNull如何代码连这个块内到达?
我试图用我的查询返回的列表填充组合框.当我执行我的程序时,它给我一个指定的强制转换无效错误(我在页面加载事件上执行).除了主键之外,我必须使用的数据库中的每个字段都可以为null.所以我尝试使用DBNull.Value但它无法使我的(int)阅读器字段起作用.我已提供下面的代码以便更好地理解.如何让我的(int)读者使用我的语句,以便在有空值时可以读取?
CustData cd = new CustData();
cd.CustomerID = (int)reader["CustomerID"];
cd.Name = reader["Name"] != DBNull.Value ? reader["Name"].ToString() : string.Empty;
cd.ShippingAddress = reader["ShippingAddress"] != DBNull.Value ? reader["ShippingAddress"].ToString() : string.Empty;
cd.ShippingCity = reader["ShippingCity"] != DBNull.Value ? reader["ShippingCity"].ToString() : string.Empty;
cd.ShippingState = reader["ShippingState"] != DBNull.Value ? reader["ShippingState"].ToString() : string.Empty;
cd.ShippingZip = (int)reader["ShippingZip"];
cd.BillingAddress = reader["BillingAddress"] != DBNull.Value ? reader["BillingAddress"].ToString() : string.Empty;
cd.BillingCity = reader["BillingCity"] != DBNull.Value ? reader["BillingCity"].ToString() : string.Empty;
cd.BillingState = reader["BillingState"] != DBNull.Value ? reader["BillingState"].ToString() : string.Empty;
cd.BillingZip = (int)reader["BillingZip"];
cd.Territory = …Run Code Online (Sandbox Code Playgroud) 我正在尝试单行if语句如下,我有理由得到错误.我该怎么做?
int? n;
n = (reader[0] == null)? null : Convert.ToInt32(reader[0]);
Run Code Online (Sandbox Code Playgroud) 在有人评论之前已经在其他问题上回答过这个问题之前,我知道这一点......但是尽管我已经回顾了这些答案
甚至我自己的问题
我无法让我的查询返回带有null参数的值
我已经尝试简化我的代码,因此可以在这里看到.
我也试过这个
int? i = null;
SqlConnection connection = new SqlConnection(Properties.Settings.Default.connstring.ToString());
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
cmd.CommandText = "Select * from view_nests where parent_pk = @parent_pk";
cmd.Parameters.AddWithValue("@parent_pk", i ?? Convert.DBNull);
cmd.Connection.Open();
var dataReader = cmd.ExecuteReader();
var dataTable = new DataTable();
dataTable.Load(dataReader);
cmd.Connection.Close();
Run Code Online (Sandbox Code Playgroud)
我尝试过这方面的变化
cmd.Parameters.AddWithValue("@parent_pk", DBNull.Value);
Run Code Online (Sandbox Code Playgroud)
我尝试过使用该查询
cmd.CommandText = "Select * from view_nests where parent_pk = @parent_pk or @parent_pk Is Null";
Run Code Online (Sandbox Code Playgroud)
我尝试将参数明确声明为可为空
cmd.Parameters.AddWithValue("@parent_pk", i ?? Convert.DBNull).IsNullable = true; …Run Code Online (Sandbox Code Playgroud) 我有一个类型的对象:
dynamic {System.DBNull}
Run Code Online (Sandbox Code Playgroud)
我想检查一下:
if (myObject!= null || myObject!= DBNull.Value)
{
MessageBox.Show("Oh hi");
}
Run Code Online (Sandbox Code Playgroud)
但MessageBox总会出现.什么是错的,是另一种类型吗?
我想我应该使用类似的东西,但我不知道如何修改它以适用于DateTime,因为这样做它告诉我DateTime不能为null.那么我该如何改变这个或者有更好的方法来解决这个问题呢?先感谢您.
我想要设置什么
startDateTime = RadDateTimePickerBegin.SelectedDate;
Run Code Online (Sandbox Code Playgroud)
我认为它看起来应该类似于
RadDateTimePickerBegin.SelectedDate == What goes here(string.Empty) ? (object)DBNull.Value : RadDateTimePickerBegin.SelectedDate);
Run Code Online (Sandbox Code Playgroud)