为什么IsDBNull没有检测到DBNull? [英] Why doesn't IsDBNull detect DBNull?

查看:53
本文介绍了为什么IsDBNull没有检测到DBNull?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这行代码返回错误13,从''DBNull''转换为''String''类型无效


如果IsDBNull(Clinics.Clinics.Item(A) )。工作电话)


<其他代码

结束我


Clinics.Clinics是一个数据集,使用两个MS SQL数据库表中的左连接加载。 workphone元素为null,因为从连接的右侧没有相应的记录


生成的用于检索工作电话的值作为数据集属性的代码正在检测DBNull并在VB应用程序代码行之前给出了强制转换错误


我必须遗漏一些东西 - 有一个函数''IsDBNull''无法检查DBNull不一致


Ji

This line of code returns error 13, cast from ''DBNull'' to type ''String'' is not valid

If IsDBNull(Clinics.Clinics.Item(A).Workphone) The

<other code
End I

Clinics.Clinics is a dataset that was loaded using a left join from two MS SQL database tables. The workphone element is null because there was no corresponding record from the right side of the join

The generated code for retrieving the value of ''Workphone'' as a dataset property is detecting the DBNull and giving the cast error before the VB application code line can do its check

I have to be missing something - having a function ''IsDBNull'' that is not able to check for a DBNull is inconsisten

Ji

推荐答案




无论如何,我不认为它是IsDbNull导致你的问题,它可以

绝对检查空值。我认为问题出在你的财产中。

我需要看看访问者b / c它不会让你明确设置一个字符串

值到DbNull,但取决于属性,它可能最终会尝试执行

只会导致强制转换异常。看看这个用法和

那个例外,你所做的只是评估,所以转换是在parens里面完成的。因此,它更有可能导致异常,而不是使用IsDbNull。


你能先发表这个声明吗?毫无例外

MessageBox.Show(Clinics.Clinics.Item(A).WorkPhone)''假设workphone是一个

字符串?


无论如何,如果我能看到访问者,它会有所帮助,但我99%肯定这是

问题...无论用什么来设置属性类型字符串是DbNull

并且不起作用。


我一直使用IsDbNull来检查DB中的Varchar字段,

因此映射到System.String为datacolumn类型,并且从未有过

问题。我真的认为这是访问者。如果messagebox命令

引发异常,那么我们几乎排除了IsDbNull为

的原因。

jim <一个******* @ discussions.microsoft.com>在消息中写道

news:87 ********************************** @ microsof t.com ...


Anyway, I don''t think it''s the IsDbNull causing your problem and it can
definitely check for null values. I think the problem is in your Property.
I''d need to see the accessor b/c it won''t let you explicitly set a string
value to DbNull, but depending on the property it could end up trying to do
just that resulting in the cast exception. Looking at the usage too and
that exception, all you are doing is an evaluation so the conversion is
being done inside the parens. As such, it''s much more likely that it''s
causing the exception rather than the use of IsDbNull.

can you first this statement without an exception
MessageBox.Show(Clinics.Clinics.Item(A).WorkPhone) ''Assuming workphone is a
string?

Anyway, it''d help if I could see the accessor, but I''m 99% sure this is the
problem...whatever is being used to set a property of type string is DbNull
and that won''t work.

I use IsDbNull all the time to check for fields that are Varchar in the DB,
hence map to System.String for the datacolumn type and have never had a
problem. I really think it''s the accessor. If the messagebox command
throws an exception , then we''ve pretty much ruled out the IsDbNull as the
cause.

"jim" <an*******@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
这行代码返回错误13,从''DBNull''转换为类型''String''是
无效。
如果是IsDBNull (Clinics.Clinics.Item(A).Workphone)然后

<其他代码>
结束如果

Clinics.Clinics是一个使用加载的数据集两个MS
SQL数据库表的左连接。 workphone元素为null,因为从连接的右侧没有

对应的记录。
生成的代码用于检索''Workphone'的值作为数据集
属性检测到DBNull并在VB
应用程序代码行可以检查之前给出了转换错误。
我必须遗漏一些东西 - 有一个函数''IsDBNull''是不是
能够检查DBNull是否不一致
Jim
This line of code returns error 13, cast from ''DBNull'' to type ''String'' is not valid.
If IsDBNull(Clinics.Clinics.Item(A).Workphone) Then

<other code>
End If
Clinics.Clinics is a dataset that was loaded using a left join from two MS SQL database tables. The workphone element is null because there was no
corresponding record from the right side of the join.
The generated code for retrieving the value of ''Workphone'' as a dataset property is detecting the DBNull and giving the cast error before the VB
application code line can do its check.
I have to be missing something - having a function ''IsDBNull'' that is not able to check for a DBNull is inconsistent
Jim






无论如何,我不认为它是IsDbNull导致你的问题,它可以

绝对检查空值。我认为问题出在你的财产中。

我需要看看访问者b / c它不会让你明确设置一个字符串

值到DbNull,但取决于属性,它可能最终会尝试执行

只会导致强制转换异常。看看这个用法和

那个例外,你所做的只是评估,所以转换是在parens里面完成的。因此,它更有可能导致异常,而不是使用IsDbNull。


你能先发表这个声明吗?毫无例外

MessageBox.Show(Clinics.Clinics.Item(A).WorkPhone)''假设workphone是一个

字符串?


无论如何,如果我能看到访问者,它会有所帮助,但我99%肯定这是

问题...无论用什么来设置属性类型字符串是DbNull

并且不起作用。


我一直使用IsDbNull来检查DB中的Varchar字段,

因此映射到System.String为datacolumn类型,并且从未有过

问题。我真的认为这是访问者。如果messagebox命令

引发异常,那么我们几乎排除了IsDbNull为

的原因。

jim <一个******* @ discussions.microsoft.com>在消息中写道

news:87 ********************************** @ microsof t.com ...


Anyway, I don''t think it''s the IsDbNull causing your problem and it can
definitely check for null values. I think the problem is in your Property.
I''d need to see the accessor b/c it won''t let you explicitly set a string
value to DbNull, but depending on the property it could end up trying to do
just that resulting in the cast exception. Looking at the usage too and
that exception, all you are doing is an evaluation so the conversion is
being done inside the parens. As such, it''s much more likely that it''s
causing the exception rather than the use of IsDbNull.

can you first this statement without an exception
MessageBox.Show(Clinics.Clinics.Item(A).WorkPhone) ''Assuming workphone is a
string?

Anyway, it''d help if I could see the accessor, but I''m 99% sure this is the
problem...whatever is being used to set a property of type string is DbNull
and that won''t work.

I use IsDbNull all the time to check for fields that are Varchar in the DB,
hence map to System.String for the datacolumn type and have never had a
problem. I really think it''s the accessor. If the messagebox command
throws an exception , then we''ve pretty much ruled out the IsDbNull as the
cause.

"jim" <an*******@discussions.microsoft.com> wrote in message
news:87**********************************@microsof t.com...
这行代码返回错误13,从''DBNull''转换为类型''String''是
无效。
如果是IsDBNull (Clinics.Clinics.Item(A).Workphone)然后

<其他代码>
结束如果

Clinics.Clinics是一个使用加载的数据集两个MS
SQL数据库表的左连接。 workphone元素为null,因为从连接的右侧没有

对应的记录。
生成的代码用于检索''Workphone'的值作为数据集
属性检测到DBNull并在VB
应用程序代码行可以检查之前给出了转换错误。
我必须遗漏一些东西 - 有一个函数''IsDBNull''是不是
能够检查DBNull是否不一致
Jim
This line of code returns error 13, cast from ''DBNull'' to type ''String'' is not valid.
If IsDBNull(Clinics.Clinics.Item(A).Workphone) Then

<other code>
End If
Clinics.Clinics is a dataset that was loaded using a left join from two MS SQL database tables. The workphone element is null because there was no
corresponding record from the right side of the join.
The generated code for retrieving the value of ''Workphone'' as a dataset property is detecting the DBNull and giving the cast error before the VB
application code line can do its check.
I have to be missing something - having a function ''IsDBNull'' that is not able to check for a DBNull is inconsistent
Jim



William

感谢您的回复


我尝试了消息框并得到了同样的错误。 WorkPhone是一个字符串。


访问者具体是什么意思?您是指的是检索数据的SQL语句或数据集的xml代码,还是MS生成的.vb代码,它将值作为数据集中的属性或我不知道的其他内容获取?我在xml下面包含了填充的sql语句,以及从数据集中检索数据的代码片段。错误13是在填充相同子例程的代码中生成的。


我们可能对开发环境的正确用法有一个非常基本的误解来定义数据集


我们使用SQLDataAdapter并根据我们检索的数据重新配置GenAdapter selectcommand.commandtext。通过设置适配器的select命令,然后生成数据集来定义数据集。在执行时,我们填写适当的数据集。以下是我们遇到问题的数据集之一的xml副本


<?xml version =" 1.0"独立= QUOT;是" ?>< xs:schema id =" Clinics"的targetNamespace = QUOT; HTTP://www.tempuri.org/Clinics.xsd" xmlns:mstns =" http://www.tempuri.org/Clinics.xsd

xmlns =" http://www.tempuri.org/Clinics.xsd" xmlns:xs =" http://www.w3.org/2001/XMLSchema

xmlns:msdata =" urn:schemas-microsoft-com:xml-msdata" attributeFormDefault = QUOT;合格" elementFormDefault =" qualified">< xs:element name =" Clinics" msdata:IsDataSet =" true">< xs:complexType>< xs:choice maxOccurs =" unbounded">< xs:element name =" Clinics">< xs:complexType>< xs:sequence>< xs:el ement name =" ClinicNum"类型= QUOT; XS:短" />< xs:element name =" ClinicID"类型= QUOT; XS:串QUOT; />< xs:element name =" ShortName"类型= QUOT; XS:串QUOT; />< xs:element name =" LongName"类型= QUOT; XS:串QUOT; />< xs:element name =" Root"类型= QUOT; XS:短" />< xs:element name =" Group"类型= QUOT; XS:串QUOT; />< xs:element name =" Discount"类型= QUOT; XS:串QUOT; />< xs:element name =" License"类型= QUOT; XS:串QUOT; />< xs:element name =" Doctor"类型= QUOT; XS:串QUOT; />< xs:element name =" DirectorName"类型= QUOT; XS:串QUOT; />< xs:element name =" DirectorTitle"类型= QUOT; XS:串QUOT; />< xs:element name =" webURL"类型= QUOT; XS:串QUOT; />< xs:element name =" AltContact"类型= QUOT; XS:串QUOT; />< xs:element name =" AltContactTitle"类型= QUOT; XS:串QUOT; />< xs:element name =" Billing"类型= QUOT; XS:串QUOT; />< xs:element name =" Expr1"类型= QUOT; XS:串QUOT; />< xs:element name =" Expr2"类型= QUOT; XS:日期时间" />< xs:element name =" Expr3"类型= QUOT; XS:短" />< xs:element name =" Expr4" MSDATA:只读= QUOT;真" MSDATA:自动递增= QUOT;真"类型= QUOT; XS:INT" />< xs:element name =" Expr5"类型= QUOT; XS:INT" />< xs:element name =" BtBID"类型= QUOT; XS:INT" />< xs:element name =" branch"类型= QUOT; XS:串QUOT; />< xs:element name =" addresstype"类型= QUOT; XS:串QUOT; />< xs:element name =" addressline1"类型= QUOT; XS:串QUOT; />< xs:element name =" streetnum"类型= QUOT; XS:串QUOT; />< xs:element name =" Streetname"类型= QUOT; XS:串QUOT; />< xs:element name =" Apt"类型= QUOT; XS:串QUOT; />< xs:element name =" City"类型= QUOT; XS:串QUOT; />< xs:element name =" State"类型= QUOT; XS:串QUOT; />< xs:element name =" Postalcode"类型= QUOT; XS:串QUOT; />< xs:element name =" Latitude"类型= QUOT; XS:串QUOT; />< xs:element name ="经度"类型= QUOT; XS:串QUOT; />< xs:element name =" County"类型= QUOT; XS:串QUOT; />< xs:element name =" Homephone"类型= QUOT; XS:串QUOT; />< xs:element name =" Workphone"类型= QUOT; XS:串QUOT; />< xs:element name =" FAX"类型= QUOT; XS:串QUOT; />< xs:element name =" Pager"类型= QUOT; XS:串QUOT; />< xs:element name =" email"类型= QUOT; XS:串QUOT; />< xs:element name =" altphone"类型= QUOT; XS:串QUOT; />< xs:element name =" AltFAX"类型= QUOT; XS:串QUOT; />< xs:element name =" AltPager"类型= QUOT; XS:串QUOT; />< xs:element name =" altemail"类型= QUOT; XS:串QUOT; />< xs:element name =" Startdate"类型= QUOT; XS:日期时间" />< xs:element name =" Enddate"类型= QUOT; XS:日期时间" />< xs:element name =" Entrydate"类型= QUOT; XS:日期时间" />< xs:element name =" EnteredBy"类型= QUOT; XS:短" />< / xs:sequence>< / xs:complexType>< / xs:element>< / xs:choice>< / xs:complexType>< xs:unique name =" Constraint1" msdata:PrimaryKey =" true">< xs:selector xpath =" .// mstns:Clinics" />< xs:field xpath =" mstns:Expr5" />< xs:field xpath =" mstns:addresstype" />< / xs:unique>< / xs:element>< / xs:schema


这里是从数据集中检索数据的代码。这是一个来自Clinics.v的片段

Public Property Workphone As String

获取

返回CType(Me(Me.tableClinics.WorkphoneColumn) ,String)

结束获取

设置

Me(Me.tableClinics.WorkphoneColumn)= value

结束集

结束属性


最后,这是用于填充数据集的sql字符串和后续代码。


SQLString =" SELECT * FROM clinics"

SQLString = SQLString +"在Address.BtBiD上的加入地址= Clinics.BtbID"

SQLString = SQLString +"并且AddressType =" + Chr(39)+CLN + Chr(39)

SQLString = SQLString +"其中Clinics.Branch =" + Chr(39)+ Branch + Chr(39)

SQLString = SQLString +"通过短名称命令


GenAdapter.SelectCommand.CommandText = SQLString

Clinics.Clinics.Clear()


尝试

GenAdapter.Fill(Clinics.Clinics)

Catch e1 As Exception

MsgBox(填充ClinicsDataSet错误+ Chr(10) + e1.Message,MsgBoxStyle.OKOnly)

结束尝试

William

Thank you for the reply

I tried the message box and got the same error. WorkPhone is a string.

What specifically do you mean by the ''accessor''? Are you referring to the SQL statement that retrieves the data or the xml code for the dataset or the MS generated .vb code that gets the values as properties from the dataset or something else that I do not know about? I am including below the xml, the sql statement for the fill, and a snippet from the code that retrieves the data from the dataset. The error 13 is generated furhter down in the code in the same subroutine that does the fill

We may have a very basic misunderstanding of the proper usage of the development environment to define datasets

We use an SQLDataAdapter and reconfigure the GenAdapter selectcommand.commandtext depending on the data we are retrieving. The dataset has been defined by setting the adapter''s select command, then generating the dataset. At execution time we do a fill of the appropriate dataset. Here is a copy of the xml from one of the datasets that we are having trouble with

<?xml version="1.0" standalone="yes" ?><xs:schema id="Clinics" targetNamespace="http://www.tempuri.org/Clinics.xsd" xmlns:mstns="http://www.tempuri.org/Clinics.xsd
xmlns="http://www.tempuri.org/Clinics.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema
xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" attributeFormDefault="qualified" elementFormDefault="qualified"><xs:element name="Clinics" msdata:IsDataSet="true"><xs:complexType><xs:choice maxOccurs="unbounded"><xs:element name="Clinics"><xs:complexType><xs:sequence><xs:el ement name="ClinicNum" type="xs:short" /><xs:element name="ClinicID" type="xs:string" /><xs:element name="ShortName" type="xs:string" /><xs:element name="LongName" type="xs:string" /><xs:element name="Root" type="xs:short" /><xs:element name="Group" type="xs:string" /><xs:element name="Discount" type="xs:string" /><xs:element name="License" type="xs:string" /><xs:element name="Doctor" type="xs:string" /><xs:element name="DirectorName" type="xs:string" /><xs:element name="DirectorTitle" type="xs:string" /><xs:element name="webURL" type="xs:string" /><xs:element name="AltContact" type="xs:string" /><xs:element name="AltContactTitle" type="xs:string" /><xs:element name="Billing" type="xs:string" /><xs:element name="Expr1" type="xs:string" /><xs:element name="Expr2" type="xs:dateTime" /><xs:element name="Expr3" type="xs:short" /><xs:element name="Expr4" msdata:ReadOnly="true" msdata:AutoIncrement="true" type="xs:int" /><xs:element name="Expr5" type="xs:int" /><xs:element name="BtBID" type="xs:int" /><xs:element name="branch" type="xs:string" /><xs:element name="addresstype" type="xs:string" /><xs:element name="addressline1" type="xs:string" /><xs:element name="streetnum" type="xs:string" /><xs:element name="Streetname" type="xs:string" /><xs:element name="Apt" type="xs:string" /><xs:element name="City" type="xs:string" /><xs:element name="State" type="xs:string" /><xs:element name="Postalcode" type="xs:string" /><xs:element name="Latitude" type="xs:string" /><xs:element name="Longitude" type="xs:string" /><xs:element name="County" type="xs:string" /><xs:element name="Homephone" type="xs:string" /><xs:element name="Workphone" type="xs:string" /><xs:element name="FAX" type="xs:string" /><xs:element name="Pager" type="xs:string" /><xs:element name="email" type="xs:string" /><xs:element name="altphone" type="xs:string" /><xs:element name="AltFAX" type="xs:string" /><xs:element name="AltPager" type="xs:string" /><xs:element name="altemail" type="xs:string" /><xs:element name="Startdate" type="xs:dateTime" /><xs:element name="Enddate" type="xs:dateTime" /><xs:element name="Entrydate" type="xs:dateTime" /><xs:element name="EnteredBy" type="xs:short" /></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType><xs:unique name="Constraint1" msdata:PrimaryKey="true"><xs:selector xpath=".//mstns:Clinics" /><xs:field xpath="mstns:Expr5" /><xs:field xpath="mstns:addresstype" /></xs:unique></xs:element></xs:schema

And here is the code that retrieves the data from the dataset This is a snippet from Clinics.v

Public Property Workphone As String
Get
Return CType(Me(Me.tableClinics.WorkphoneColumn),String)
End Get
Set
Me(Me.tableClinics.WorkphoneColumn) = value
End Set
End Property

Finally, here is the sql string and subsequent code used to fill the dataset.

SQLString = "SELECT * FROM clinics "
SQLString = SQLString + " left Join Address on Address.BtBiD=Clinics.BtbID"
SQLString = SQLString + " And AddressType=" + Chr(39) + "CLN" + Chr(39)
SQLString = SQLString + " where Clinics.Branch = " + Chr(39) + Branch + Chr(39)
SQLString = SQLString + " order by shortname"

GenAdapter.SelectCommand.CommandText = SQLString
Clinics.Clinics.Clear()

Try
GenAdapter.Fill(Clinics.Clinics)
Catch e1 As Exception
MsgBox("Error filling ClinicsDataSet" + Chr(10) + e1.Message, MsgBoxStyle.OKOnly)
End Try


这篇关于为什么IsDBNull没有检测到DBNull?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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