属性'Item'是'ReadOnly'

时间:2013-04-06 12:59:21

标签: vb.net

我想知道是否有人可以帮助我。我对编程和开发很陌生(所以如果这是一个愚蠢的问题,我很抱歉)但我仍然坚持我正在进行的项目。

我正在编写一个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'是ReadOnly。任何帮助将非常感谢。

2 个答案:

答案 0 :(得分:2)

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

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

答案 1 :(得分:0)

是的。它是read only属性,

解决此问题的方法是variable保留database的值。但请注意,在分配给variable之前,我们必须检查它是否为{{1}或者没有。

DbNull

然后使用该变量 Dim xCity as string = string.empty .................... .................... .................... xCity = if(IsDbNull(drd("City")),"some text","some other text") 以供将来使用。

修改

如果您想避免使用其他变量,那么您可以直接检查该值并传递一个有效的字符串,

xCity