Hi,

I am getting the following error when I load the page shown below:

Object variable or With block variable not set. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object variable or With block variable not set.

Source Error: 


Line 215:	Response.Write ("<option selected value=""" & MonthCheck(MonthCount) & """>" & MonthCount & "</option>" & vbCrLf)
Line 216:Else
Line 217:	Response.Write ("<option value=""" & MonthCheck(MonthCount) & """>" & MonthCount & "</option>" & vbCrLf)
Line 218:End If
Line 219:

I'm stuck, I'm thinking it must be something to do with the Function, MonthCheck, but I'm not making much of it. Any help would be greatly appreciated, thank you.

<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="person_edit.aspx.vb" Explicit="True" Inherits="Contractors.person_edit" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html lang="en">
	<head>
		<title>Contractors and Consultants Induction Search Tool</title> 
		<meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
		<meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
		<meta name="vs_defaultClientScript" content="VBScript">
		<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
		<meta http-equiv="content-type" content="text/html;charset=utf-8">
		<meta http-equiv="Content-Style-Type" content="text/css">
		<link rel="stylesheet" href="style.css" type="text/css">
	</head>
	<script runat="server" language="vb">
	Function MonthCheck(num)

		If Len(num) < 2 Then
			MonthCheck = "0" & num
		Else
			MonthCheck = num
		End If

	End Function
	</script>
	<%
	
	Dim MyConnection As SqlConnection
	Dim MyCommand As SqlCommand
	Dim MyReader As SqlDataReader
	Dim MyBinder
	Dim sSQL As String
	Dim nameSQL As String

	Dim SelectedPerson

	MyConnection = New SqlConnection("server=DBASESVR;uid=CSC;pwd=CSC!;database=Contractors")
	MyConnection.Open()
	SelectedPerson = CStr(Request.Form("SelectedPerson"))

	%>
	<body>
		<table cellspacing="0" cellpadding="0" border="0">
			<tr>
				<td width="20" background="images/topleft_corner.gif" height="20"></td>
				<td background="images/top_border.gif" height="20"></td>
				<td width="20" background="images/topright_corner.gif" height="20"></td>
			</tr>
			<tr>
				<td width="20" background="images/left_border.gif"></td>
				<td valign="top" bgcolor="#f5f5f5">
					<span class="title">Contractors and Consultants Database Search</span><br />
					<br />
					<br />
					<table align="center" width="600" cellpadding="10">
						<tr>
						<%

						If SelectedPerson = "" Then

						%>
							<td class="info" colspan="5">
								To edit a person's details, please select them from the listbox.<br />
								<br />
								<%

								nameSQL = "SELECT ID, VendorNo, DateIssued, Surname, FirstName, ContactNumber, Address FROM Contacts"
								MyCommand = New SqlCommand(nameSQL, MyConnection)
								MyReader = MyCommand.ExecuteReader
								Contractors.DataSource = MyReader
								Contractors.DataBind()

								%>
								<form runat="server">
								<asp:Repeater id="Contractors" runat="server">

								<HeaderTemplate>
								<select name="SelectedPerson" size="2" style="height:200px;">
								</HeaderTemplate>

								<ItemTemplate>
								<option value="<%#Container.DataItem("ID")%>"><%#Container.DataItem("FirstName")%> <%#Container.DataItem("Surname")%></option>
								</ItemTemplate>

								<FooterTemplate>
								</select>
								<br /><br />
								<input type="submit" name="Edit" value="Edit" style="width:50px" />
								</FooterTemplate>

								</asp:Repeater>
								</form>
							</td>
							<%

							Else


Dim MyCommand2 As SqlCommand
Dim MyReader2 As SqlDataReader
Dim MyBinder2
Dim sSQL2 As String
Dim nameSQL2 As String

nameSQL2 = "SELECT ID, VendorNo, DateIssued, Surname, FirstName, ContactNumber, Address FROM Contacts WHERE ID = " & SelectedPerson
MyCommand2 = New SqlCommand(nameSQL2, MyConnection)
MyReader2 = MyCommand2.ExecuteReader

While MyReader2.Read

Dim DateIssued
Dim DateDay
Dim DateMonth
Dim DateYear

DateIssued = Split(MyReader2.GetValue(2), "/")
DateDay = CInt(DateIssued(0))
DateMonth = CInt(DateIssued(1))
DateYear = CInt(DateIssued(2))

%>
<td class="info" colspan="5">
Edit the following fields then submit the changes.
<br /><br />
<form method="post" action="person_process.asp">
<table cellpadding="10">
<tr>
<td class="heading2">
First Name:
</td>
<td>
<input type="text" name="FirstName" value="<%=MyReader2.GetValue(4)%>" maxlength="50" style="width:200px" />
</td>
</tr>
<tr>
<td class="heading2">
Surname:
</td>
<td>
<input type="text" name="Surname" value="<%=MyReader2.GetValue(3)%>" maxlength="50" style="width:200px" />
</td>
</tr>
<tr>
<td class="heading2">
Address:
</td>
<td>
<input type="text" name="Address" value="<%=MyReader2.GetValue(6)%>" maxlength="50" style="width:200px" />
</td>
</tr>
<tr>
<td class="heading2">
Contact Number:
</td>
<td>
<input type="text" name="ContactNumber" value="<%=MyReader2.GetValue(5)%>" maxlength="50" style="width:200px" />
</td>
</tr>
<tr>
<td class="heading2">
Vendor Number:
</td>
<td>
<input type="text" name="VendorNo" value="<%=MyReader2.GetValue(1)%>" maxlength="50" style="width:200px" />
</td>
</tr>
<tr>
<td class="heading2">
Date Issued:
</td>
<td>
<select name="DayIssued" size="1">
<%

dim DayCount
DayCount = 1

Do While DayCount < 32

If CInt(DateIssued(0)) = CInt(DayCount) Then
	Response.Write ("<option selected value=""" & DayCount & """>" & DayCount & "</option>" & vbCrLf)
Else
	Response.Write ("<option value=""" & DayCount & """>" & DayCount & "</option>" & vbCrLf)
End If

DayCount = DayCount + 1

Loop ' END: Do While DayCount < 32

%>
</select>

<strong>:</strong>

<select name="MonthIssued" size="1">
<%

Dim MonthCount
dim MonthCheck
MonthCount = 1

Do While MonthCount < 13

If CInt(DateIssued(1)) = CInt(MonthCount) Then
	Response.Write ("<option selected value=""" & MonthCheck(MonthCount) & """>" & MonthCount & "</option>" & vbCrLf)
Else
	Response.Write ("<option value=""" & MonthCheck(MonthCount) & """>" & MonthCount & "</option>" & vbCrLf)
End If

MonthCount = MonthCount + 1

Loop ' END: Do While MonthCount < 13

%>
</select>

<strong>:</strong>

<select name="YearIssued" size="1">
<%

dim YearCount
YearCount = Year(Now) -1

Do While YearCount < (Year(Now) + 2)

If CInt(DateIssued(2)) = CInt(YearCount) Then
	Response.Write ("<option selected value=""" & YearCount & """>" & YearCount & "</option>" & vbCrLf)
Else
	Response.Write ("<option value=""" & YearCount & """>" & YearCount & "</option>" & vbCrLf)
End If

YearCount = YearCount + 1

Loop ' END: Do While YearCount < (Year(Now) + 2)

%>
</select>
</td>
</tr>
<tr>
<td>
<input type="submit" name="submit" value="Save" style="width:80px" />
<input type="hidden" name="personid" value="<%=SelectedPerson%>" />
</td>
</tr>
</table>
</form>
</td>
<%

End While

							MyConnection.Close()

						End If ' END: If SelectedPerson = ""

					%>
					</tr>
					</table>
					<br />
					<div style="text-align: right;"><a href="index.aspx">Index</a></div>
				</td>
				<td width="20" background="images/right_border.gif"></td>
			</tr>
			<tr>
				<td width="20" background="images/bottomleft_corner.gif" height="20"></td>
				<td background="images/bottom_border.gif" height="20"></td>
				<td width="20" background="images/bottomright_corner.gif" height="20"></td>
			</tr>
		</table>
	</body>
</html>

Hi there,

try adding me. in front of your function call.

For instance:

me.MonthCheck(MonthCount)

I tampered with your function to get it to work. Its probably not all needed but some of it might be. I always use codebehind you see I don't use inline coding anymore. I;m not that familiar with writing function within the page like you have,...

public Function MonthCheck(num as integer) as string

	If num < 10 Then
		MonthCheck = "0" & cstr(num)
	Else
		MonthCheck = cstr(num)
	End If

End Function

A couple of things

1. I believe your error is coming from the fact that you have a Function called MonthCheck and a variable called MonthCheck.

2. You are using Visual Studio.net is there a particular reason you are writing classic ASP style spaghetti code instead of using the code behind and web server controls. it might take a little time to get used to, but once you do, you won't believe you ever even tried to do it this way.

3. you really should not just dim a variable, you should define them as a specific type. It makes things so much easier in the long run. For example, you do this:
' Your Code
While MyReader2.Read

Dim DateIssued
Dim DateDay
Dim DateMonth
Dim DateYear

DateIssued = Split(MyReader2.GetValue(2), "/")
DateDay = CInt(DateIssued(0))
DateMonth = CInt(DateIssued(1))
DateYear = CInt(DateIssued(2))
' end of your code
It would be so much easier to do this

Dim DateIssued as Date
DateIssued = ctype(MyReader2.GetValue(2),Date)

Then you could easily use the built in Date Functions DateIssued.Month, DateIssued.Day etc...

4. It looks like you are using a combination of classic ASP and ASP.net. While this approach can work, it has been my experience that it is usually easier to rewrite the entire application in .net then just part of it.

Finally, I would drop the monthcheck function completely. Try this instead:
First Dim MonthCount as an integer then you can do this
Monthcount.ToString.PadLeft(2, "0")

That would automatically add a 0 to the left of a single digit month number.


Hope this helps, I don't mean to be critical of your style of coding, I know we all learn at different speeds and are all at different stages of development. One of the things i love about ASP and ASP.net is there are an almost unlimited number of ways to accomplish a task. But I was looking over what it is that you are doing and I would bet that I could rewrite the page using less than half the code doing it the .net way even if you replaced the person_proccess.asp page you could still probably do it in less code that you typed for this page.

Seposm

".. I don't mean to be critical of your style of coding, I know we all learn at different speeds and are all at different stages of development. One of the things i love about ASP and ASP.net is there are an almost unlimited number of ways to accomplish a task. But I was looking over what it is that you are doing and I would bet that I could rewrite the page using less than half the code doing it the .net way even if you replaced the person_proccess.asp page you could still probably do it in less code that you typed for this page."

I totally agree with this assessment. Everything you said was bang on. I think that in the days of .NET and OOP (as close to it as we are and can be), spaghetti code is the way of the past. Or at least is should be in my opinion.

Let us know how things work out!

:cool:

Happy Coding

Hello,

I am writing a windows application in ASP.net and I am getting the following error message. I tried so many things and it did not work. Would you please help me for the due date is today.

Thanks!
See the end of this message for details on invoking
just-in-time (JIT) debugging instead of this dialog box.
************** Exception Text **************
System.NullReferenceException: Object variable or With block variable not set.
at Microsoft.VisualBasic.CompilerServices.LateBinding.LateGet(Object o, Type objType, String name, Object[] args, String[] paramnames, Boolean[] CopyBack)
at ExtractExcelAutomatedEmail.Form1.Page_Load(Object sender, EventArgs e) in C:\Inetpub\wwwroot\ExtractExcelAutomatedEmail\ExtractExcelAutomatedEmail\Form1.vb:line 73
at System.Windows.Forms.Form.OnLoad(EventArgs e)
at System.Windows.Forms.Form.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WmShowWindow(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

************** Loaded Assemblies **************
mscorlib
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2407
CodeBase: file:///c:/windows/microsoft.net/framework/v1.1.4322/mscorlib.dll
----------------------------------------
ExtractExcelAutomatedEmail
Assembly Version: 1.0.3398.13026
Win32 Version: 1.0.3398.13026
CodeBase: file:///E:/ExtractExcelAutomatedEmail/ExtractExcelAutomatedEmail/bin/ExtractExcelAutomatedEmail.exe
----------------------------------------
System.Windows.Forms
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/assembly/gac/system.windows.forms/1.0.5000.0__b77a5c561934e089/system.windows.forms.dll
----------------------------------------
System
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2407
CodeBase: file:///c:/windows/assembly/gac/system/1.0.5000.0__b77a5c561934e089/system.dll
----------------------------------------
System.Drawing
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/assembly/gac/system.drawing/1.0.5000.0__b03f5f7f11d50a3a/system.drawing.dll
----------------------------------------
Microsoft.VisualBasic
Assembly Version: 7.0.5000.0
Win32 Version: 7.10.6001.4
CodeBase: file:///c:/windows/assembly/gac/microsoft.visualbasic/7.0.5000.0__b03f5f7f11d50a3a/microsoft.visualbasic.dll
----------------------------------------
System.Data
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/assembly/gac/system.data/1.0.5000.0__b77a5c561934e089/system.data.dll
----------------------------------------
System.Xml
Assembly Version: 1.0.5000.0
Win32 Version: 1.1.4322.2032
CodeBase: file:///c:/windows/assembly/gac/system.xml/1.0.5000.0__b77a5c561934e089/system.xml.dll
----------------------------------------
************** JIT Debugging **************
To enable just in time (JIT) debugging, the config file for this
application or machine (machine.config) must have the
jitDebugging value set in the system.windows.forms section.
The application must also be compiled with debugging
enabled.
For example:
<configuration>
<system.windows.forms jitDebugging="true" />
</configuration>
When JIT debugging is enabled, any unhandled exception
will be sent to the JIT debugger registered on the machine
rather than being handled by this dialog.

This article has been dead for over six months. Start a new discussion instead.