你调用的对象是空的。 splitItems(0)的ToString

时间:2012-02-01 06:25:24

标签: asp.net vb.net gridview split

我有这个按钮,我在数据行添加但是,它给我错误,任何人都可以帮我告诉我为什么是splitItems(0).ToString导致错误,我应该如何获得该值存储在数据库

Protected Sub Button2_Click(sender As Object, e As System.EventArgs) Handles Button2.Click

        Dim rowIndex As Integer = 0
        Dim sc As New StringCollection()
        If ViewState("CurrentTable") IsNot Nothing Then
            Dim dtCurrentTable As DataTable = DirectCast(ViewState("CurrentTable"), DataTable)
            Dim drCurrentRow As DataRow = Nothing
            If dtCurrentTable.Rows.Count < 10 Then
                For i As Integer = 1 To dtCurrentTable.Rows.Count
                    'extract the TextBox values

                    Dim box5 As TextBox = DirectCast(Gridview3.Rows(rowIndex).Cells(1).FindControl("TextBox5"), TextBox)

                    'get the values here

                    Dim box6 As Date
                    box6 = Convert.ToDateTime(box5.Text)
                    Dim box7 As Date = Convert.ToDateTime(box5.Text)



                    sc.Add(box6)



                    rowIndex += 1
                Next

                InsertRecords(sc)
            End If
        Else
            '   lblMessage.Text = "Cannot save as there no information recorded"
            MsgBox("failed")
        End If
    End Sub

 Protected Sub InsertRecords(sc As StringCollection)



        Dim sb As New StringBuilder(String.Empty)

        Dim splitItems As String() = Nothing

        For Each item As String In sc


            If item.Contains(",") Then
                splitItems = item.Split(",".ToCharArray())




            End If
        Next

        Try

            Dim myConn As New SqlConnection
            Dim myCmd As New SqlCommand
            myConn.ConnectionString = ConfigurationManager.ConnectionStrings("mydatabase").ConnectionString
            Dim cmd As String
            cmd = "Insert into Date values (@date) "
            myCmd.CommandText = cmd
            myCmd.CommandType = CommandType.Text
           //error found at this line splitItems(0).ToString()
            myCmd.Parameters.Add(New SqlParameter("@date", splitItems(0).ToString()))


            myCmd.Connection = myConn
            myConn.Open()
            myCmd.ExecuteNonQuery()
            myCmd.Dispose()
            myConn.Dispose()

            Page.ClientScript.RegisterClientScriptBlock(GetType(Page), "Script", "alert('Records Successfuly Saved!');", True)
        Catch ex As System.Data.SqlClient.SqlException
            Dim msg As String = "Insert Error:"
            msg += ex.Message

            Throw New Exception(msg)
        Finally
            conn.Close()
        End Try
    End Sub

2 个答案:

答案 0 :(得分:1)

请检查断点并确保splitItems有多个值。

答案 1 :(得分:0)

您可能会发现splitItems数组中没有元素,或者splitItem(0)元素等于“Nothing”。

只是简单地查看你的代码,我想知道主要问题是否在InsertRecords顶部的ForEach中。我认为如果sc = {“1/1/2010”}那么“ If item.contains(”,“)”永远不会成立,所以日期永远不会被添加到splitItems数组中。 A.K.A当splitItems数组击中抛出异常的ToString行时,它将为空。