Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    'Object variable or With block variable not set' Error

    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" "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>

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,030
    Thanks
    0
    Thanked 247 Times in 244 Posts
    Why did you declare MonthCheck as a variable when it is a function?

    dim MonthCheck

    Remove it and see how it goes.

  • #3
    New to the CF scene
    Join Date
    Jan 2005
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It worked, thanks. I have no idea why I declared it anyway, I actually added that Function in LAST, what was I thinking :P Thanks alot, much appreciated.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •