Imports System.Data.SqlClient
PublicClass Main
ProtectedWithEvents DataGridView1 As DataGridView
Dim instForm2 AsNew Exceptions
PrivateSub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startpayrollButton.Click
Dim ssql AsString = "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
TryWith 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()
EndWithIf 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 = FalseEndIfEndIf
oDr.Close()
oCmd.Connection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
oCmd.Connection.Close()
EndTryEndSubPrivateSub 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 AsString = "Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx"Dim ds AsNew 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 IsNothing) 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()
EndIfCatch ex As Exception
MessageBox.Show(ex.ToString)
connection.Close()
EndTry
Exceptions.Show()
EndSubPrivateSub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
EndSubPrivateSub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) HandlesMyBase.Load
EndSubEndClass
and when I run my app, I get a SQL error that tells me that I need to declare a value for '@payperiodstartdate' but I know that that value is declared because the first query runs fine. I am told that the line where the error is is line 59, which is this line:
adapter.Fill(ds)
Can someone please tell me what I may be missing?
Thank you
Doug
5 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Suthish NairRank #92 · 22.8K reputationPosted Jan 19, 2011, 11:00 AM EST
0
SqlDataAdapter manages the connection itself, meaning it opens the connection and it closes the connection itself.
Doug Ancil16 reputationPosted Jan 19, 2011, 11:23 AM EST
0
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)
0
Blocked AccountRank #403 · 6.2K reputationPosted Jan 20, 2011, 12:42 AM EST
0
Hi Doug,
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_Click event my 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.
Suthish NairRank #92 · 22.8K reputationPosted Jan 19, 2011, 11:00 AM EST
remove connection.Open() and try..
refer msdn
Doug Ancil16 reputationPosted Jan 19, 2011, 11:07 AM EST
I removed the connection.Open() and still the same error.
Suthish NairRank #92 · 22.8K reputationPosted Jan 19, 2011, 11:18 AM EST
You need to create parameters, for example..
using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString())) { using (SqlCommand cmd = new SqlCommand()) { cmd.Connection = con; cmd.CommandText = "FetchCustomerDetails"; cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Clear(); cmd.Parameters.Add("@type", SqlDbType.Int).Value = 2; cmd.Parameters.Add("@cust", SqlDbType.VarChar).Value = hdnCustValue.Value; hdnCustValue.Value = ""; using (SqlDataAdapter sda = new SqlDataAdapter(cmd)) { using (DataSet ds = new DataSet()) { sda.Fill(ds); if (ds != null && ds.Tables[0].Rows.Count > 0) { GridView1.DataSource = ds.Tables[0]; GridView1.DataBind(); } } } } }Doug Ancil16 reputationPosted Jan 19, 2011, 11:23 AM EST
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)
Blocked AccountRank #403 · 6.2K reputationPosted Jan 20, 2011, 12:42 AM EST
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.