I have a dropdownlist that I am trying to preselect the value of based on a value returned from a stored proc in the code behind page. I've tried a few things but nothing is working. I am populating the dropdownlist from a datasource in the aspx page and then trying to select the appropriate value in the Page_Load according to a value from a stored proc.
--------------------------
----------
-------
'error message which occurs in line 54
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.NullReferenceExcept
ion: Object reference not set to an instance of an object.
Source Error:
Line 52: email.Text = emailTxt
Line 53: phone.Text = phoneTxt
Line 54: statusId.Items.FindByText(
statusTxt)
.Selected = True
Line 55: comments.Text = commentsTxt
Line 56:
--------------------------
----------
----------
----------
------
'html code for dropdownlist
<asp:DropDownList ID="statusId" runat="server" DataSourceID="SqlDataSourc
e1" DataTextField="status" DataValueField="statusID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:shcbaria
tricsConne
ctionStrin
g %>" SelectCommand="procLeadSta
tus" SelectCommandType="StoredP
rocedure">
</asp:SqlD
ataSource>
--------------------------
----------
----------
----------
----------
-
'relevant code-behind
If Not (IsPostBack) Then
Dim datetimeTxt As String = ""
Dim sourceTxt As String = ""
Dim referrerTxt As String = ""
Dim firstnameTxt As String = ""
Dim lastnameTxt As String = ""
Dim emailTxt As String = ""
Dim phoneTxt As String = ""
Dim statusTxt As String = ""
Dim commentsTxt As String = ""
Dim leadId As Integer = Request.QueryString("eid")
Dim objConn As SqlConnection
Dim dataReader As SqlDataReader
Dim strSql As String
Dim ObjCmd As SqlCommand
objConn = New SqlConnection(System.Confi
guration.C
onfigurati
onManager.
Connection
Strings("s
hcbariatri
csConnecti
onString")
.Connectio
nString)
strSql = "EXECUTE procLeadDetails '" & leadId & "'"
ObjCmd = New SqlCommand(strSql, objConn)
objConn.Open()
dataReader = ObjCmd.ExecuteReader
If dataReader.Read() Then
datetimeTxt = dataReader("dateof")
sourceTxt = dataReader("sourceDescript
ion")
referrerTxt = dataReader("referrer")
firstnameTxt = dataReader("firstname")
lastnameTxt = dataReader("lastname")
emailTxt = dataReader("email")
phoneTxt = dataReader("phone1")
statusTxt = dataReader("status")
commentsTxt = dataReader("dateof")
End If
datetime.Text = datetimeTxt
source.Text = sourceTxt
referrer.Text = referrerTxt
firstname.Text = firstnameTxt
lastname.Text = lastnameTxt
email.Text = emailTxt
phone.Text = phoneTxt
statusId.Items.FindByText(
statusTxt)
.Selected = True
comments.Text = commentsTxt
End If
Start Free Trial