vb.net 属性“项目”是“只读”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15851261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 13:08:36  来源:igfitidea点击:

Property 'Item' is 'ReadOnly'

vb.net

提问by Chris Wright

I wonder if someone could assist me. I'm very new to programming and development (and so I apologise if this is a silly question) but I am pretty stuck on a project I am currently working on.

我想知道是否有人可以帮助我。我对编程和开发很陌生(如果这是一个愚蠢的问题,我深表歉意),但我非常坚持我目前正在从事的项目。

I am coding a WinForms application which is due to run once a day to retrieve information from a database. From this, it is to automatically email individuals in the building if particular criteria is met. The message body is to contain various details that were brought into the datagrid.

我正在编写一个 WinForms 应用程序,该应用程序每天运行一次以从数据库中检索信息。由此,如果满足特定条件,则自动向建筑物中的个人发送电子邮件。消息正文将包含带入数据网格的各种详细信息。

At the moment, my application is crashing when it tries to transfer the details over to the sub responsible for generating the email (as some of the variables are returning as 'Null' and so cannot be converted to a string). I am trying to implement an If statement to assign a string to the variable if it returns as Null, as below:

目前,我的应用程序在尝试将详细信息传输到负责生成电子邮件的子程序时崩溃(因为某些变量返回为“空”,因此无法转换为字符串)。我正在尝试实现一个 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

The Ifstatement I am working on is this:

If我正在处理的声明是这样的:

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

However it just returns as Property 'Item' is ReadOnly. Any help would be greatly appreciated on this.

然而,它只是返回,因为属性“Item”是只读的。任何帮助将不胜感激。

采纳答案by Tim Schmelter

You cannot change a field of a record of a SqlDataReader. The DataReaderis used to read data only. If you want to modify it you ca use a DataTable/DataRowand use it's IsNullmethod to check if the value of a field is null:

您不能更改 .a 记录的字段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

回答by Rajaprabhu Aravindasamy

Yes it is. It is a read onlyproperty,

是的。它是一种read only财产,

Solution for this will be having a variableto hold the value from database.But, note this, before assigning to that variable, we must check whether it is DbNullor not.

对此的解决方案是使用 avariable来保存database. 但是,请注意这一点,在分配给 that 之前variable,我们必须检查它是否DbNull存在。

       Dim xCity as string = string.empty

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

       xCity = if(IsDbNull(drd("City")),"some text","some other text")

Then use that variable xCityfor your future usage.

然后使用该变量xCity供您将来使用。

Edit:

编辑:

If suppose you want to avoid that additional variable, then you can directly check that value and pass a valid string like this,

如果假设您想避免使用该附加变量,则可以直接检查该值并传递这样的有效字符串,

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