Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

'Object variable or With block variable not set' Error

Status
Not open for further replies.

ajclifford

Programmer
Jan 16, 2005
17
AU
Hi,

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

Code:
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.

Code:
<%@ 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" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">[/URL]
<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="[URL unfurl="true"]http://schemas.microsoft.com/intellisense/ie5">[/URL]
		<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>
 
It will be something to do with your MonthCheck function, however, you will have to bebug the project and see what line the error occurs on as it could potentially occur anywhere where an object doesn't exist.

I did notice, however, that you have a both a function and a variable named MonthCheck.

----------------------------------------------------------------------

Need help finding an answer?

Try the search facilty ( or read FAQ222-2244 on how to get better results.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top