473,811 Members | 3,026 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

NullReferenceEx ception was unhandled

347 Contributor
I have the following form:

Expand|Select|Wrap|Line Numbers
  1. Imports System.Data.SqlClient
  2. Public Class Main
  3.     Protected WithEvents DataGridView1 As DataGridView
  4.     Dim instForm2 As New Exceptions
  5.     Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles startpayrollButton.Click
  6.         Dim ssql As String = "select MAX(payrolldate) AS [payrolldate], " & _
  7.                  "dateadd(s,  518399, max(payrolldate)) AS [Sunday]" & _
  8.                   "from dbo.payroll" & _
  9.                   " where payrollran = 'no'"
  10.         Dim oCmd As System.Data.SqlClient.SqlCommand
  11.         Dim oDr As System.Data.SqlClient.SqlDataReader
  12.         oCmd = New System.Data.SqlClient.SqlCommand
  13.  
  14.         Try
  15.             With oCmd
  16.                 .Connection = New System.Data.SqlClient.SqlConnection("Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx")
  17.                 .Connection.Open()
  18.                 .CommandType = CommandType.Text
  19.                 .CommandText = ssql
  20.                 oDr = .ExecuteReader()
  21.             End With
  22.             If oDr.Read Then
  23.                 payperiodstartdate = oDr.GetDateTime(1)
  24.                 payperiodenddate = payperiodstartdate.AddDays(7)
  25.                 Dim ButtonDialogResult As DialogResult
  26.                 ButtonDialogResult = MessageBox.Show("      The Next Payroll Start Date is: " & payperiodstartdate.ToString() & System.Environment.NewLine & "            Through End Date: " & payperiodenddate.ToString())
  27.                 If ButtonDialogResult = Windows.Forms.DialogResult.OK Then
  28.                     exceptionsButton.Enabled = True
  29.                     startpayrollButton.Enabled = False
  30.                 End If
  31.             End If
  32.             oDr.Close()
  33.             oCmd.Connection.Close()
  34.         Catch ex As Exception
  35.             MessageBox.Show(ex.Message)
  36.             oCmd.Connection.Close()
  37.         End Try
  38.  
  39.     End Sub
  40.     Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles exceptionsButton.Click
  41.         Dim connection As System.Data.SqlClient.SqlConnection
  42.         Dim connectionString As String = "Initial Catalog=mdr;Data Source=xxxxx;uid=xxxxx;password=xxxxx"
  43.         Dim ds As New DataSet
  44.         Dim _sql As String = "SELECT [Exceptions].Employeenumber,[Exceptions].exceptiondate, [Exceptions].starttime, [exceptions].endtime, [Exceptions].code, datediff(minute, starttime, endtime)  as duration INTO scratchpad3" & _
  45.  " FROM Employees INNER JOIN Exceptions ON [Exceptions].EmployeeNumber = [Exceptions].Employeenumber" & _
  46.  " where [Exceptions].exceptiondate between @payperiodstartdate and @payperiodenddate" & _
  47.  " GROUP BY [Exceptions].Employeenumber, [Exceptions].Exceptiondate, [Exceptions].starttime, [exceptions].endtime," & _
  48.  " [Exceptions].code, [Exceptions].exceptiondate"
  49.         connection = New SqlConnection(connectionString)
  50.         Dim _CMD As SqlCommand = New SqlCommand(_sql, connection)
  51.         Dim adapter As System.Data.SqlClient.SqlDataAdapter
  52.         adapter.SelectCommand = _CMD
  53.         _CMD.Parameters.Add(New SqlClient.SqlParameter("@payperiodstartdate", payperiodstartdate))
  54.         _CMD.Parameters.Add(New SqlClient.SqlParameter("@payperiodenddate", payperiodenddate))
  55.         Try
  56.             adapter.Fill(ds)
  57.             If (ds Is Nothing) Then
  58.                 'it's empty
  59.                 MsgBox("There was no data for this time period, press Ok to continue", "No Data")
  60.                 connection.Close()
  61.                 Exceptions.Hide()
  62.             Else
  63.                 'it exists and there are rows 
  64.                 adapter.Fill(ds)
  65.                 connection.Close()
  66.             End If
  67.  
  68.         Catch ex As Exception
  69.             MessageBox.Show(ex.ToString)
  70.             connection.Close()
  71.         End Try
  72.         Exceptions.Show()
  73.     End Sub
  74.     Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellContentClick
  75.  
  76.     End Sub
  77.  
  78.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  79.  
  80.     End Sub
  81. End Class
  82.  
and when I run this with debug, I get to this line:

Expand|Select|Wrap|Line Numbers
  1.    adapter.SelectCommand = _CMD
  2.  
and I get the error NullReferenceEx ception was unhandled.

Intellisense warns me that the variable adapter is used before it has been assigned a value. I've tried

Expand|Select|Wrap|Line Numbers
  1.  Dim adapter As System.Data.SqlClient.SqlDataAdapter = Nothing
  2.  
and that didn't seem to work either.

Can anyone offer any help with this?

Thank you
Jan 20 '11 #1
0 991

Sign in to post your reply or Sign up for a free account.

Similar topics

3
9148
by: Terrence | last post by:
I am doing some of the C# walkthroughs to transition from VB to C#. When I try to execute static void Main() { Aplication.Run(new Form1()) } I raise a 'System.NullReferenceException" in system.windows.forms.dll
1
536
by: Rafael | last post by:
Hi, I hope I can find some help for this problem IDE: Visual Studio.NET 2003 Developer Editio Language: C# Problem: "An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll. Additional information: Object reference not set to an instance of an object. Visual Studio's IDE is not allowing me to develop projects in C#... I've trying to create a simple Windows Forms project using Visual Studio.NET...
7
8107
by: mike p. | last post by:
I have a docbook xml file, and am using standard docbook 1.61.3 xsl stylesheets to do xhtml transform. Transform works fine when using MSXML. When I try to do the following using asp.net 1.1: private void Page_Load(object sender, System.EventArgs e) { // load content XslTransform trans = new XslTransform();
0
9784
by: muralidharan | last post by:
WebForm1.aspx Code: <%@ Register TagPrefix="ComponentArt" Namespace="ComponentArt.Web.UI" Assembly="ComponentArt.Web.UI" %> <ComponentArt:TreeView id="TreeView1" Height="520" AutoPostBackOnNodeMove="false" DragAndDropEnabled="true" NodeEditingEnabled="False" KeyboardEnabled="true" CssClass="TreeView" NodeCssClass="TreeNode" SelectedNodeCssClass="SelectedTreeNode" HoverNodeCssClass="HoverTreeNode" NodeEditCssClass="NodeEdit"
7
4005
by: David Elliott | last post by:
I have created an application that will dynamically load other DLLs (plugins). The new plugin is a winform with an embedded IE Browser. I am wanting to have the form to run in its own thread. This would allow for other plugins and the main application to be free to do other work. I have written a little TestDriver for the plugin and am having some difficulty. If I don't use threads everything works just fine. If I do use threads, upon...
2
7852
by: Raed Sawalha | last post by:
i have a windows form(Main) with listview, when click an item in listview i open other window form (Sub) which generate the selected item from parent window in as treeview items when click any item in treeview i display the content item in axWebBrowser, i close the sub form normally when i close the main the following error is generated An unhandled exception of type 'System.NullReferenceException' occurred in system.windows.forms.dll ...
7
2718
by: Chuck Hartman | last post by:
I have a Windows service that requests web pages from a site using an HttpWebRequest object. When I try to request a page from an ASP.NET 2 site, I get a WebException with message "The remote server returned an error: (500) Internal Server Error." I found a post that suggested to catch the WebException to retrieve the actual HttpWebResponse object for more information. The response returned is shown below. At first I thought this was a...
2
2784
by: sxiao | last post by:
Hi, there I got a NullReferenceException when there are more than one users trying to open the same page at the same time. The senerio is: Two users logged into the web application using the Windows domain user account and tried to open the same page which has the databasde query code. When the two user tried to open the same page at the same time, there is a runtime error messsage showing as follows:
5
5295
by: Saber | last post by:
in <system.web> of web.config I've: <profile> <properties> <add name="EnterTime" type="System.DateTime" allowAnonymous="true"/> </properties> </profile> And in Session_Start of global.asax:
1
2879
by: mdhaman | last post by:
hi, I have a windows service written in VB.Net and framework 2.0. It is a multithread service and it is using threadpool to manage threads. Recently I have started getting NullReferenceException and after that windows service crashes. System.Transactions Critical: 0 : <TraceRecord xmlns="http://schemas.microsoft.com/2004/10/E2ETraceEvent/TraceRecord"
0
9727
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10647
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10133
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7669
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6889
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5692
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4339
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3865
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.