Click here to Skip to main content
14,449,862 members
Rate this:
Please Sign up or sign in to vote.
See more:
When i tried to insert data from datagridview to my sql database table named "GenPaySlip", it throws an error:- Must declare the scalar variable @GID.
Can anyone suggest me how to solve this error....

What I have tried:

Below is my code for inserting data...
Dim oldb_cmd As OleDbCommand
        Dim gp_id, fid, wkhr As Integer
        Dim phpy, tdpy As Decimal
        Dim dtpt As String
        oldb_cmd = New OleDbCommand("INSERT INTO GenPaySlip(GPS_Id, Fac_Id, WrkHrs, DtPsnt,PrHrPay, TotDayPay) VALUES (@GID, @FID, @WHR, @DPT, @PHP, @TDP)", con)

            For i As Integer = 0 To DgvGPaySlip.Rows.Count - 1
                gp_id = gps_id
                fid = DgvGPaySlip.Rows(i).Cells(0).Value
                wkhr = DgvGPaySlip.Rows(i).Cells(2).Value
                dtpt = DgvGPaySlip.Rows(i).Cells(3).Value
                phpy = DgvGPaySlip.Rows(i).Cells(4).Value
                tdpy = DgvGPaySlip.Rows(i).Cells(5).Value

                oldb_cmd.Parameters.AddWithValue("@GID", gp_id)
                oldb_cmd.Parameters.AddWithValue("@FID", fid)
                oldb_cmd.Parameters.AddWithValue("@WHR", wkhr)
                oldb_cmd.Parameters.AddWithValue("@DPT", dtpt)
                oldb_cmd.Parameters.AddWithValue("@PHP", phpy)
                oldb_cmd.Parameters.AddWithValue("@TDP", tdpy)
        Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Information, "Information")
        End Try



        MessageBox.Show("All Data Inserted")

Sql Table Description:-

GPS_Id	int	Unchecked
Fac_Id	int	Checked
WrkHrs	int	Checked
DtPsnt	varchar(15)	Checked
PrHrPay	decimal(18, 2)	Checked
TotDayPay	decimal(18, 2)	Checked
Updated 7-Feb-20 21:20pm
Rate this:
Please Sign up or sign in to vote.

Solution 1

Most likely, it's because gp_id is null, and that's probably because gps_id is null
WHy is it null? We can't tell as we don't have your data, or your code running in front of us.

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. If you don't know how to use it then a quick Google for "Visual Studio debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
Madhu Chatterjee 8-Feb-20 2:34am
GPS_Id is null because it is a Primary Key...
OriginalGriff 8-Feb-20 4:47am
Primary keys can't be null.
If you mean it's assigned by the DB, you can't set it, so don't even try to pass it across.
Rate this:
Please Sign up or sign in to vote.

Solution 2

Another observation. To work efficiently with parameters, you can define them outside the loop and simply replace the values inside the loop.

Consider the following example
oldb_cmd.Parameters.Add(New OleDbParameter("@GID", SqlDbType.Int))
oldb_cmd.Parameters.Add(New OleDbParameter("@FID", SqlDbType.Int))
oldb_cmd.Parameters.Add(New OleDbParameter("@WHR", SqlDbType.Int))
oldb_cmd.Parameters.Add(New OleDbParameter("@DPR", SqlDbType.Varchar, 15))
oldb_cmd.Parameters.Add(New OleDbParameter("@PHP", SqlDbType.Decimal))
oldb_cmd.Parameters.Add(New OleDbParameter("@TDP", SqlDbType.Decimal))

For i As Integer = 0 To DgvGPaySlip.Rows.Count - 1
    command.Parameters["@GID"].Value = gps_id
    command.Parameters["@FID"].Value = DgvGPaySlip.Rows(i).Cells(0).Value
    command.Parameters["@WHR"].Value = DgvGPaySlip.Rows(i).Cells(2).Value
    command.Parameters["@DPR"].Value = DgvGPaySlip.Rows(i).Cells(3).Value
    command.Parameters["@PHP"].Value = DgvGPaySlip.Rows(i).Cells(4).Value
    command.Parameters["@TDP"].Value = DgvGPaySlip.Rows(i).Cells(5).Value


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100