I have a range of textboxes and dropdown lists on my page. On the load event I am populating to these controls with a record from a table in the database.
I have a field called AgeRange. I want to display the value in the field as the selected item of a dropdownlist. Then they can change if required.
The datasource for the dropdown list is a list of AgeRanges from another table.
the error occurs if the value in the agerange field of the main record is NULL or empty
I have a field called AgeRange. I want to display the value in the field as the selected item of a dropdownlist. Then they can change if required.
The datasource for the dropdown list is a list of AgeRanges from another table.
the error occurs if the value in the agerange field of the main record is NULL or empty
Code:
Protected Sub PopulatePersonalDetails()
Dim strConnString As String = ConfigurationManager.ConnectionStrings("HostLinkConnectionString").ConnectionString
Dim con As New SqlConnection(strConnString)
Dim cmd As New SqlCommand()
Dim reader As SqlDataReader
Dim sqlDa As New SqlDataAdapter(cmd)
Dim dt As New DataTable()
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "spGetPersonalDetails"
cmd.Parameters.Add("@HostID", SqlDbType.Int).Value = Session("HostID")
cmd.Connection = con
con.Open()
sqlDa.Fill(dt)
If dt.Rows.Count > 0 Then
txtFirstname.Text = dt.Rows(0)("Firstname").ToString() 'Where ColumnName is the Field from the DB that you want to display
txtSurname.Text = dt.Rows(0)("Surname").ToString()
ddlAgeRange.SelectedValue = dt.Rows(0)("AgeRange").ToString()
ddlGender.SelectedValue = dt.Rows(0)("Gender").ToString()
txtOccupation.Text = dt.Rows(0)("Occupation").ToString()
ddlNationalities.SelectedValue = dt.Rows(0)("Nationality").ToString()
ddlReligions.SelectedValue = dt.Rows(0)("Religion").ToString()
ddlEthnicity.SelectedValue = dt.Rows(0)("Ethnicity").ToString()
End If
reader = cmd.ExecuteReader
con.Close()
End Sub
Code:
<asp:DropDownList ID="ddlAgeRange" runat="server" DataSourceID="GetAgeRanges"
DataTextField="AgeRange" DataValueField="AgeRange" AppendDataBoundItems="True">
<asp:ListItem Value="" >Please Select</asp:ListItem>
</asp:DropDownList>