gridview データをデータベースに渡そうとしています。私が抱えている問題は、グリッドビューのすべてのデータがデータベースに入力されていないことです。メッセージボックスを実行すると、名前列のみが表示されます。これが私のコードです
Protected Sub Button2_Click1(sender As Object, e As EventArgs) Handles Button2.Click
Dim sc As StringCollection = New StringCollection
Dim field1 As String = String.Empty
Dim i As Integer = 0
Do While (i < GridView1.Rows.Count)
field1 = GridView1.Rows(i).Cells(0).Text
i = (i + 1)
Loop
' get the field to be Inserted
sc.Add(field1)
' add the field to be Inserted in the StringCollection
InsertRecords(sc)
' call method for insert and pass the StringCollection values
End Sub
Dim conn As MySqlConnection = New MySqlConnection("Server=****************;Database=******;Uid=*******;Pwd=****;allow user variables=true")
Dim sb As StringBuilder = New StringBuilder(String.Empty)
For Each item As String In sc
Const sqlStatement As String = "INSERT INTO student(name, age, adress) VALUES ("
sb.AppendFormat("{0}'{1}' ", sqlStatement, item)
Next
sb.Append(")")
MsgBox(sb.ToString)
Try
conn.Open()
Dim cmd As MySqlCommand = New MySqlCommand(sb.ToString, conn)
cmd.CommandType = CommandType.Text
cmd.ExecuteNonQuery()
Catch ex As System.Data.SqlClient.SqlException
Dim msg As String = "Insert Error:"
msg = (msg + ex.Message)
Throw New Exception(msg)
Finally
conn.Close()
End Try
End Sub