Imports System.Data.SqlClient Public Class Main Protected WithEvents DataGridView1 As DataGridView Dim instForm2 As New Exceptions Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startpayrollButton.Click Dim ssql As String = "select MAX(payrolldate) AS [payrolldate], " & _ "dateadd(dd, ((datediff(dd, '17530107', MAX(payrolldate))/7)*7)+7, '17530107') AS [Sunday]" & _ "from dbo.payroll" & _ " where payrollran = 'no'" Dim oCmd As System.Data.SqlClient.SqlCommand Dim oDr As System.Data.SqlClient.SqlDataReader oCmd = New System.Data.SqlClient.SqlCommand
Try With oCmd .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx") .Connection.Open() .CommandType = CommandType.Text .CommandText = ssql oDr = .ExecuteReader() End With If oDr.Read Then payperiodstartdate = oDr.GetDateTime(1) payperiodenddate = payperiodstartdate.AddDays(7) Dim ButtonDialogResult As DialogResult ButtonDialogResult = MessageBox.Show(" The Next Payroll Start Date is: " & payperiodstartdate.ToString() & System.Environment.NewLine & " Through End Date: " & payperiodenddate.ToString()) If ButtonDialogResult = Windows.Forms.DialogResult.OK Then exceptionsButton.Enabled = True startpayrollButton.Enabled = False End If End If oDr.Close() oCmd.Connection.Close() Catch ex As Exception MessageBox.Show(ex.Message) oCmd.Connection.Close() End Try
End Sub Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exceptionsButton.Click Dim adapter As System.Data.SqlClient.SqlDataAdapter Dim connection As System.Data.SqlClient.SqlConnection Dim connectionString As String = "Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx" Dim ds As New DataSet connection = New SqlConnection(connectionString) Try connection.Open() adapter = New SqlDataAdapter("SELECT [Exceptions].Employeenumber,[Exceptions].exceptiondate, [Exceptions].starttime, [exceptions].endtime, [Exceptions].code, datediff(minute, starttime, endtime) as duration INTO scratchpad3" & _ " FROM Employees INNER JOIN Exceptions ON [Exceptions].EmployeeNumber = [Exceptions].Employeenumber" & _ " where [Exceptions].exceptiondate between @payperiodstartdate and @payperiodenddate" & _ " GROUP BY [Exceptions].Employeenumber, [Exceptions].Exceptiondate, [Exceptions].starttime, [exceptions].endtime," & _ " [Exceptions].code, [Exceptions].exceptiondate", connection) If (ds Is Nothing) Then 'it's empty MsgBox("There was no data for this time period, press Ok to continue", "No Data") connection.Close() Exceptions.Hide() Else adapter.Fill(ds) connection.Close() End If
Catch ex As Exception MessageBox.Show(ex.ToString) connection.Close() End Try Exceptions.Show() End Sub Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub End Class
|
Manish DwivediPosted Jan 20, 2011, 5:42 AM
You have created this module
Module Module1
Public payperiodstartdate As Date
Public payperiodenddate As Date
End Module
but you are using @payperiodstartdate and @payperiodenddate in
Button2_Clickeventmy suggestion is use same parameter in both query either payperiodstartdate or @payperiodstartdate
because you are using @payperiodstartdate in second query, it means you are using parameterized query, so you need to set the value before using.
I think you got it.
Doug AncilPosted Jan 19, 2011, 4:23 PM
Suthish,
I have a module with those parameters in it. As I said, I KNOW that the parameters are correct because my first query calls those parameters. My module is this:
Module Module1
Public payperiodstartdate As Date
Public payperiodenddate As Date
End Module
and this worked before I added this portion of code:
If (ds Is Nothing) Then
'it's empty
MsgBox("There was no data for this time period, press Ok to continue", "No Data")
connection.Close()
Exceptions.Hide()
Else
adapter.Fill(ds)
Suthish NairPosted Jan 19, 2011, 4:18 PM
@payperiodstartdate - its a parameter.
You need to create parameters, for example..
Doug AncilPosted Jan 19, 2011, 4:07 PM
I removed the connection.Open() and still the same error.
Suthish NairPosted Jan 19, 2011, 4:00 PM
SqlDataAdapter manages the connection itself, meaning it opens the connection and it closes the connection itself.
remove connection.Open() and try..
refer msdn