错误:可为空的对象必须具有一个值 [英] Error: Nullable object must have a value

查看:290
本文介绍了错误:可为空的对象必须具有一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在整数类型的字段中输入数据NULL(而不是数字等).我使用以下代码:

I want to enter data NULL (rather than numeric numbers, etc.) to the field of type integer. I use the following code:

Dim Col2 As Nullable(Of Integer) = Nothing
OpenDatabase()
Command.Connection = Con
Command.CommandType = CommandType.Text
Command.CommandText = "INSERT INTO SIGN(TYPESIGN_ID) VALUES('" + CStr(Col2) + "')"
Command.ExecuteNonQuery()
Con.Close()



结果是可为空的对象必须具有值".我的错误在哪里?
注意:我正在使用VB .Net 2010& sql server 2005



the result is "Nullable object must have a value". where my mistake?
Note : I''m using VB .Net 2010 & sql server 2005

推荐答案

不要在第一行中写"什么"

您可以编写如下的插入查询,
don''t write ''nothing'' in first line
or
you can write insert query like below,
"INSERT INTO SIGN(TYPESIGN_ID) VALUES(" + "null" + ")"


祝您编码愉快!
:)


Happy Coding!
:)


hiii,


hiii,


To retrieve the value of a variable of a nullable type, you should first test its HasValue property to confirm that it has a value. If you try to read the value when HasValue is False, Visual Basic throws an InvalidOperationException exception. The following example shows the recommended way to read the variable Col2

If Col2.HasValue Then
    Command.CommandText = "INSERT INTO SIGN(TYPESIGN_ID) VALUES('" + CStr(Col2) + "')"
End If


如果为空对象,则无需使用CStr.
否则,请在使用CStr之前检查Col2是否不为空.
If this is a nullable object, you don''t need to use CStr.
Or else check if Col2 is not null before using CStr.


这篇关于错误:可为空的对象必须具有一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆