| | |
Object reference not set to an instance of an object
Please support our ASP.NET advertiser: Intel Parallel Studio Home
![]() |
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:
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:
ASP.NET Syntax (Toggle Plain Text)
Imports System.Data Imports System.Data.SqlClient Partial Class AddCall Inherits System.Web.UI.Page Dim conn As New System.Data.SqlClient.SqlConnection("Data Source=IT-P02\SQLEXPRESS;Initial Catalog=HRIService;Integrated Security=True") Dim cmd As System.Data.SqlClient.SqlCommand Dim reader As System.Data.SqlClient.SqlDataReader Dim reader2 As System.Data.SqlClient.SqlDataReader Protected Sub Page_Load(ByVal S As Object, ByVal E As System.EventArgs) If Not Page.IsPostBack Then Dim bol As Boolean = False Dim conID As String = Trim(Request.QueryString("conID")).ToString() Dim cusID As String = Trim(Request.QueryString("cusID")).ToString() If Len(cusID) > 0 And Len(conID) > 0 Then Dim FirstName As TextBox = TryCast(FormView1.FindControl("FirstName"), TextBox) Dim LastName As TextBox = TryCast(FormView1.FindControl("LastName"), TextBox) Dim Company As TextBox = TryCast(FormView1.FindControl("Company"), TextBox) Dim Address1 As TextBox = TryCast(FormView1.FindControl("Address1"), TextBox) Dim Address2 As TextBox = TryCast(FormView1.FindControl("Address2"), TextBox) Dim Phone As TextBox = TryCast(FormView1.FindControl("Phone"), TextBox) Dim City As TextBox = TryCast(FormView1.FindControl("City"), TextBox) Dim State As TextBox = TryCast(FormView1.FindControl("State"), TextBox) Dim Zip As TextBox = TryCast(FormView1.FindControl("Zip"), TextBox) ' Try cmd = New System.Data.SqlClient.SqlCommand("SELECT cusID, FirstName+ ' ' + LastName AS ContactName FROM Contacts WHERE cusID=@cusID ORDER BY LastName, FirstName", conn) cmd.Parameters.AddWithValue("@cusID", cusID) conn.Open() reader = cmd.ExecuteReader() If reader.HasRows Then DropDownList2.DataSource = reader DropDownList2.DataTextField = "ContactName" DropDownList2.DataValueField = "cusID" DropDownList2.DataBind() DropDownList2.Items.Insert(0, ("-- Choose a Contact --")) DropDownList2.SelectedIndex = DropDownList2.Items.IndexOf(DropDownList2.Items.FindByValue(conID)) Else DropDownList2.Items.Insert(0, ("-- Error: No Contacts Found --")) End If reader.Close() 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) cmd.Parameters.AddWithValue("@cusID", cusID) cmd.Parameters.AddWithValue("@ContactID", conID) reader2 = cmd.ExecuteReader() If reader.HasRows Then While reader.Read() Company.Text = reader.Item("Company").ToString Address1.Text = reader.Item("Address1").ToString Address2.Text = reader.Item("Address2").ToString Phone.Text = reader.Item("Phone").ToString FirstName.Text = reader.Item("FirstName").ToString() LastName.Text = reader.Item("LastName").ToString() City.Text = reader.Item("City").ToString() State.Text = reader.Item("State").ToString() Zip.Text = reader.Item("Zip").ToString() End While End If reader.Close() conn.Close() ' Catch ' End Try bol = True End If cmd = New System.Data.SqlClient.SqlCommand("SELECT cusID, Company FROM Customers", conn) ' Try conn.Open() reader = cmd.ExecuteReader() If reader.HasRows Then DropDownList1.DataSource = reader DropDownList1.DataTextField = "Company" DropDownList1.DataValueField = "cusID" DropDownList1.DataBind() DropDownList1.Items.Insert(0, ("-- Choose a Company --")) If bol = True Then DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByValue(conID)) Else DropDownList1.Items.Insert(0, ("-- Error: No Companies Found --")) End If reader.Close() conn.Close() ' Catch ' End Try End If End Sub Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged Dim FirstName As TextBox = TryCast(FormView1.FindControl("FirstName"), TextBox) Dim LastName As TextBox = TryCast(FormView1.FindControl("LastName"), TextBox) Dim DropDownListEquip As DropDownList = TryCast(FormView1.FindControl("DropDownListEquip"), DropDownList) Dim DropDownList1 As DropDownList = TryCast(FormView1.FindControl("DropDownList1"), DropDownList) ' Try cmd = New System.Data.SqlClient.SqlCommand("SELECT cusID, FirstName+ ' ' + LastName AS ContactName FROM Contacts WHERE cusID=@cusID ORDER BY LastName, FirstName", conn) cmd.Parameters.AddWithValue("@cusID", DropDownList1.SelectedValue) conn.Open() reader = cmd.ExecuteReader() If reader.HasRows Then DropDownList2.DataSource = reader DropDownList2.DataTextField = "ContactName" DropDownList2.DataValueField = "cusID" DropDownList2.DataBind() DropDownList2.Items.Insert(0, ("-- Choose a Contact --")) Else DropDownList2.Items.Insert(0, ("-- Error: No Contacts Found --")) End If reader.Close() 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) cmd.Parameters.AddWithValue("@cusID", DropDownList1.SelectedValue) reader2 = cmd.ExecuteReader() If reader.HasRows Then DropDownListEquip.DataSource = reader DropDownListEquip.DataTextField = "CompanyEqpDesc" DropDownListEquip.DataValueField = "eqpID" DropDownListEquip.DataBind() DropDownListEquip.Items.Insert(0, ("-- Choose Equipment --")) Else DropDownListEquip.Items.Insert(0, ("-- Error: No Equipment Found --")) End If reader.Close() conn.Close() ' Catch ' End Try FirstName.Text = "" LastName.Text = "" End Sub Protected Sub DropDownList2_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList2.SelectedIndexChanged Dim Company As TextBox = TryCast(FormView1.FindControl("Company"), TextBox) Dim Address1 As TextBox = TryCast(FormView1.FindControl("Address1"), TextBox) Dim Address2 As TextBox = TryCast(FormView1.FindControl("Address2"), TextBox) Dim Phone As TextBox = TryCast(FormView1.FindControl("Phone"), TextBox) Dim FirstName As TextBox = TryCast(FormView1.FindControl("FirstName"), TextBox) Dim LastName As TextBox = TryCast(FormView1.FindControl("LastName"), TextBox) Dim City As TextBox = TryCast(FormView1.FindControl("City"), TextBox) Dim State As TextBox = TryCast(FormView1.FindControl("State"), TextBox) Dim Zip As TextBox = TryCast(FormView1.FindControl("Zip"), TextBox) Dim DropDownList2 As DropDownList = TryCast(FormView1.FindControl("DropDownList2"), DropDownList) 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) cmd.Parameters.AddWithValue("@cusID", DropDownList1.SelectedValue) cmd.Parameters.AddWithValue("@conID", DropDownList2.SelectedValue) ' Try conn.Open() reader = cmd.ExecuteReader() If reader.HasRows Then While reader.Read() Company.Text = reader.Item("Company").ToString Address1.Text = reader.Item("Address1").ToString Address2.Text = reader.Item("Address2").ToString Phone.Text = reader.Item("Phone").ToString FirstName.Text = reader.Item("FirstName").ToString() LastName.Text = reader.Item("LastName").ToString() City.Text = reader.Item("City").ToString() State.Text = reader.Item("State").ToString() Zip.Text = reader.Item("Zip").ToString() End While End If reader.Close() conn.Close() ' Catch 'End Try End Sub Public Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Dim disID As String Dim recs As Integer = 0 Dim Problem As TextBox = TryCast(FormView1.FindControl("Problem"), TextBox) Dim DropDownListPriority As DropDownList = TryCast(FormView1.FindControl("DropDownListPriority"), DropDownList) Dim DropDownListBill As DropDownList = TryCast(FormView1.FindControl("DropDownListBill"), DropDownList) Dim Notes As TextBox = TryCast(FormView1.FindControl("Notes"), TextBox) cmd = New System.Data.SqlClient.SqlCommand("INSERT INTO Dispatch (cusID, Problem, Priority, Contract) VALUES (@cusID, @Problem, @Priority, @Contract); SELECT SCOPE_IDENTITY()", conn) cmd.Parameters.AddWithValue("@cusID", DropDownList1.SelectedItem.Value) cmd.Parameters.AddWithValue("@Problem", Trim(Problem.Text)) cmd.Parameters.AddWithValue("@Priority", DropDownListPriority.SelectedItem.Text) cmd.Parameters.AddWithValue("@Contract", DropDownListBill.SelectedItem.Text) ' Try conn.Open() disID = cmd.ExecuteScalar() cmd = New System.Data.SqlClient.SqlCommand("INSERT INTO DispatchEvents (disID, Notes) VALUES (@disID, @Notes)", conn) cmd.Parameters.AddWithValue("@disID", disID) cmd.Parameters.AddWithValue("@Notes", Trim(Notes.Text)) recs = cmd.ExecuteNonQuery() conn.Close() If Len(disID) > 0 Then recs += 1 If recs > 1 Then Response.Redirect("Dispatch.aspx") Else Response.Write("Recs Counted: " & recs & " out of 2.") End If ' Catch ex As System.Data.SqlClient.SqlException ' Response.Write(ex) ' End Try End Sub End Class
•
•
Join Date: Jan 2008
Posts: 2,052
Reputation:
Solved Threads: 118
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();
}
}
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.
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
It's already on there. VB.NET version of his code:
ASP.NET Syntax (Toggle Plain Text)
C#: protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { fill the dropdowns(); } } VB.NET: protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) if Not Page.IsPostBack then 'fill the dropdowns(); end if
Last edited by SheSaidImaPregy; Feb 22nd, 2008 at 1:25 pm.
•
•
Join Date: Sep 2007
Posts: 1,080
Reputation:
Solved Threads: 68
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.
Post your aspx code please so I can do one more DEBUG for you.
![]() |
Similar Threads
- Updated : Simple ASP.Net Login Page (ASP.NET)
- How to set attribute to the controls in datagrid? (ASP.NET)
- how to save what u type? (VB.NET)
- sql data pull with no result gives Object reference not set exception (VB.NET)
- Listbox selectedItem.value cannot be retrieved (ASP.NET)
- Error message when passing property changes between forms (VB.NET)
- session variable:again (ASP.NET)
- Radiobuttonlist control - help! (ASP.NET)
- c++ debugging error (C++)
Other Threads in the ASP.NET Forum
- Previous Thread: load sql data into asp.net table
- Next Thread: Its not redirecting to the same page
| Thread Tools | Search this Thread |
.net activexcontrol advice ajax alltypeofvideos appliances asp asp.net bc30451 beginner bottomasp.net box browser button c# cac checkbox click commonfunctions control css dataaccesslayer database datagridview datagridviewcheckbox datalist deadlock deployment development dgv dialog dropdownlist dynamic dynamically edit embeddingactivexcontrol expose fileuploader fill findcontrol flash formatdecimal formview gridview gudi iframe iis javascript listbox login microsoft mono mouse mssql multistepregistration news novell numerical objects opera panelmasterpagebuttoncontrols radio redirect registration relationaldatabases reportemail rotatepage save schoolproject search security sessionvariables silverlight smartcard smoobjects software sql-server sqlserver2005 ssl suse textbox tracking treeview unauthorized validatedate validation vb.net video videos virtualdirectory vista visualstudio web webapplications webdevelopemnt webdevelopment webprogramming webservice xsl youareanotmemberofthedebuggerusers






