I created two table parent and child.
Cus: Parent
+----+-------------------------+------+
| ID | Name | Age |
+----+-------------------------+------+
| 1 | Japhet | 22 |
| 2 | Abegail | 31 |
| 3 | Norlee | 35 |
| 4 | Pacita | 60 |
| 5 | Reynaldo | 65 |
| 6 | Barro, Reynaldo Batucan | 65 |
| 7 | Batucan, Japhet C. | NULL |
| 8 | Barro, Reynaldo B. | NULL |
+----+-------------------------+------+
Cus2: Child
+-----+----+------+---------+
| QID | ID | Name | Country |
+-----+----+------+---------+
| | | | |
+-----+----+------+---------+
I've used the codes below to populate the datagridview with the custom rows, but when I attempt to save it, it won't save. I get this error message:
Input string was not in correct format. Couldn't store in ID Column. Expected type is Int32.
Also, the Cus.ID will not populate in Cus2.ID
Private Sub Populate()
Dim dt As New DataTable()
dt.Columns.AddRange(New DataColumn(3) {New DataColumn("QID", GetType(Integer)), New DataColumn("ID", GetType(Integer)), New DataColumn("Name", GetType(String)), New DataColumn("Country", GetType(String))})
Try
dt.Rows.Add(1, 0, "John Hammond", "United States")
dt.Rows.Add(2, 0, "Mudassar Khan", "India")
dt.Rows.Add(3, 0, "Suzanne Mathews", "France")
dt.Rows.Add(4, 0, "Robert Schidner", "Russia")
Me.Cus2DataGridView.DataSource = dt
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Populate()
End Sub
Private Sub Cus2DataGridView_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles Cus2DataGridView.CellContentClick
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
For Each row As DataGridViewRow In Cus2DataGridView.Rows
Dim constring As String = "Data Source=DESKTOP-0M1930H\PNJK;Initial Catalog=Hello;Integrated Security=True"
Using con As New SqlConnection(constring)
Using cmd As New SqlCommand("INSERT INTO Cus2 VALUES(@QID, @ID, @Name, @Country)", con)
Try
cmd.Parameters.AddWithValue("@QID", row.Cells("QID").Value)
cmd.Parameters.AddWithValue("@ID", row.Cells("ID").Value)
cmd.Parameters.AddWithValue("@Name", row.Cells("Name").Value)
cmd.Parameters.AddWithValue("@Country", row.Cells("Country").Value)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Catch ex As Exception
End Try
End Using
End Using
Next
End Sub
I am new to this, so please help. Thanks :D