Click here to Skip to main content
15,921,694 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
Public Sub SqlSelectTimesAwaitingApproval(dgv as datagridview, listOfEmployees As List(Of integer))

    Dim con As New SqlConnection(My.Settings.AlphaCureCRMConnectionString)
    Dim cmd As New SqlCommand()
    Dim ds As New DataSet
    Try
        cmd.Connection = Con
        For Each intId As Integer In listOfEmployees
            Con.Open()
            cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = intId
            cmd.CommandText = "SELECT * FROM tblAttendanceTimes WHERE intApprovedOvertime = 0 AND fkEmployee = @EmployeeID"
            Dim da As New SqlDataAdapter() With
            {.SelectCommand = New SqlCommand(cmd.CommandText, con)}
            da.Fill(ds, "Table")
            con.Close()
            da.Dispose()
        Next
        Dgv.DataSource = ds.Tables("Table")
        ds.Dispose()
    Catch ex As Exception
        MsgBox(Ex.Message)
    Finally
        Cmd.Dispose()
    End Try
End Sub


Cannot figure out why i am getting this error, the parameter is being defined before it's called. I have had this working for a while but something must of changed and now i can't get this to work at all.

What I have tried:

I have tried mostly just changing around the code as it is but to no avail.
Posted
Updated 14-Apr-17 8:41am
v5
Comments
[no name] 14-Apr-17 14:23pm    
"parameter is being defined before it's called", where? It's not in the code posted here. The title of your posting and code here do not match.
BeginnerCoderPete 14-Apr-17 14:27pm    
The title is the error message i am receiving from this piece of code. I add the parameter then create the command same as all my other SQL queries but for some reason it's giving me this error.
[no name] 14-Apr-17 14:39pm    
"The title is the error message", if that were true, why did you change it? Is the TITLE of your posting where you are supposed to be telling us about your problem? hint: no it is not.
BeginnerCoderPete 14-Apr-17 14:50pm    
Thanks for your help.
PIEBALDconsult 14-Apr-17 14:38pm    
Well, you're doing it wrong anyway -- you need to move a bunch of that out of the loop.
In the loop, all you need to do is set the Value of the parameter and execute the statement.

Instantiate the command
Set the text
Add the parameters

for ...
{
Set the parameter values
Execute
}

You add the command parameter to the original command, not the one you fill your DataSet from via the adapter:
cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = intId
                cmd.CommandText = "SELECT * FROM tblAttendanceTimes WHERE intApprovedOvertime = 0 AND fkEmployee = @EmployeeID"
                Dim da As New SqlDataAdapter(cmd) With 
                {.SelectCommand = New SqlCommand(cmd.CommandText, con)}
                da.Fill(ds, "Table")

Try this:
cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = intId
cmd.CommandText = "SELECT * FROM tblAttendanceTimes WHERE intApprovedOvertime = 0 AND fkEmployee = @EmployeeID"
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds, "Table")
 
Share this answer
 
Comments
BeginnerCoderPete 14-Apr-17 14:47pm    
{ Dim con As New SqlConnection(My.Settings.AlphaCureCRMConnectionString)
Dim cmd As New SqlCommand()
Dim ds As New DataSet
Try
cmd.Connection = Con

For Each intId As Integer In listOfEmployees
Con.Open()
cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = intId
cmd.CommandText = "SELECT * FROM tblAttendanceTimes WHERE intApprovedOvertime = 0 AND fkEmployee = @EmployeeID"
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds, "Table")
da.Dispose()
con.Close()
Next
cmd.Dispose()
Dgv.DataSource = ds.Tables("Table")
ds.Dispose()
Catch ex As Exception
MsgBox(Ex.Message)
Finally
Cmd.Dispose()
End Try}

That is how i have it now but now i'm getting the error 'Variable name has been declared' but hopefully i'll get there from here. Thank you. So far this seems to of helped.
PIEBALDconsult 14-Apr-17 21:02pm    
Because you are still adding the parameter in the loop -- which is wrong; it leads to duplicate parameters.
BeginnerCoderPete 14-Apr-17 22:03pm    
Thank you, i have managed to sort it out now, using the cmd.parameters.clear before it loops again.
PIEBALDconsult 14-Apr-17 22:05pm    
No, please don't do that.
Create and add the parameter before the loop, then in the loop just set the Value.
BeginnerCoderPete 14-Apr-17 22:16pm    
I wasn't sure how to create the parameters outside of the loop to be honest.
Out of curiosity what is wrong with clearing the parameters?
cmd.Parameters.Add("@EmployeeID", SqlDbType.Int).Value = intId
cmd.CommandText = "SELECT * FROM tblAttendanceTimes WHERE intApprovedOvertime = 0 AND fkEmployee = @EmployeeID"
Dim da As New SqlDataAdapter() With
{.SelectCommand = New SqlCommand(cmd.CommandText, con)}

Maybe you meant...

{.SelectCommand = cmd}
 
Share this answer
 
v2
Comments
BeginnerCoderPete 14-Apr-17 14:48pm    
Yes this seems to of helped me so far, i should be able to work out the rest from here i hope.

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900