Object reference not set to an instance of an object.

M

Moe Sizlak

Hi There,

I have a user control that has 2 listmenus populated from a database,
I want the form to submit when the listmenu are changed which seems
to happen no problem when the page is on it's own but when I have it
as user control I get the error "Object reference not set to an
instance
of an object. " what am I doing wrong?

Moe <><

!--- page

<%@ Register TagPrefix="UserControl1" TagName="header"
Src="includes/_Header.ascx" %>
<%@ Register TagPrefix="UserControl2" TagName="footer"
Src="includes/_footer.ascx" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-
1">
<meta name="distribution" content="global">
<meta name="revisit-after" content="14 days">
<meta name="ROBOTS" content="ALL">

<title>title</title>
</head>

<body topmargin="0" leftmargin="0" marginheight="0" marginwidth="0">
<form runat="server">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<td colspan="2" valign=top><UserControl1:Header
runat="server" /></td>
<tr>
<td width="100" valign=top></td>
<td width="563" valign=top>content area</td>
</tr>

<TR>
<td colspan=2><UserControl2:footer runat="server" /></td>
<td>
</tr>
</table>
</form>
</body>
</html>


!-- user control

<%@ Control Language="vb" EnableViewState="False" %>
<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<script language="vb" runat="server">

Public Sub Page_Load(sender as Object, e as EventArgs)

If Not Page.IsPostBack Then
BindStates()
LoadCategories()
Else
Dim strFormID as Integer
Dim strSelectedStates as Integer
Dim strSelectedCategories as Integer

strSelectedCategories =
lstCategorie.SelectedItem.Value
strSelectedStates =
lstStates.SelectedItem.Value


If strSelectedStates <> "0" AND
strSelectedCategories ="0" Then
strFormID = strSelectedStates
SendUserToState(strFormID)
Elseif strSelectedStates = "0" AND
strSelectedCategories <> "0" Then
'response.write ("categories")
'response.end
strFormID = strSelectedCategories
SendUserToCategorie(strFormID)
End If

End If
End Sub

Public Function SendUserToState(ByVal strFormID as Integer)
Select Case strFormID
Case 1,2,3,4,5,6,7,8,9
response.redirect("browselistingsbystate.aspx?
StateID=" & strFormID)
Case Else
'do jack all
End Select
End Function

Public Function SendUserToCategorie(ByVal strFormID as Integer)
Select Case strFormID
Case 6,10,8,7,11,13,9,12
response.redirect("browselistings.aspx?
CategoryID=" & strFormID)
Case Else
'do jack all
End Select
End Function


Public Sub BindStates()
Try
Dim myConnection as New SqlConnection
(ConfigurationSettings.AppSettings("connectionString"))
Const strSQL as String = "SELECT
LocationID, '...' + Location + '...' AS Location " & _

"FROM tbLocation ORDER BY Location"
Dim myCommand as New SqlCommand(strSQL, myConnection)
myConnection.Open()
Dim objDR as SqlDataReader
objDR = myCommand.ExecuteReader
(CommandBehavior.CloseConnection)
lstStates.DataSource = objDR
lstStates.DataBind()
lstStates.Items.Insert(0, new ListItem("--
Choose a State --","0"))

Catch ex as InvalidCastException
Status.Text = ex.ToString()

Catch ex As SqlException
Status.Text = "Database error: " & ex.message

Catch ex As Exception
Status.Text = "General error: " & ex.message
End Try

End Sub


Public Sub LoadCategories()
Try
Dim myConnection as New SqlConnection
(ConfigurationSettings.AppSettings("connectionString"))
Const strSQL as String = "SELECT
CategoryID, '...' + CategoryDesc + '...' AS CategoryDesc " & _

"FROM tblProductCategories ORDER BY CategoryDesc"
Dim myCommand as New SqlCommand(strSQL, myConnection)
myConnection.Open()
Dim objDR as SqlDataReader
objDR = myCommand.ExecuteReader
(CommandBehavior.CloseConnection)
lstCategorie.DataSource = objDR
lstCategorie.DataBind()
lstCategorie.Items.Insert(0, new ListItem("--
Choose a Category --","0"))

Catch ex as InvalidCastException
Status.Text = ex.ToString()

Catch ex As SqlException
Status.Text = "Database error: " & ex.message

Catch ex As Exception
Status.Text = "General error: " & ex.message
End Try

End Sub

</script>




<table width="100%" border="0" cellspacing="10" cellpadding="0">
<tr>
<td width="58%"><img src="images/Header.gif" width="441"
height="47"> </td>
<td width="42%">
<div align="right"><font face="Verdana, Arial, Helvetica, sans-
serif" size="1" color="#999999"><br>
<br>
Log-in | Join Site | Terms &amp; Conditions | Contact us
</font></div>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="5"
height="20">
<tr bgcolor="#999999">
<td height="20" width="19%">
<div align="right"><font face="Verdana, Arial, Helvetica, sans-
serif" size="1" color="#FFFFFF">Sell
Irrigation equipment :</font></div>
</td>
<td height="20" width="18%">

<div align="left">

</div>

</td>
<td height="20" width="11%">
<div align="right"><font face="Verdana, Arial, Helvetica, sans-
serif" size="1" color="#FFFFFF">Browse
:</font></div>
</td>
<td height="20" width="14%">

<div align="left">
<asp:listbox id="lstCategorie" runat="server" Rows="1"
AutoPostBack="true"
DataTextField="CategoryDesc"
DataValueField="CategoryID" />

</div>

</td>
<td height="20" width="11%">
<div align="right"><font face="Verdana, Arial, Helvetica, sans-
serif" size="1" color="#FFFFFF">Browse
:</font></div>
</td>
<td height="20" width="14%">

<div align="left">
<asp:listbox id="lstStates" runat="server" Rows="1"
AutoPostBack="true"
DataTextField="Location" DataValueField="LocationID" />
</div>

</td>
<td height="20" width="17%">
<div align="center"><font face="Verdana, Arial, Helvetica, sans-
serif" size="1" color="#FFFFFF">
Search Site </font></div>
</td>
</tr>
</table>


<asp:Label id="status" runat="server"></asp:Label>
 
M

Moe Sizlak

Hi There,

it's line 16 where I try to capture the value from the listbox control.

Moe

Line 14: Dim strSelectedCategories as Integer
Line 15:
Line 16: strSelectedCategories = lstCategorie.SelectedItem.Value
Line 17: strSelectedStates = lstStates.SelectedItem.Value
Line 18:

Source File: C:\webs\linpac\includes\_Header.ascx Line: 16
 
M

Moe Sizlak

HI There,

I am only new to VS .NET and I am not sure how step through the process
given that these files are not part of a project, do you know of any
tutorials?

Moe


Tampa .NET Koder said:
I wanted to say STEP THROUGH the code
you are using VS.NET so you can set a break point at the line above the
error and look at the auto and local debugging window to see if an object is
null. So, set throught this code and see wat you find. I will see if anyone
else has a suggestion
 
M

Moe Sizlak

It's a funny thing because the code works in a single page, but when I have
it in a control file as an include the code falls over? It's as if the
listmenu objects are not being recognised when the page is a control.

Time is a luxury I do not have, I might have to look at other options.

Sean


Tampa .NET Koder said:
If your using somethign like Web Matrix or dreamweaver then you cannot
debug. You can use the visual studio help to learn about debugging,
otherwise you will have to debug the old fashion way: Comment out code and
then run your page and make sure it comes up, then uncomment each line one
by one while running your page in between. So, by that time you should know
exactly what is causing the error.
If you are using the code behind method then importing these files in
vs.net will be must easier. But you will have to create a project and then
add these pages as existing files to that project.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,763
Messages
2,569,563
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top