Quantcast
Channel: VBForums - Visual Basic .NET
Viewing all articles
Browse latest Browse all 27193

VS 2005 [RESOLVED] Inserting Record to Database

$
0
0
Hi!
The first code below work but I tried changing it with the second posted code. All the records are saving perfectly except the column for Subjects. What am I missing here? I am displaying the subjects in a datagrid's 2nd column.
Code:

            'insert all selected subjects
            y = Me.BindingContext(dsSub.Tables("tblSubjects")).Count - 1
            Me.BindingContext(dsSub.Tables("tblSubjects")).Position = 0

            For x = 0 To y
                cmdStudList.CommandText = "INSERT INTO tblEnrol(IDNo,SubjectName, " & _
                        "FirstName,MI,LastName,YearLevel, Address,Gender) " & _
                    "VALUES ('" & txtStudID.Text & "','" & txtSubjectName.Text & _
                    "','" & txtFirstName.Text & "', '" & txtMI.Text & _
                    "','" & txtLastName.Text & "','" & cboYearLevel.Text & _
                    "','" & SQLEncode(txtCompleteAddress.Text) & "','" & txtGender.Text & "')"
                daStud.InsertCommand = cmdStudList
                daStud.InsertCommand.ExecuteNonQuery()
                Me.BindingContext(dsSub.Tables("tblSubjects")).Position += 1
                daStud.InsertCommand = cmdStudList
            Next

New Code
Code:

            'insert all selected subjects
            y = Me.SubjListBindingSource.Count - 1
            Me.SubjListBindingSource.Position = 0

            For x = 0 To y
                Using cmdRegister As New SqlCommand("INSERT INTO tblEnrolK12(IDNo,SubjectName, " & _
                        "FirstName,MI,LastName,YearLevel, Address,Gender) " & _
                    "VALUES (@StudID,@SubjName,@FName,@MI,@LName,@YearLevel,@Address,@Gender)", cnn)

                    With cmdRegister.Parameters
                        .AddWithValue("@StudID", txtStudID.Text.Trim)
                        .AddWithValue("@SubjName", txtSubjectName.Text.Trim)
                        .AddWithValue("@FName", txtFirstName.Text.Trim)
                        .AddWithValue("@MI", txtMI.Text.Trim)
                        .AddWithValue("@LName", txtLastName.Text.Trim)
                        .AddWithValue("@YearLevel", cboYearLevel.Text.Trim)
                        .AddWithValue("@Address", txtCompleteAddress.Text.Trim)
                        .AddWithValue("@Gender", txtGender.Text.Trim)
                    End With

                    cmdRegister.ExecuteNonQuery()
                    Me.SubjListBindingSource.Position += 1
                End Using
            Next


Viewing all articles
Browse latest Browse all 27193

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>