Datagrid keeps displaying previously entered data

Obeng

Member
Joined
Apr 12, 2012
Messages
5
Programming Experience
1-3
I'm having this weird problem with my windows application, I have fields for entering data into an underlying access database, my problem is after clicking the submit button upon entering the first data it shows neatly in the datagrid but subsequent data entered repeats what was initially entered into the datagrid. Please below is a sample of my codes:

Private Sub BtnSubmit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnSubmit.Click
'Sets Null Fields to 0

If TextBox5.Text = "" Then
TextBox5.Text = 0
End If

If TextBox28.Text = "" Then
TextBox28.Text = 0
End If

If TextBox6.Text = "" Then
TextBox6.Text = 0
End If
If TextBox27.Text = "" Then
TextBox27.Text = 0
End If

If TextBox7.Text = "" Then
TextBox7.Text = 0
End If

If TextBox26.Text = "" Then
TextBox26.Text = 0
End If

If TextBox8.Text = "" Then
TextBox8.Text = 0
End If

If TextBox25.Text = "" Then
TextBox25.Text = 0
End If

If TextBox9.Text = "" Then
TextBox9.Text = 0
End If

If TextBox24.Text = "" Then
TextBox24.Text = 0
End If

If TextBox10.Text = "" Then
TextBox10.Text = 0
End If

If TextBox23.Text = "" Then
TextBox23.Text = 0
End If

If TextBox11.Text = "" Then
TextBox11.Text = 0
End If

If TextBox22.Text = "" Then
TextBox22.Text = 0
End If

If TextBox12.Text = "" Then
TextBox12.Text = 0
End If

If TextBox21.Text = "" Then
TextBox21.Text = 0
End If

If TextBox13.Text = "" Then
TextBox13.Text = 0
End If

If TextBox20.Text = "" Then
TextBox20.Text = 0
End If

If TextBox14.Text = "" Then
TextBox14.Text = 0
End If

If TextBox19.Text = "" Then
TextBox19.Text = 0
End If

If TextBox15.Text = "" Then
TextBox15.Text = 0
End If

If TextBox18.Text = "" Then
TextBox18.Text = 0
End If

objConnection.Open()
objCommand.Connection = objConnection

'Inserts parameter placeholders
objCommand.CommandText = "INSERT INTO CollectionForm (Region, District, Circuit, AcademicYear, Term, NameOfSchool, SchoolCode, Stream, Shift," & _
" DateOf, OpeningDays, InstructionalDays, EnrolKg1Boys, EnrolKg1Girls, TotalKg1Enrol," & _
" EnrolKg2Boys, EnrolKg2Girls, TotalKg2Enrol, EnrolP1Boys, EnrolP1Girls, TotalP1Enrol," & _
" EnrolP2Boys, EnrolP2Girls, TotalP2Enrol, EnrolP3Boys, EnrolP3Girls, TotalP3Enrol, EnrolP4Boys," & _
" EnrolP4Girls, TotalP4Enrol, EnrolP5Boys, EnrolP5Girls, TotalP5Enrol, EnrolP6Boys, EnrolP6Girls," & _
" TotalP6Enrol, EnrolJHS1Boys, EnrolJHS1Girls, TotalJHS1, EnrolJHS2Boys, EnrolJHS2Girls, TotalJHS2," & _
" EnrolJHS3Boys, EnrolJHS3Girls, TotalJHS3 ) " & _
"VALUES ( @Region, @District, @Circuit, @AcademicYear, @term, @nameOf, @SchoolCode, @Stream, @shift," & _
" @Date,@OpeningDays,@InstructionalDays, @Kg1B, @Kg1G, @TotalKg1, @kg2b, @kg2g, @totalkg2," & _
" @p1b, @p1g, @totalp1, @p2b, @p2g, @totalp2, @p3b, @p3g, @totalp3, @p4b, @p4g, @totalp4, @p5b," & _
" @p5g, @totalp5, @p6b, @p6g, @totalp6, @jhs1b, @jhs1g, @totaljhs1, @jhs2b, @jhs2g, @totaljhs2," & _
" @jhs3b, @jhs3g, @totaljhs3 )"
' Inserts Data Parameters
With objCommand.Parameters
.AddWithValue("@Region", ComboBox2.Text) : .AddWithValue("@District", ComboBox3.Text) : .AddWithValue("@Circuit", CircuitTextbox.Text)
.AddWithValue("@AcademicYear", ComboBox1.Text) : .AddWithValue("@Term", TextBox48.Text) : .AddWithValue("@nameOf", ComboBox4.Text)
.AddWithValue("@SchoolCode", ComboBox5.Text) : .AddWithValue("@Stream", TextBox50.Text) : .AddWithValue("@Shift", TextBox49.Text)
.AddWithValue("@Date", DateTimePicker1.Value.Date) : .AddWithValue("@OpeningDays", TextBox51.Text) : .AddWithValue("@Instructional Days", TextBox52.Text)
.AddWithValue("@Kg1B", TextBox5.Text) : .AddWithValue("@Kg1G", TextBox28.Text) : .AddWithValue("@totalKg1", TextBox40.Text)
.AddWithValue("@Kg2b", TextBox6.Text) : .AddWithValue("@Kg2g", TextBox27.Text) : .AddWithValue("@TotalKg2", TextBox39.Text)
.AddWithValue("@P1b", TextBox7.Text) : .AddWithValue("@P1g", TextBox26.Text) : .AddWithValue("@TotalP1", TextBox38.Text)
.AddWithValue("@P2b", TextBox8.Text) : .AddWithValue("@P2g", TextBox25.Text) : .AddWithValue("@TotalP2", TextBox37.Text)
.AddWithValue("@P3b", TextBox9.Text) : .AddWithValue("@P3g", TextBox24.Text) : .AddWithValue("@TotalP3", TextBox36.Text)
.AddWithValue("@P4b", TextBox10.Text) : .AddWithValue("@P4g", TextBox23.Text) : .AddWithValue("@TotalP4", TextBox35.Text)
.AddWithValue("@P5b", TextBox11.Text) : .AddWithValue("@P5g", TextBox22.Text) : .AddWithValue("@TotalP5", TextBox34.Text)
.AddWithValue("@P6b", TextBox12.Text) : .AddWithValue("@P6g", TextBox21.Text) : .AddWithValue("@TotalP6", TextBox33.Text)
.AddWithValue("@JHS1b", TextBox13.Text) : .AddWithValue("@JHS1g", TextBox20.Text) : .AddWithValue("@TotalJHS1", TextBox32.Text)
.AddWithValue("@jhs2b", TextBox14.Text) : .AddWithValue("@jhs2g", TextBox19.Text) : .AddWithValue("@totaljhs2", TextBox31.Text)
.AddWithValue("@jhs3b", TextBox15.Text) : .AddWithValue("@jhs3g", TextBox18.Text) : .AddWithValue("@totaljhs3", TextBox30.Text)
End With
objCommand.ExecuteNonQuery()
FillDatagrid()
objConnection.Close()


'Resets the contents to empty
TextBox5.Text = ""
TextBox28.Text = ""
TextBox6.Text = ""
TextBox27.Text = ""
TextBox7.Text = ""
TextBox26.Text = ""
TextBox8.Text = ""
TextBox25.Text = ""
TextBox9.Text = ""
TextBox24.Text = ""
TextBox10.Text = ""
TextBox23.Text = ""
TextBox11.Text = ""
TextBox22.Text = ""
TextBox12.Text = ""
TextBox21.Text = ""
TextBox13.Text = ""
TextBox20.Text = ""
TextBox14.Text = ""
TextBox19.Text = ""
TextBox15.Text = ""
TextBox18.Text = ""
End Sub

The procedure for the FillDatagrid is :
Private Sub FillDatagrid()
Dim ds As New DataSet
Dim dt As New DataTable
ds.Tables.Add(dt)
Dim da As New OleDbDataAdapter
da = New OleDbDataAdapter("SELECT * From CollectionForm ORDER BY NameOfSchool", objConnection)
da.Fill(dt)
objConnection.Close()
Me.DataGridView1.DataSource = dt
End Sub
 
Wow that's a lot of textboxes! Is that really the most efficient way to enter data?

I think the FillDataGrid sub is the problem. If this is an existing database created outside the program set up the bindings using the Data Source wizard to add the database to the project and the gridview should update automatically. You can also add the new information a whole lot more efficiently using CommandBuilder. Look it up in Help.



 
Back
Top