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(s, 518399, max(payrolldate)) 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 connection As System.Data.SqlClient.SqlConnection
Dim connectionString AsString = "Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx"Dim ds AsNew DataSet
Dim _sql AsString = "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 = New SqlConnection(connectionString)
Dim _CMD As SqlCommand = New SqlCommand(_sql, connection)
Dim adapter As System.Data.SqlClient.SqlDataAdapter
adapter.SelectCommand = _CMD
_CMD.Parameters.Add(New SqlClient.SqlParameter("@payperiodstartdate", payperiodstartdate))
_CMD.Parameters.Add(New SqlClient.SqlParameter("@payperiodenddate", payperiodenddate))
Try
adapter.Fill(ds)
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'it exists and there are rows
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 this with debug, I get to this line
Code
adapter.SelectCommand = _CMD
and get the error of NullReferenceException was unhandled. Intellisense warns me that the variable adapter is used before it has been assigned a value. I've tried
Code
Dim adapter As System.Data.SqlClient.SqlDataAdapter = Nothing
can anyone offer some assitance on this?
Thank you
Doug
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
Raghvendr Singh81 reputationPosted Jan 20, 2011, 6:43 PM EST
0
Hi,
I am C# developer and your code is in VB.I think about the problem that u have declared adapter as:
Dim adapter As System.Data.SqlClient.SqlDataAdapter
But never created instance of this class as new keyword something like:
Dim adapter As System.Data.SqlClient.SqlDataAdapter=new System.Data.SqlClient.SqlDataAdapter.
Raghvendr Singh81 reputationPosted Jan 20, 2011, 6:43 PM EST
I am C# developer and your code is in VB.I think about the problem that u have declared adapter as:
Dim adapter As System.Data.SqlClient.SqlDataAdapter
But never created instance of this class as new keyword something like:
Dim adapter As System.Data.SqlClient.SqlDataAdapter=new System.Data.SqlClient.SqlDataAdapter.
Doug Ancil16 reputationPosted Jan 21, 2011, 5:30 AM EST
Raghvendr,
I believe that what you said is correct. I modified my code and added your line to it and it looks fine now.
Thank you,
Doug