User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 397,752 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,536 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 43867 | Replies: 3
Reply
Join Date: Jan 2005
Posts: 8
Reputation: AlexClifford is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
AlexClifford AlexClifford is offline Offline
Newbie Poster

'Object variable or With block variable not set' Error

  #1  
Jan 24th, 2005
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>
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jan 2005
Location: Chester
Posts: 3
Reputation: michaelsowa is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
michaelsowa michaelsowa is offline Offline
Newbie Poster

Solution Re: 'Object variable or With block variable not set' Error

  #2  
Jan 28th, 2005
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
Reply With Quote  
Join Date: Jun 2005
Posts: 1
Reputation: Seposm is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Seposm Seposm is offline Offline
Newbie Poster

Re: 'Object variable or With block variable not set' Error

  #3  
Jun 24th, 2005
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
Reply With Quote  
Join Date: Feb 2003
Location: Canada
Posts: 786
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Rep Power: 9
Solved Threads: 25
Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

Re: 'Object variable or With block variable not set' Error

  #4  
Jun 24th, 2005
".. 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!



Happy Coding
Assistant Manager, Regional Pharmacy Information Systems
TLC Services Website (Under Construction)
Updated : ASP.Net Login Code
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb ASP.NET Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 3:31 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC