I have code that when an item from the first dropdownlist is selected, two dropdownlists and a textbox is populated. I added a blank item at the beginning of the first dropdown. Without the blank item, the code works fine. However, with the blank item, the ddls are populated, except the textbox, which gives me a NullReferenceException
error.
The error points at:
TextBox1.Text = Convert.ToString(DropDownList2.SelectedItem.Value)
I can't pinpoint the problem here.
Here is my code:
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration
Imports System.Web.UI.WebControls
Partial Class companydropdown
Inherits System.Web.UI.Page
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page here
DropDownList1.Items.Add("")
End Sub
Private Sub BindDropDownList(DropdownList1 As DropDownList, query As String, text As String, value As String, defaultText As String)
If Not IsPostBack Then
' Read sql server connection string from web.config file
Dim sConstr As String = ConfigurationManager.ConnectionStrings("ds17701ConnectionString").ConnectionString
Dim Conn As New SqlConnection(sConstr)
Dim cmd As SqlCommand = New SqlCommand("sspCompanyReportList", Conn)
Dim dt As New DataSet()
'Set up Connection object and Connection String for a SQL Client
Using Conn
Conn.Open()
Dim comm As New SqlCommand("SELECT CompanyName FROM CompanyList ORDER BY CompanyName", Conn)
cmd.CommandType = CommandType.StoredProcedure 'Setup Command Type
cmd.CommandText = "sspCompanyReportList" ' Stored Procedure to Call
cmd.CommandType = CommandType.StoredProcedure
Dim da As New SqlDataAdapter(comm)
da.Fill(dt)
End Using
DropdownList1.DataSource = dt
DropdownList1.DataTextField = "CompanyName"
DropdownList1.DataValueField = "CompanyID"
DropdownList1.DataBind()
End If
End Sub
Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)
DropDownList2.Enabled = False
DropDownList3.Enabled = False
If DropDownList1.SelectedIndex > 0 Then
Dim query As String = String.Format("CompanyName", DropDownList1.SelectedIndex)
DropDownList3.Enabled = True
DropDownList2.Enabled = True
DropDownList1.Enabled = True
TextBox1.Enabled = True
Else
DropDownList1.Items.Clear()
End If
End Sub
Protected Sub DropdownList2_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
If DropDownList1.SelectedIndex > 0 Then
Dim sConstr As String = ConfigurationManager.ConnectionStrings("ds17701ConnectionString").ConnectionString
Dim Conn As New SqlConnection(sConstr)
Dim dt As New DataSet()
Dim valsql As String = ""
valsql = "SELECT [CompanyID], [CompanyName], [MemberTypeID], [MembershipStatus], [GroupID] FROM CompanyList WHERE COMPANYID = " & DropDownList1.SelectedValue
Using Conn
Conn.Open()
Dim comm As New SqlCommand(valsql, Conn)
Dim da As New SqlDataAdapter(comm)
da.Fill(dt)
End Using
'DropDownList1.Items.Add(New ListItem(""))
DropDownList2.DataTextField = "MemberTypeID"
DropDownList2.DataValueField = "MembershipStatus"
DropDownList3.DataTextField = "GroupID"
DropDownList3.DataValueField = "GroupID"
TextBox1.Text = Convert.ToString(DropDownList2.SelectedItem.Value)
'Bind sql server data into the Dropdown List
DropDownList2.DataBind()
DropDownList3.DataBind()
TextBox1.DataBind()
Else
DropDownList2.Items.Clear()
DropDownList3.Items.Clear()
TextBox1.Text.ToString()
End If
End Sub
'Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
'End Sub
End Class