Object reference not set to an instance of an object. -- Please Urgent

M

Moe Sizlak

Hi There,

I am trying to write the selected value of a listcontrol when a button is
clicked and I keep getting the error "object not set to a reference of an
object". The libox itself is in a usercontrol and all i'm really needing to
do is get the selected value when the button is clicked on the form.

Can someone tell what I need to include in my page to get this working ?

Moe <><

!-- response.write

response.write (lstS.selectedItem.tostring())

response.end


!-- class function

Public Class CommonFunctions
Public Shared Function BindStates() as SqlDataReader

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)
Return objDr
End Function
End Class

!-- bind the contro

lstS.DataSource = CommonFunctions.BindStates
lstS.DataBind()
lstS.Items.Insert(0, new ListItem("By State","0"))

!- lisbox on page

<asp:listbox id="lstS" runat="server" rows=1
DataTextField="Location" DataValueField="LocationID"/>
 
S

S. Justin Gengo

Moe,

First I am assuming that you aren't trying to repsponse.write the value
until after it's selected. You'll get the object reference error right away
otherwise since no value has been selected yet.

Second, you need to make certain that the code populating the list only
fires on page load. If not, it is resetting the list every time. Do this
with the following if then:

If Not Page.IsPostBack Then
'---Populate list only once here
End If


--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
M

Moe Sizlak

Hi Justin,

I have the listmenu on the page and I do make sure that the menu is selected
before clicking the button, the load sud where I bind the list is in the
user control. Would this make a difference?

Moe

Sub Page_Load(byVal obj As Object, byVal e As EventArgs)
If Not Page.IsPostBack Then
lstS.DataSource = CommonFunctions.BindStates
lstS.DataBind()
lstS.Items.Insert(0, new ListItem("By State","0"))
End If
End Sub
 
S

S. Justin Gengo

Moe,

No, that looks good. It shouldn't make a difference that it's in a user
control.

Are you adding the user control to the page dynamically?

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
M

Moe Sizlak

Hi Justin,

The control I am using is "includes/_control_content.ascx" and I just
include it in the main page. What else could it be?

Sean

<% @Import Namespace="System.Data" %>
<% @Import Namespace="System.Data.SqlClient" %>
<% @Import Namespace="System.Web.UI.WebControls.DropDownList" %>
<%@ Import Namespace="System.Web.UI.WebControls.CheckBox" %>
<%@ Register TagPrefix="UserControl2" TagName="PlaceHeader"
Src="includes/_placelistingsuser_header.ascx" %>
<%@ Register TagPrefix="UserControl3" TagName="Control"
Src="includes/_control_content.ascx" %>
<%@ Register TagPrefix="UserControl4" TagName="FormList"
Src="includes/_formlist_content.ascx" %>
<link rel="stylesheet" type="text/css" href=">
<script language="vb" runat="server">



</script>


<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>
<form enctype="multipart/form-data" runat="server">
<body topmargin="0" marginheight="0" marginwidth="0">
<table width="100%" border="0" cellpadding="0" cellspacing="0">
<td colspan="2" valign=top><UserControl2:placeHeader runat="server"
/></td>
<tr>
<td width="563" valign=top style="padding-left:40px "><div align=left
class=formtext sty style="padding-left:8px "></div><br><UserControl3:Control
runat="server" /></td>
<td>2</td>
</tr>

<TR>
<td colspan=2>&nbsp;</td>
<td>
</tr>
</table>
</form>
</body>
</html>
 
M

Moe Sizlak

Hi Justin,

Any other ideas? have a forgotten to declare something in my page?
 
S

S. Justin Gengo

Moe,

Are you trying to response.write the value from the user control itself or
from the page the control is on?

If you are trying to write it from the page the control is on you can't
refer to it directly. You have to refer to it through the user control
itself.

You could do this two ways:

One would be to just response.write the value from the control itself using
the data list's OnSelectedValueChanged handler

The other would be to use FindControl on the main page to get a reference to
the user control object and then use that to refer to the datalist.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
 
M

Moe Sizlak

Hi Justin,

I am trying to use the response.write from the user control itself, I
managed to create a class in a code behind then reference the control in the
usercontrol page. The control displays correctly but when I try to catch the
value from the listbox it gives me the message "text is not a member of
string". I know that I'm almost there!!

Once I have the basic framework with this piece of code I will be able to
rollon from there.

Sean
 
D

Dan Williams

Hi.

Did anyone manage to find the solution to this problem?

I'm having a similar issue with populating a drop-down list menu from
a SQLDataReader.

Here is my function:-

Function SelectOptions(ByVal queryString) As
System.Data.SqlClient.SqlDataReader
' Application("ConnectionString") is set in our global.asax file

Dim sqlConnection As System.Data.SqlClient.SqlConnection = New
System.Data.SqlClient.SqlConnection(Application("ConnectionString"))

Dim sqlCommand As System.Data.SqlClient.SqlCommand = New
System.Data.SqlClient.SqlCommand(queryString, sqlConnection)

sqlConnection.Open()
Dim dataReader As System.Data.SqlClient.SqlDataReader =
sqlCommand.ExecuteReader(System.Data.CommandBehavior.CloseConnection)

Return dataReader
End Function

If I call this function directly in my aspx page it works fine, no
problem.

However, if i attempt to call it via my own custom web user control
ascx page, it causes a "Object reference not set to an instance of an
object." error.

Here is my non-working code:-

Dim func As New CommonFunctions
dropdownlist.DataSource = func.SelectOptions("SELECT colname FROM
table")
dropdownlist.DataBind()

Any help is much appreciated.

Thanks in advance

Dan Williams.
 
D

Dan Williams

It appears that my web user control didn't like my
Application("ConnectionString") setting.

As soon as i referenced it using
System.Web.HttpContext.Current.Application("connectionString"), it
worked straight away, no problem.

Cheers

Dan.
 

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

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top