Question insert all rows of datagridview into database

sirajahmed0303

New member
Joined
Apr 26, 2014
Messages
1
Programming Experience
Beginner
I am trying to insert all the rows of a datagridview into a database table. Below is my code and error message snapshot. Could plz tell me where m going wrong.


VB.NET:
Private Sub addnewclasses[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]

    Dim [/COLOR][COLOR=#00008B]open[/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] String [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] db[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]OpenDB
    [/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#00008B]open[/COLOR][COLOR=#000000]<>[/COLOR][COLOR=#800000]"OK"[/COLOR][COLOR=#00008B]Then[/COLOR][COLOR=#000000]
        MessageBox[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Show[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"Error openining the database check the connection path to your database:"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"POS"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] MessageBoxButtons[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]OK[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] MessageBoxIcon[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Error[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Exit[/COLOR][COLOR=#000000] Sub
    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#000000]

    [/COLOR][COLOR=#00008B]For[/COLOR][COLOR=#000000] i [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] Integer [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]0[/COLOR][COLOR=#00008B]To[/COLOR][COLOR=#000000] DGV_Class[/COLOR][COLOR=#000000].[/COLOR][COLOR=#00008B]Rows[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Count [/COLOR][COLOR=#000000]-[/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000]

        Dim AirlineCode [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] String [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] DGV_Airline[/COLOR][COLOR=#000000].[/COLOR][COLOR=#00008B]Rows[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#000000]Cells[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#000000]Value
        Dim ClassCode [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] String [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] DGV_Class[/COLOR][COLOR=#000000].[/COLOR][COLOR=#00008B]Rows[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]i[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#000000]Cells[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]0[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#000000]Value
        Dim ClassDesc [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] String [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] DGV_Class[/COLOR][COLOR=#000000].[/COLOR][COLOR=#00008B]Rows[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]i[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#000000]Cells[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]1[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#000000]Value
        Dim ClassSort [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] Integer [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] DGV_Class[/COLOR][COLOR=#000000].[/COLOR][COLOR=#00008B]Rows[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]i[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#000000]Cells[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]2[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#000000]Value
        Dim ClassBook [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] Boolean [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#000000] DGV_Class[/COLOR][COLOR=#000000].[/COLOR][COLOR=#00008B]Rows[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]i[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#000000]Cells[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]3[/COLOR][COLOR=#000000]).[/COLOR][COLOR=#000000]Value

        [/COLOR][COLOR=#800000]'========================================================================
        airline.setairlinecode(AirlineCode)
        airline.setClassCode(ClassCode)
        airline.setClassDesc(ClassDesc)
        airline.setClassSort(ClassSort)
        airline.setClassBooking(ClassBook)
        '[/COLOR][COLOR=#000000]========================================================================[/COLOR][COLOR=#000000]
    Next
    Try
        [/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#00008B]Not[/COLOR][COLOR=#000000] airline[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]insert_class_master[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]db[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] True[/COLOR][COLOR=#000000])[/COLOR][COLOR=#00008B]Then[/COLOR][COLOR=#000000]
            MessageBox[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Show[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"Error occurred while adding new airline to the system make sure the information your provided is correct!"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"Error"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] MessageBoxButtons[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]OK[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] MessageBoxIcon[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Error[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
            [/COLOR][COLOR=#00008B]Exit[/COLOR][COLOR=#000000] Sub
        [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#000000]

        MessageBox[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Show[/COLOR][COLOR=#000000]([/COLOR][COLOR=#800000]"New airline has been added to the system successfully"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"system Updated successfully"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] MessageBoxButtons[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]OK[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] MessageBoxIcon[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Information[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]


    Catch ex [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] Exception
        MessageBox[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Show[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]ex[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Message[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"Error"[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] MessageBoxButtons[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]OK[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] MessageBoxIcon[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Error[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] Try[/COLOR]
End Sub
VB.NET:
[COLOR=#00008B]Public [/COLOR][COLOR=#00008B]Function[/COLOR][COLOR=#000000] insert_class_master[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]ByVal dbaccesscl [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] databaselogic[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] ByVal [/COLOR][COLOR=#00008B]save[/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] Boolean[/COLOR][COLOR=#000000])[/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] Boolean
    Try
        Dim SQL [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] String [/COLOR][COLOR=#000000]=[/COLOR][COLOR=#800000]"insert into Tbl_Class_Master ( "[/COLOR][COLOR=#000000]&[/COLOR][COLOR=#000000] _
                                         [/COLOR][COLOR=#800000]"Airline_Code, "[/COLOR][COLOR=#000000]&[/COLOR][COLOR=#000000] _
                                         [/COLOR][COLOR=#800000]"Class_Code, "[/COLOR][COLOR=#000000]&[/COLOR][COLOR=#000000] _
                                         [/COLOR][COLOR=#800000]"Class_Description, "[/COLOR][COLOR=#000000]&[/COLOR][COLOR=#000000] _
                                         [/COLOR][COLOR=#800000]"Class_Sorting, "[/COLOR][COLOR=#000000]&[/COLOR][COLOR=#000000] _
                                         [/COLOR][COLOR=#800000]"Class_Booking) values "[/COLOR][COLOR=#000000]&[/COLOR][COLOR=#000000] _
                                        [/COLOR][COLOR=#800000]"(@0,@1,@2,@3,@4)"[/COLOR][COLOR=#000000]
        dbaccesscl[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]ExecuteSQL[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]SQL[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] Me[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]AirlineCode[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] Me[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]ClassCode[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] Me[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]ClassDesc[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] Me[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Classsort[/COLOR][COLOR=#000000],[/COLOR][COLOR=#000000] Me[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Classbooking[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]

        [/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#00008B]save[/COLOR][COLOR=#00008B]Then[/COLOR][COLOR=#000000]
            dbaccesscl[/COLOR][COLOR=#000000].[/COLOR][COLOR=#00008B]Commit[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#00008B]If[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Return[/COLOR][COLOR=#000000] True
    Catch ex [/COLOR][COLOR=#00008B]As[/COLOR][COLOR=#000000] Exception
        MessageBox[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Show[/COLOR][COLOR=#000000]([/COLOR][COLOR=#000000]ex[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]Message[/COLOR][COLOR=#000000].[/COLOR][COLOR=#000000]ToString[/COLOR][COLOR=#000000],[/COLOR][COLOR=#800000]"error from inserting"[/COLOR][COLOR=#000000])[/COLOR][COLOR=#000000]
        dbaccesscl[/COLOR][COLOR=#000000].[/COLOR][COLOR=#00008B]Rollback[/COLOR][COLOR=#000000]()[/COLOR][COLOR=#000000]
        [/COLOR][COLOR=#00008B]Return[/COLOR][COLOR=#000000] False
    [/COLOR][COLOR=#00008B]End[/COLOR][COLOR=#000000] Try
[/COLOR][COLOR=#00008B]End [/COLOR][COLOR=#00008B]Function
[/COLOR]

Following is the error generated.
ERROR.jpg
 
Back
Top