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"/>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
"Moe Sizlak" <moe@.moe.com> wrote in message
news:%23LjercQeEHA.3552@.tk2msftngp13.phx.gbl...
> 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"/>
>
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. Justin Gengo" <sjgengo@.aboutfortunate[no-spam].com> wrote in message
news:10gu2o4t1gmb294@.corp.supernews.com...
> 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
> "Moe Sizlak" <moe@.moe.com> wrote in message
> news:%23LjercQeEHA.3552@.tk2msftngp13.phx.gbl...
is
an
> to
myCommand.ExecuteReader(CommandBehavior.CloseConnection)
>
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
"Moe Sizlak" <moe@.moe.com> wrote in message
news:e%23ckR9QeEHA.3684@.TK2MSFTNGP09.phx.gbl...
> 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. Justin Gengo" <sjgengo@.aboutfortunate[no-spam].com> wrote in message
> news:10gu2o4t1gmb294@.corp.supernews.com...
> away
> is
> an
needing
?
AS
> myCommand.ExecuteReader(CommandBehavior.CloseConnection)
>
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="http://links.10026.com/?link=>
<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> </td>
<td>
</tr>
</table>
</form>
</body>
</html>
"S. Justin Gengo" <sjgengo@.aboutfortunate[no-spam].com> wrote in message
news:10gu5g8q5cnksf0@.corp.supernews.com...
> 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
> "Moe Sizlak" <moe@.moe.com> wrote in message
> news:e%23ckR9QeEHA.3684@.TK2MSFTNGP09.phx.gbl...
> selected
value
only
this
button
of
> needing
working
> ?
> AS
BY
DataValueField="LocationID"/>
>
Hi Justin,
Any other ideas? have a forgotten to declare something in my page?
"Moe Sizlak" <moe@.moe.com> wrote in message
news:uD27XQReEHA.2848@.TK2MSFTNGP10.phx.gbl...
> 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="http://pics.10026.com/?src=includes/_placelistingsuser_header.ascx" %>
> <%@. Register TagPrefix="UserControl3" TagName="Control"
> src="http://pics.10026.com/?src=includes/_control_content.ascx" %>
> <%@. Register TagPrefix="UserControl4" TagName="FormList"
> src="http://pics.10026.com/?src=includes/_formlist_content.ascx" %>
> <link rel="stylesheet" type="text/css" href="http://links.10026.com/?link=>
> <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> </td>
> <td>
> </tr>
> </table>
> </form>
> </body>
> </html>
>
> "S. Justin Gengo" <sjgengo@.aboutfortunate[no-spam].com> wrote in message
> news:10gu5g8q5cnksf0@.corp.supernews.com...
the
message
> value
right
> only
> this
> button
reference
> of
form.
> working
SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
Location
ORDER
> BY
> DataValueField="LocationID"/>
>
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
"Moe Sizlak" <moe@.moe.com> wrote in message
news:eDpjmkReEHA.3916@.TK2MSFTNGP11.phx.gbl...
> Hi Justin,
> Any other ideas? have a forgotten to declare something in my page?
>
>
> "Moe Sizlak" <moe@.moe.com> wrote in message
> news:uD27XQReEHA.2848@.TK2MSFTNGP10.phx.gbl...
align=left
> "></div><br><UserControl3:Control
user
> the
> message
> right
Do
> reference
> form.
> SqlConnection(ConfigurationSettings.AppSettings("connectionString"))
> Location
> ORDER
>
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
"S. Justin Gengo" <sjgengo@.aboutfortunate[no-spam].com> wrote in message
news:10gv3867d7uot23@.corp.supernews.com...
> 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
> "Moe Sizlak" <moe@.moe.com> wrote in message
> news:eDpjmkReEHA.3916@.TK2MSFTNGP11.phx.gbl...
charset=iso-8859-1">
runat="server"
> align=left
message
> user
is
in
the
list
> Do
a
really
myConnection)
>
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.
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.
dan_williams@.newcross-nursing.com (Dan Williams) wrote in message news:<2eac5d02.0408250258
.287751@.posting.google.com>...
> 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.
0 comments:
Post a Comment