'Object variable or With block variable not set' Error

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Jan 2005
Posts: 8
Reputation: AlexClifford is an unknown quantity at this point 
Solved Threads: 0
AlexClifford AlexClifford is offline Offline
Newbie Poster

'Object variable or With block variable not set' Error

 
0
  #1
Jan 25th, 2005
Hi,

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

  1. Object variable or With block variable not set.
  2. 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.
  3.  
  4. Exception Details: System.NullReferenceException: Object variable or With block variable not set.
  5.  
  6. Source Error:
  7.  
  8.  
  9. Line 215: Response.Write ("<option selected value=""" & MonthCheck(MonthCount) & """>" & MonthCount & "</option>" & vbCrLf)
  10. Line 216:Else
  11. Line 217: Response.Write ("<option value=""" & MonthCheck(MonthCount) & """>" & MonthCount & "</option>" & vbCrLf)
  12. Line 218:End If
  13. 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.

  1. <%@ Import Namespace="System.Data.SqlClient" %>
  2. <%@ Page Language="vb" AutoEventWireup="false" Codebehind="person_edit.aspx.vb" Explicit="True" Inherits="Contractors.person_edit" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4. <html lang="en">
  5. <head>
  6. <title>Contractors and Consultants Induction Search Tool</title>
  7. <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
  8. <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
  9. <meta name="vs_defaultClientScript" content="VBScript">
  10. <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
  11. <meta http-equiv="content-type" content="text/html;charset=utf-8">
  12. <meta http-equiv="Content-Style-Type" content="text/css">
  13. <link rel="stylesheet" href="style.css" type="text/css">
  14. </head>
  15. <script runat="server" language="vb">
  16. Function MonthCheck(num)
  17.  
  18. If Len(num) < 2 Then
  19. MonthCheck = "0" & num
  20. Else
  21. MonthCheck = num
  22. End If
  23.  
  24. End Function
  25. </script>
  26. <%
  27.  
  28. Dim MyConnection As SqlConnection
  29. Dim MyCommand As SqlCommand
  30. Dim MyReader As SqlDataReader
  31. Dim MyBinder
  32. Dim sSQL As String
  33. Dim nameSQL As String
  34.  
  35. Dim SelectedPerson
  36.  
  37. MyConnection = New SqlConnection("server=DBASESVR;uid=CSC;pwd=CSC!;database=Contractors")
  38. MyConnection.Open()
  39. SelectedPerson = CStr(Request.Form("SelectedPerson"))
  40.  
  41. %>
  42. <body>
  43. <table cellspacing="0" cellpadding="0" border="0">
  44. <tr>
  45. <td width="20" background="images/topleft_corner.gif" height="20"></td>
  46. <td background="images/top_border.gif" height="20"></td>
  47. <td width="20" background="images/topright_corner.gif" height="20"></td>
  48. </tr>
  49. <tr>
  50. <td width="20" background="images/left_border.gif"></td>
  51. <td valign="top" bgcolor="#f5f5f5">
  52. <span class="title">Contractors and Consultants Database Search</span><br />
  53. <br />
  54. <br />
  55. <table align="center" width="600" cellpadding="10">
  56. <tr>
  57. <%
  58.  
  59. If SelectedPerson = "" Then
  60.  
  61. %>
  62. <td class="info" colspan="5">
  63. To edit a person's details, please select them from the listbox.<br />
  64. <br />
  65. <%
  66.  
  67. nameSQL = "SELECT ID, VendorNo, DateIssued, Surname, FirstName, ContactNumber, Address FROM Contacts"
  68. MyCommand = New SqlCommand(nameSQL, MyConnection)
  69. MyReader = MyCommand.ExecuteReader
  70. Contractors.DataSource = MyReader
  71. Contractors.DataBind()
  72.  
  73. %>
  74. <form runat="server">
  75. <asp:Repeater id="Contractors" runat="server">
  76.  
  77. <HeaderTemplate>
  78. <select name="SelectedPerson" size="2" style="height:200px;">
  79. </HeaderTemplate>
  80.  
  81. <ItemTemplate>
  82. <option value="<%#Container.DataItem("ID")%>"><%#Container.DataItem("FirstName")%> <%#Container.DataItem("Surname")%></option>
  83. </ItemTemplate>
  84.  
  85. <FooterTemplate>
  86. </select>
  87. <br /><br />
  88. <input type="submit" name="Edit" value="Edit" style="width:50px" />
  89. </FooterTemplate>
  90.  
  91. </asp:Repeater>
  92. </form>
  93. </td>
  94. <%
  95.  
  96. Else
  97.  
  98.  
  99. Dim MyCommand2 As SqlCommand
  100. Dim MyReader2 As SqlDataReader
  101. Dim MyBinder2
  102. Dim sSQL2 As String
  103. Dim nameSQL2 As String
  104.  
  105. nameSQL2 = "SELECT ID, VendorNo, DateIssued, Surname, FirstName, ContactNumber, Address FROM Contacts WHERE ID = " & SelectedPerson
  106. MyCommand2 = New SqlCommand(nameSQL2, MyConnection)
  107. MyReader2 = MyCommand2.ExecuteReader
  108.  
  109. While MyReader2.Read
  110.  
  111. Dim DateIssued
  112. Dim DateDay
  113. Dim DateMonth
  114. Dim DateYear
  115.  
  116. DateIssued = Split(MyReader2.GetValue(2), "/")
  117. DateDay = CInt(DateIssued(0))
  118. DateMonth = CInt(DateIssued(1))
  119. DateYear = CInt(DateIssued(2))
  120.  
  121. %>
  122. <td class="info" colspan="5">
  123. Edit the following fields then submit the changes.
  124. <br /><br />
  125. <form method="post" action="person_process.asp">
  126. <table cellpadding="10">
  127. <tr>
  128. <td class="heading2">
  129. First Name:
  130. </td>
  131. <td>
  132. <input type="text" name="FirstName" value="<%=MyReader2.GetValue(4)%>" maxlength="50" style="width:200px" />
  133. </td>
  134. </tr>
  135. <tr>
  136. <td class="heading2">
  137. Surname:
  138. </td>
  139. <td>
  140. <input type="text" name="Surname" value="<%=MyReader2.GetValue(3)%>" maxlength="50" style="width:200px" />
  141. </td>
  142. </tr>
  143. <tr>
  144. <td class="heading2">
  145. Address:
  146. </td>
  147. <td>
  148. <input type="text" name="Address" value="<%=MyReader2.GetValue(6)%>" maxlength="50" style="width:200px" />
  149. </td>
  150. </tr>
  151. <tr>
  152. <td class="heading2">
  153. Contact Number:
  154. </td>
  155. <td>
  156. <input type="text" name="ContactNumber" value="<%=MyReader2.GetValue(5)%>" maxlength="50" style="width:200px" />
  157. </td>
  158. </tr>
  159. <tr>
  160. <td class="heading2">
  161. Vendor Number:
  162. </td>
  163. <td>
  164. <input type="text" name="VendorNo" value="<%=MyReader2.GetValue(1)%>" maxlength="50" style="width:200px" />
  165. </td>
  166. </tr>
  167. <tr>
  168. <td class="heading2">
  169. Date Issued:
  170. </td>
  171. <td>
  172. <select name="DayIssued" size="1">
  173. <%
  174.  
  175. dim DayCount
  176. DayCount = 1
  177.  
  178. Do While DayCount < 32
  179.  
  180. If CInt(DateIssued(0)) = CInt(DayCount) Then
  181. Response.Write ("<option selected value=""" & DayCount & """>" & DayCount & "</option>" & vbCrLf)
  182. Else
  183. Response.Write ("<option value=""" & DayCount & """>" & DayCount & "</option>" & vbCrLf)
  184. End If
  185.  
  186. DayCount = DayCount + 1
  187.  
  188. Loop ' END: Do While DayCount < 32
  189.  
  190. %>
  191. </select>
  192.  
  193. <strong>:</strong>
  194.  
  195. <select name="MonthIssued" size="1">
  196. <%
  197.  
  198. Dim MonthCount
  199. dim MonthCheck
  200. MonthCount = 1
  201.  
  202. Do While MonthCount < 13
  203.  
  204. If CInt(DateIssued(1)) = CInt(MonthCount) Then
  205. Response.Write ("<option selected value=""" & MonthCheck(MonthCount) & """>" & MonthCount & "</option>" & vbCrLf)
  206. Else
  207. Response.Write ("<option value=""" & MonthCheck(MonthCount) & """>" & MonthCount & "</option>" & vbCrLf)
  208. End If
  209.  
  210. MonthCount = MonthCount + 1
  211.  
  212. Loop ' END: Do While MonthCount < 13
  213.  
  214. %>
  215. </select>
  216.  
  217. <strong>:</strong>
  218.  
  219. <select name="YearIssued" size="1">
  220. <%
  221.  
  222. dim YearCount
  223. YearCount = Year(Now) -1
  224.  
  225. Do While YearCount < (Year(Now) + 2)
  226.  
  227. If CInt(DateIssued(2)) = CInt(YearCount) Then
  228. Response.Write ("<option selected value=""" & YearCount & """>" & YearCount & "</option>" & vbCrLf)
  229. Else
  230. Response.Write ("<option value=""" & YearCount & """>" & YearCount & "</option>" & vbCrLf)
  231. End If
  232.  
  233. YearCount = YearCount + 1
  234.  
  235. Loop ' END: Do While YearCount < (Year(Now) + 2)
  236.  
  237. %>
  238. </select>
  239. </td>
  240. </tr>
  241. <tr>
  242. <td>
  243. <input type="submit" name="submit" value="Save" style="width:80px" />
  244. <input type="hidden" name="personid" value="<%=SelectedPerson%>" />
  245. </td>
  246. </tr>
  247. </table>
  248. </form>
  249. </td>
  250. <%
  251.  
  252. End While
  253.  
  254. MyConnection.Close()
  255.  
  256. End If ' END: If SelectedPerson = ""
  257.  
  258. %>
  259. </tr>
  260. </table>
  261. <br />
  262. <div style="text-align: right;"><a href="index.aspx">Index</a></div>
  263. </td>
  264. <td width="20" background="images/right_border.gif"></td>
  265. </tr>
  266. <tr>
  267. <td width="20" background="images/bottomleft_corner.gif" height="20"></td>
  268. <td background="images/bottom_border.gif" height="20"></td>
  269. <td width="20" background="images/bottomright_corner.gif" height="20"></td>
  270. </tr>
  271. </table>
  272. </body>
  273. </html>
  274.  
Reply With Quote Quick reply to this message  
Join Date: Jan 2005
Posts: 3
Reputation: michaelsowa is an unknown quantity at this point 
Solved Threads: 0
michaelsowa michaelsowa is offline Offline
Newbie Poster

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

 
0
  #2
Jan 28th, 2005
Hi there,

try adding me. in front of your function call.

For instance:

  1. 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,...

  1.  
  2. public Function MonthCheck(num as integer) as string
  3.  
  4. If num < 10 Then
  5. MonthCheck = "0" & cstr(num)
  6. Else
  7. MonthCheck = cstr(num)
  8. End If
  9.  
  10. End Function
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1
Reputation: Seposm is an unknown quantity at this point 
Solved Threads: 0
Seposm Seposm is offline Offline
Newbie Poster

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

 
0
  #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 Quick reply to this message  
Join Date: Feb 2003
Posts: 793
Reputation: Paladine has a spectacular aura about Paladine has a spectacular aura about Paladine has a spectacular aura about 
Solved Threads: 27
Team Colleague
Paladine's Avatar
Paladine Paladine is offline Offline
Master Poster

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

 
0
  #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, Pharmacy Informatics
Wordpress Learning Blog
Updated : ASP.Net Login Code
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 1
Reputation: zoo999 is an unknown quantity at this point 
Solved Threads: 0
zoo999 zoo999 is offline Offline
Newbie Poster

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

 
0
  #5
Apr 23rd, 2009
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC