文本框

时间:2016-06-17 15:28:41

标签: vb.net drop-down-menu textbox nullreferenceexception

我有代码,当选择第一个下拉列表中的项目时,会填充两个下拉列表和一个文本框。我在第一个下拉列表的开头添加了一个空白项目。没有空白项,代码工作正常。但是,对于空白项目,会填充ddl,但文本框除外,这会给我一个 NullReferenceException 错误。

错误点在:

TextBox1.Text = Convert.ToString(DropDownList2.SelectedItem.Value)

我无法在这里查明问题。

这是我的代码:

 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

0 个答案:

没有答案