私はデータの行に追加するこのボタンを持っているが、それは私にエラーを与える誰も私にsplitItems(0)理由は教えてくれますか?データベースに格納する値オブジェクト参照がオブジェクトのインスタンスに設定されていません。 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
orhこれはsc = {"1/1/2010"}の場合、このアイテムをデータベースにどのように取得するのですか? – actKing
もし私の前提が正しければ、それを修正する一つの方法は、item.Split文の周りからif文を削除することです。コンマを持たない文字列に対して 'splitItems = item.Split("、 ")'を実行すると、splitItemsは値として{"1/1/2010"}を持つ単一の要素配列になります(I * think *あなたのコードは期待しています)。私の元々の仮定が正しいことを確認できますか? – EverPresent
はい、あなたは正しいです、私はまったくコンマを必要としません、日付だけが挿入されるので、あなたはどのように表示できますか? – actKing