Object reference not set to an instance of an object

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

Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Object reference not set to an instance of an object

 
0
  #1
Feb 22nd, 2008
I've been working on a page that has ddls populating other ddls and form fields. Right now I'm getting this error:

erver Error in '/HRIService' Application.
Object reference not set to an instance of an object.
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 reference not set to an instance of an object.

Source Error:

Line 115: ' Try
Line 116: cmd = New System.Data.SqlClient.SqlCommand("SELECT cusID, FirstName+ ' ' + LastName AS ContactName FROM Contacts WHERE cusID=@cusID ORDER BY LastName, FirstName", conn)
Line 117: cmd.Parameters.AddWithValue("@cusID", DropDownList1.SelectedValue)
Line 118:
Line 119: conn.Open()


Source File: C:\Inetpub\wwwroot\HRIService\ServiceExpress\AddCall.aspx.vb Line: 117

Here is my code:

  1. Imports System.Data
  2. Imports System.Data.SqlClient
  3.  
  4. Partial Class AddCall
  5. Inherits System.Web.UI.Page
  6.  
  7. Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=IT-P02\SQLEXPRESS;Initial Catalog=HRIService;Integrated Security=True")
  8. Dim cmd As System.Data.SqlClient.SqlCommand
  9. Dim reader As System.Data.SqlClient.SqlDataReader
  10. Dim reader2 As System.Data.SqlClient.SqlDataReader
  11.  
  12. Protected Sub Page_Load(ByVal S As Object, ByVal E As System.EventArgs)
  13.  
  14. If Not Page.IsPostBack Then
  15.  
  16. Dim bol As Boolean = False
  17. Dim conID As String = Trim(Request.QueryString("conID")).ToString()
  18. Dim cusID As String = Trim(Request.QueryString("cusID")).ToString()
  19.  
  20. If Len(cusID) > 0 And Len(conID) > 0 Then
  21.  
  22. Dim FirstName As TextBox = TryCast(FormView1.FindControl("FirstName"), TextBox)
  23. Dim LastName As TextBox = TryCast(FormView1.FindControl("LastName"), TextBox)
  24. Dim Company As TextBox = TryCast(FormView1.FindControl("Company"), TextBox)
  25. Dim Address1 As TextBox = TryCast(FormView1.FindControl("Address1"), TextBox)
  26. Dim Address2 As TextBox = TryCast(FormView1.FindControl("Address2"), TextBox)
  27. Dim Phone As TextBox = TryCast(FormView1.FindControl("Phone"), TextBox)
  28. Dim City As TextBox = TryCast(FormView1.FindControl("City"), TextBox)
  29. Dim State As TextBox = TryCast(FormView1.FindControl("State"), TextBox)
  30. Dim Zip As TextBox = TryCast(FormView1.FindControl("Zip"), TextBox)
  31.  
  32. ' Try
  33. cmd = New System.Data.SqlClient.SqlCommand("SELECT cusID, FirstName+ ' ' + LastName AS ContactName FROM Contacts WHERE cusID=@cusID ORDER BY LastName, FirstName", conn)
  34. cmd.Parameters.AddWithValue("@cusID", cusID)
  35.  
  36. conn.Open()
  37. reader = cmd.ExecuteReader()
  38.  
  39. If reader.HasRows Then
  40. DropDownList2.DataSource = reader
  41. DropDownList2.DataTextField = "ContactName"
  42. DropDownList2.DataValueField = "cusID"
  43. DropDownList2.DataBind()
  44. DropDownList2.Items.Insert(0, ("-- Choose a Contact --"))
  45. DropDownList2.SelectedIndex = DropDownList2.Items.IndexOf(DropDownList2.Items.FindByValue(conID))
  46. Else
  47. DropDownList2.Items.Insert(0, ("-- Error: No Contacts Found --"))
  48. End If
  49.  
  50. reader.Close()
  51.  
  52. cmd = New System.Data.SqlClient.SqlCommand("SELECT a.Company, a.Address1, a.Address2, b.FirstName, b.LastName, c.City, c.State, c.Zip FROM Customers a JOIN Contacts b ON a.cusID=b.cusID JOIN Zip c ON a.cusID=c.cusID WHERE a.cusID=@cusID and b.ContactID=@conID", conn)
  53. cmd.Parameters.AddWithValue("@cusID", cusID)
  54. cmd.Parameters.AddWithValue("@ContactID", conID)
  55. reader2 = cmd.ExecuteReader()
  56.  
  57. If reader.HasRows Then
  58. While reader.Read()
  59. Company.Text = reader.Item("Company").ToString
  60. Address1.Text = reader.Item("Address1").ToString
  61. Address2.Text = reader.Item("Address2").ToString
  62. Phone.Text = reader.Item("Phone").ToString
  63. FirstName.Text = reader.Item("FirstName").ToString()
  64. LastName.Text = reader.Item("LastName").ToString()
  65. City.Text = reader.Item("City").ToString()
  66. State.Text = reader.Item("State").ToString()
  67. Zip.Text = reader.Item("Zip").ToString()
  68. End While
  69. End If
  70.  
  71. reader.Close()
  72. conn.Close()
  73. ' Catch
  74. ' End Try
  75.  
  76. bol = True
  77.  
  78. End If
  79.  
  80. cmd = New System.Data.SqlClient.SqlCommand("SELECT cusID, Company FROM Customers", conn)
  81.  
  82. ' Try
  83. conn.Open()
  84. reader = cmd.ExecuteReader()
  85.  
  86. If reader.HasRows Then
  87. DropDownList1.DataSource = reader
  88. DropDownList1.DataTextField = "Company"
  89. DropDownList1.DataValueField = "cusID"
  90. DropDownList1.DataBind()
  91. DropDownList1.Items.Insert(0, ("-- Choose a Company --"))
  92. If bol = True Then DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(conID))
  93. Else
  94. DropDownList1.Items.Insert(0, ("-- Error: No Companies Found --"))
  95. End If
  96.  
  97. reader.Close()
  98. conn.Close()
  99. ' Catch
  100. ' End Try
  101.  
  102. End If
  103.  
  104. End Sub
  105.  
  106.  
  107. Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
  108.  
  109. Dim FirstName As TextBox = TryCast(FormView1.FindControl("FirstName"), TextBox)
  110. Dim LastName As TextBox = TryCast(FormView1.FindControl("LastName"), TextBox)
  111. Dim DropDownListEquip As DropDownList = TryCast(FormView1.FindControl("DropDownListEquip"), DropDownList)
  112. Dim DropDownList1 As DropDownList = TryCast(FormView1.FindControl("DropDownList1"), DropDownList)
  113.  
  114.  
  115. ' Try
  116. cmd = New System.Data.SqlClient.SqlCommand("SELECT cusID, FirstName+ ' ' + LastName AS ContactName FROM Contacts WHERE cusID=@cusID ORDER BY LastName, FirstName", conn)
  117. cmd.Parameters.AddWithValue("@cusID", DropDownList1.SelectedValue)
  118.  
  119. conn.Open()
  120. reader = cmd.ExecuteReader()
  121.  
  122. If reader.HasRows Then
  123. DropDownList2.DataSource = reader
  124. DropDownList2.DataTextField = "ContactName"
  125. DropDownList2.DataValueField = "cusID"
  126. DropDownList2.DataBind()
  127. DropDownList2.Items.Insert(0, ("-- Choose a Contact --"))
  128. Else
  129. DropDownList2.Items.Insert(0, ("-- Error: No Contacts Found --"))
  130. End If
  131.  
  132. reader.Close()
  133.  
  134. cmd = New System.Data.SqlClient.SqlCommand("SELECT a.Company + ' - ' + b.Description As CompanyEqpDesc, b.eqpID FROM Manufacturers a JOIN Equipment b ON a.mfgID=b.mfgID WHERE b.eqpID IN (SELECT eqpID FROM Cus_Equip WHERE cusID=@cusID)", conn)
  135. cmd.Parameters.AddWithValue("@cusID", DropDownList1.SelectedValue)
  136.  
  137. reader2 = cmd.ExecuteReader()
  138.  
  139. If reader.HasRows Then
  140. DropDownListEquip.DataSource = reader
  141. DropDownListEquip.DataTextField = "CompanyEqpDesc"
  142. DropDownListEquip.DataValueField = "eqpID"
  143. DropDownListEquip.DataBind()
  144. DropDownListEquip.Items.Insert(0, ("-- Choose Equipment --"))
  145. Else
  146. DropDownListEquip.Items.Insert(0, ("-- Error: No Equipment Found --"))
  147. End If
  148.  
  149. reader.Close()
  150. conn.Close()
  151. ' Catch
  152. ' End Try
  153.  
  154. FirstName.Text = ""
  155. LastName.Text = ""
  156.  
  157. End Sub
  158.  
  159.  
  160. Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged
  161.  
  162. Dim Company As TextBox = TryCast(FormView1.FindControl("Company"), TextBox)
  163. Dim Address1 As TextBox = TryCast(FormView1.FindControl("Address1"), TextBox)
  164. Dim Address2 As TextBox = TryCast(FormView1.FindControl("Address2"), TextBox)
  165. Dim Phone As TextBox = TryCast(FormView1.FindControl("Phone"), TextBox)
  166. Dim FirstName As TextBox = TryCast(FormView1.FindControl("FirstName"), TextBox)
  167. Dim LastName As TextBox = TryCast(FormView1.FindControl("LastName"), TextBox)
  168. Dim City As TextBox = TryCast(FormView1.FindControl("City"), TextBox)
  169. Dim State As TextBox = TryCast(FormView1.FindControl("State"), TextBox)
  170. Dim Zip As TextBox = TryCast(FormView1.FindControl("Zip"), TextBox)
  171. Dim DropDownList2 As DropDownList = TryCast(FormView1.FindControl("DropDownList2"), DropDownList)
  172.  
  173. cmd = New System.Data.SqlClient.SqlCommand("SELECT a.Company, a.Address1, a.Address2, b.FirstName, b.LastName, c.City, c.State, c.Zip FROM Customers a JOIN Contacts b ON a.cusID=b.cusID JOIN Zip c ON a.cusID=c.cusID WHERE a.cusID=@cusID and b.ContactID=@conID", conn)
  174. cmd.Parameters.AddWithValue("@cusID", DropDownList1.SelectedValue)
  175. cmd.Parameters.AddWithValue("@conID", DropDownList2.SelectedValue)
  176.  
  177. ' Try
  178. conn.Open()
  179. reader = cmd.ExecuteReader()
  180.  
  181. If reader.HasRows Then
  182. While reader.Read()
  183. Company.Text = reader.Item("Company").ToString
  184. Address1.Text = reader.Item("Address1").ToString
  185. Address2.Text = reader.Item("Address2").ToString
  186. Phone.Text = reader.Item("Phone").ToString
  187. FirstName.Text = reader.Item("FirstName").ToString()
  188. LastName.Text = reader.Item("LastName").ToString()
  189. City.Text = reader.Item("City").ToString()
  190. State.Text = reader.Item("State").ToString()
  191. Zip.Text = reader.Item("Zip").ToString()
  192. End While
  193. End If
  194.  
  195. reader.Close()
  196. conn.Close()
  197. ' Catch
  198. 'End Try
  199.  
  200. End Sub
  201.  
  202.  
  203. Public Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
  204.  
  205. Dim disID As String
  206. Dim recs As Integer = 0
  207. Dim Problem As TextBox = TryCast(FormView1.FindControl("Problem"), TextBox)
  208. Dim DropDownListPriority As DropDownList = TryCast(FormView1.FindControl("DropDownListPriority"), DropDownList)
  209. Dim DropDownListBill As DropDownList = TryCast(FormView1.FindControl("DropDownListBill"), DropDownList)
  210. Dim Notes As TextBox = TryCast(FormView1.FindControl("Notes"), TextBox)
  211.  
  212. cmd = New System.Data.SqlClient.SqlCommand("INSERT INTO Dispatch (cusID, Problem, Priority, Contract) VALUES (@cusID, @Problem, @Priority, @Contract); SELECT SCOPE_IDENTITY()", conn)
  213. cmd.Parameters.AddWithValue("@cusID", DropDownList1.SelectedItem.Value)
  214. cmd.Parameters.AddWithValue("@Problem", Trim(Problem.Text))
  215. cmd.Parameters.AddWithValue("@Priority", DropDownListPriority.SelectedItem.Text)
  216. cmd.Parameters.AddWithValue("@Contract", DropDownListBill.SelectedItem.Text)
  217.  
  218. ' Try
  219. conn.Open()
  220. disID = cmd.ExecuteScalar()
  221.  
  222. cmd = New System.Data.SqlClient.SqlCommand("INSERT INTO DispatchEvents (disID, Notes) VALUES (@disID, @Notes)", conn)
  223. cmd.Parameters.AddWithValue("@disID", disID)
  224. cmd.Parameters.AddWithValue("@Notes", Trim(Notes.Text))
  225.  
  226. recs = cmd.ExecuteNonQuery()
  227. conn.Close()
  228.  
  229. If Len(disID) > 0 Then recs += 1
  230.  
  231. If recs > 1 Then
  232. Response.Redirect("Dispatch.aspx")
  233. Else
  234. Response.Write("Recs Counted: " & recs & " out of 2.")
  235. End If
  236. ' Catch ex As System.Data.SqlClient.SqlException
  237. ' Response.Write(ex)
  238. ' End Try
  239.  
  240. End Sub
  241.  
  242. End Class
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Object reference not set to an instance of an object

 
0
  #2
Feb 22nd, 2008
Hi foundsheep, i got your message. Make sure that your dropdown's enableViewState attribute is set to true and make sure that you fill the drop down between the "not IsPostBack" block.

Modify your page_load handler like this :

protected void Page_Load(object sender, EventArgs e)
{
if(!IstPostBack)
{
fill the dropdowns();
}
}
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Object reference not set to an instance of an object

 
0
  #3
Feb 22nd, 2008
yeah, I did his coding but I am inline, so there's some flaws here.

It's within the ispostback.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Object reference not set to an instance of an object

 
0
  #4
Feb 22nd, 2008
Is that C#? What would it look like in VB?
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Object reference not set to an instance of an object

 
0
  #5
Feb 22nd, 2008
It's already on there. VB.NET version of his code:
  1. C#:
  2. protected void Page_Load(object sender, EventArgs e)
  3. {
  4. if(!IsPostBack)
  5. {
  6. fill the dropdowns();
  7. }
  8. }
  9.  
  10. VB.NET:
  11. protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
  12. if Not Page.IsPostBack then
  13. 'fill the dropdowns();
  14. end if
  15.  
Last edited by SheSaidImaPregy; Feb 22nd, 2008 at 1:25 pm.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Object reference not set to an instance of an object

 
0
  #6
Feb 22nd, 2008
So, that wasn't the solution.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 2,052
Reputation: serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light serkan sendur is a glorious beacon of light 
Solved Threads: 118
Featured Poster
serkan sendur serkan sendur is offline Offline
Postaholic

Re: Object reference not set to an instance of an object

 
0
  #7
Feb 22nd, 2008
You said it is within ispostback, it must be within "not ispostback"
Due to lack of freedom of speech, i no longer post on this website.
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Object reference not set to an instance of an object

 
0
  #8
Feb 22nd, 2008
I meant it's within a postback statement. So yes it's within this:

If Not Page.IsPostBack Then
...
End If
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 179
Reputation: foundsheep is an unknown quantity at this point 
Solved Threads: 0
foundsheep's Avatar
foundsheep foundsheep is offline Offline
Junior Poster

Re: Object reference not set to an instance of an object

 
0
  #9
Feb 22nd, 2008
I had some tell me this. Is it a valid question?

In this line here
Code:

cmd.Parameters.AddWithValue("@cusID", DropDownList1.SelectedValue)

Check and make sure that DropDownList1.SelectedValue is not null
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Object reference not set to an instance of an object

 
0
  #10
Feb 22nd, 2008
No, it's not a valid question. The reason is because this is on an event that isn't triggered until it is selected. Therefore, if you are getting this error at runtime and cannot even load the page, then that is not it. That event hasn't been triggered. So there is an error somewhere.

Post your aspx code please so I can do one more DEBUG for you.
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