|
 |
aspx_beginners thread: problem setting selected in dropdownlist
Message #1 by "John Tyson" <jtyson@t...> on Tue, 12 Nov 2002 07:09:24 -0800
|
|
Hi,
I am having difficulty setting the default value of my dropdownlist. In
my aspx page I am getting the value of a selected insurance plan (the
one I am trying to make the selected item in my dropdown) from the first
stored procedure in my sub, and then I call another stored procedure to
get all possible insurance plans to populate my dropdownlist with. The
error I'm getting is:
Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.
Source Error:
Line 119: ddlPayerProd.ClearSelection()
Line 120: ddlPayerProd.Items.FindByText(strPlan).Selected
=3D True
Line 121: End Try
If someone wouldn't mind looking at this code - it is brief - and help
me figure out why I am getting this error I would be greatly
appreciative. It was working before when I had the calls to the stored
procedures in separate subs, but this should work just as easily too. I
did test to make sure strPlan was being assigned a value and that my
calls and stored procedures are working correctly...is this happening
because I am trying to get the values for my dropdown and get the value
for my selected item and set it in the same sub?
Thank you very much.
<script language=3D"VB" runat=3D"server">
Sub Page_Load(s As Object, e As EventArgs)
If User.Identity.IsAuthenticated Then
If Not Page.IsPostBack Then
Dim strPlan As String
Dim dbconn As SqlConnection =3D New SqlConnection _
(ConfigurationSettings.AppSettings("ConnectionString2"))
Dim cmd As SqlCommand =3D New SqlCommand("GET_displayReg",
dbconn)
cmd.CommandType =3D CommandType.StoredProcedure
Dim insPlanParm As SqlParameter =3D
cmd.Parameters.Add("@insPlan",
SqlDbType.NVarChar, 25)
insPlanParm.Direction =3D ParameterDirection.Output
Try
dbconn.Open()
cmd.ExecuteNonQuery()
Catch exc As Exception
Response.Write("An error has occurred: " &
exc.ToString())
Finally
strPlan =3D cmd.Parameters("@insPlan").Value
End Try
With cmd
'.Parameters.Remove(insPlanParm)
.CommandText =3D "GET_planList"
End With
Dim dr As SqlDataReader
Try
dr =3D cmd.ExecuteReader()
ddlPayerProd.DataSource =3D dr
ddlPayerProd.DataBind()
Catch exc As Exception
Response.Write("An error has occurred: " &
exc.ToString())
Finally
If Not dr Is Nothing Then
dr.Close()
End If
ddlPayerProd.ClearSelection()
ddlPayerProd.Items.FindByText(strPlan).Selected =3D True
End Try
End If
Else
Response.Redirect("../login.aspx")
End If
End Sub
</script>
<body>
<form runat=3D"server">
<p>Insurance Plan: <asp:DropDownList id=3D"ddlPayerProd"
DataValueField=3D"InsPlanID" DataTextField=3D"InsPlanName"
onSelectedIndexChanged=3D"PayerProd_Change" runat=3D"server"
AutoPostBack=3D"True" />
</form>
</body>
</html>
Message #2 by "Ken Schaefer" <ken@a...> on Wed, 13 Nov 2002 16:00:48 +1100
|
|
Not sure, but I suspect that the item you are looking for is not being
found, "Nothing" is being returned, and you are trying to set the Selected
property of a "Nothing", which is not a ListItem.
I'm not 100% sure on what the best way to do this is. What I've seen is
this:
<script>
Dim myListItem as ListItem
Dim strPlan as String
strPlan = "myTestPlan"
myListItem = myWebControl.Items.FindByText(strPlan)
If not isNothing(myListItem) then
myListItem.Selected = True
End If
</script>
Cheers
Ken
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
From: "John Tyson" <jtyson@t...>
Subject: [aspx_beginners] problem setting selected in dropdownlist
Hi,
I am having difficulty setting the default value of my dropdownlist. In
my aspx page I am getting the value of a selected insurance plan (the
one I am trying to make the selected item in my dropdown) from the first
stored procedure in my sub, and then I call another stored procedure to
get all possible insurance plans to populate my dropdownlist with. The
error I'm getting is:
Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object.
Source Error:
Line 119: ddlPayerProd.ClearSelection()
Line 120: ddlPayerProd.Items.FindByText(strPlan).Selected
= True
Line 121: End Try
If someone wouldn't mind looking at this code - it is brief - and help
me figure out why I am getting this error I would be greatly
appreciative. It was working before when I had the calls to the stored
procedures in separate subs, but this should work just as easily too. I
did test to make sure strPlan was being assigned a value and that my
calls and stored procedures are working correctly...is this happening
because I am trying to get the values for my dropdown and get the value
for my selected item and set it in the same sub?
Thank you very much.
<script language="VB" runat="server">
Sub Page_Load(s As Object, e As EventArgs)
If User.Identity.IsAuthenticated Then
If Not Page.IsPostBack Then
Dim strPlan As String
Dim dbconn As SqlConnection = New SqlConnection _
(ConfigurationSettings.AppSettings("ConnectionString2"))
Dim cmd As SqlCommand = New SqlCommand("GET_displayReg",
dbconn)
cmd.CommandType = CommandType.StoredProcedure
Dim insPlanParm As SqlParameter
cmd.Parameters.Add("@insPlan",
SqlDbType.NVarChar, 25)
insPlanParm.Direction = ParameterDirection.Output
Try
dbconn.Open()
cmd.ExecuteNonQuery()
Catch exc As Exception
Response.Write("An error has occurred: " &
exc.ToString())
Finally
strPlan = cmd.Parameters("@insPlan").Value
End Try
With cmd
'.Parameters.Remove(insPlanParm)
.CommandText = "GET_planList"
End With
Dim dr As SqlDataReader
Try
dr = cmd.ExecuteReader()
ddlPayerProd.DataSource = dr
ddlPayerProd.DataBind()
Catch exc As Exception
Response.Write("An error has occurred: " &
exc.ToString())
Finally
If Not dr Is Nothing Then
dr.Close()
End If
ddlPayerProd.ClearSelection()
ddlPayerProd.Items.FindByText(strPlan).Selected = True
End Try
End If
Else
Response.Redirect("../login.aspx")
End If
End Sub
</script>
<body>
<form runat="server">
<p>Insurance Plan: <asp:DropDownList id="ddlPayerProd"
DataValueField="InsPlanID" DataTextField="InsPlanName"
onSelectedIndexChanged="PayerProd_Change" runat="server"
AutoPostBack="True" />
</form>
</body>
</html>
|
|
 |