Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 财产';项目';是';只读';_Vb.net - Fatal编程技术网

Vb.net 财产';项目';是';只读';

Vb.net 财产';项目';是';只读';,vb.net,Vb.net,我想知道是否有人能帮助我。我对编程和开发非常陌生(如果这是一个愚蠢的问题,我很抱歉),但我在一个我目前正在进行的项目上陷入了困境 我正在编写一个WinForms应用程序,该应用程序将每天运行一次,以从数据库检索信息。因此,如果满足特定标准,将自动向大楼内的个人发送电子邮件。消息体将包含引入数据网格的各种细节 目前,当我的应用程序试图将详细信息传递给负责生成电子邮件的子系统时,它正在崩溃(因为一些变量返回为“Null”,因此无法转换为字符串)。我试图实现一个If语句,在变量返回为Null时为其分配

我想知道是否有人能帮助我。我对编程和开发非常陌生(如果这是一个愚蠢的问题,我很抱歉),但我在一个我目前正在进行的项目上陷入了困境

我正在编写一个WinForms应用程序,该应用程序将每天运行一次,以从数据库检索信息。因此,如果满足特定标准,将自动向大楼内的个人发送电子邮件。消息体将包含引入数据网格的各种细节

目前,当我的应用程序试图将详细信息传递给负责生成电子邮件的子系统时,它正在崩溃(因为一些变量返回为“Null”,因此无法转换为字符串)。我试图实现一个If语句,在变量返回为Null时为其分配一个字符串,如下所示:

Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles RunTimer.Tick

    years = My.Settings.Years.Split(",")

    If TimeOfDay = "09:00:00" Then
        Using cn As New SqlClient.SqlConnection
            cn.ConnectionString = constr
            cn.Open()

            SQLstr = "long SQL string that works as have run it in WinSQL"

            Using command As New SqlClient.SqlCommand(SQLstr, cn)
                command.CommandTimeout = 0
                command.CommandText = SQLstr
                Using drd As SqlClient.SqlDataReader = command.ExecuteReader
                    While drd.Read

                        If Not drd("Joined Date") Is DBNull.Value Then
                            Dim memberyears As Integer = DateDiff(DateInterval.Year, drd("Joined Date"), Now)
                            Dim element As Integer

                            For element = 0 To years.GetUpperBound(0)
                                If years(element) = memberyears Then
                                    Label1.Text = "Sending email"
                                End If
                                If Not drd("City") Is DBNull.Value Then
                                    drd("City") = "Bleh"
                                End If
                                SendEmail(years(element), drd("Shop Name"), drd("Contact Name"), drd("Shop ID"), drd("Business Name"), drd("Address 1"), drd("Address 2"), drd("Address 3"), drd("District"), drd("City"), drd("County"), drd("Shop Postal Code"), drd("ASM"))
                            Next
                        End If
                    End While
                End Using
            End Using
        End Using


    Else
        Label1.Text = "Skipped"
        Exit Sub
    End If

End Sub
我正在处理的
If
语句如下:

If Not drd("City") Is DBNull.Value Then
    drd("City") = "Bleh"
End If

但是,它只是返回属性“Item”为只读。在此方面如有任何帮助,将不胜感激。

是的。它是一个
只读
属性

解决方法是使用
变量
保存
数据库
中的值。但是,请注意,在分配给该
变量
之前,我们必须检查它是否为
DbNull

       Dim xCity as string = string.empty

       ....................
       ....................
       ....................

       xCity = if(IsDbNull(drd("City")),"some text","some other text")
然后将该变量
xCity
用于将来的使用

编辑:

如果您想避免那个额外的变量,那么您可以直接检查那个值,并像这样传递一个有效字符串

SendEmail(**,**, if(IsDbNull(drd("City")),"some text","some other text"), **, **, **)

不能更改
SqlDataReader
记录的字段。
DataReader
仅用于读取数据。如果要修改它,可以使用
DataTable
/
DataRow
并使用它的方法检查字段的值是否为空:

Using cn As New SqlClient.SqlConnection(constr)
    Using da = New SqlClient.SqlDataAdapter("long SQL string that works as have run it in WinSQL", cn)
        da.SelectCommand.CommandTimeout = 0
        Dim table = New DataTable()
        da.Fill(table)
        For Each row As DataRow In table.Rows
            If Not row.IsNull("Joined Date") Then
                Dim joined = row.Field(Of Date)("Joined Date")
                Dim memberyears As Long = DateDiff(DateInterval.Year, joined, Date.Now)
                For i As Int32 = 0 To years.Length - 1
                    If memberyears.ToString = years(i) Then
                        Label1.Text = "Sending email"
                        If Not row.IsNull("City") Then
                            row("City") = "Bleh"
                        End If
                        SendEmail(years(i), row("Shop Name"), row("Contact Name"), row("Shop ID"), row("Business Name"), row("Address 1"), row("Address 2"), row("Address 3"), row("District"), row("City"), row("County"), row("Shop Postal Code"), row("ASM"))
                    End If
                Next
            End If
        Next
    End Using
End Using

这似乎有很大的逻辑意义,但是当我尝试调试它时,它抱怨说它无法从空值转换为字符串,即使使用该字符串,它返回“从类型“DBNull”转换为类型“string”无效”。@ChrisWright你是否尝试过使用.ToString(),比如drd(“City”).ToString()。在SendEmail的行中,我只是复制了您提供的代码,以覆盖drd(“城市”)的旧代码@ChrisWright您现在面临任何问题吗。?如果是,请准确地告诉我,错误在哪一行出现。:)谢谢你的回答:)我可以假设如果还有其他变量,比如县、区等,那么我可以使用类似于城市的if语句吗?@ChrisWright:可以。请注意,您还应该使用
行.Field(字符串的)(“City”)
,因为是强类型(不返回
对象
)并且还支持
可为空的类型。因此,您甚至可以编写:
row.Field(Of Date)(“Joined Date”).HasValue
,以检查日期是否为空。我尝试添加所有可能为空的其他变量,尽管这对其中一些变量来说现在工作得很好,但对其他变量,空值仍在传输?当然:如果不是row.IsNull(“地址2”)如果不是行,则行(“地址2”)=“Bleh”结束。如果不是行,则为空(“地址3”),如果地址2传输“Bleh”,则行(“地址3”)=“无效”结束,但是地址3仍然返回为Null@ChrisWright:如果
行(“地址3”)
确实是
DBNull.Value
,则这是不可能的。您确定它是null(使用调试器)还是可能只是空的(一个或多个空格字符)。然后您需要以下代码:
如果String.IsNullOrWhiteSpace(字符串的row.Field)(“地址3”)),则row.SetField(“地址3”,“无效”)结束,如果