使用数据库

时间:2016-04-14 17:26:34

标签: asp.net vb.net

当我更改下拉列表的选定值时,我正在尝试填充ID为O1IDText的文本框。我收到错误"对象引用没有设置为对象的实例"。

以下是我在ASP下拉列表中的代码:

<EditItemTemplate>
    <asp:DropDownList ID="DropDownList1" AutoPostBack="true" runat="server" DataSourceID="SqlDataSource2" AppendDataBoundItems="true" OnDataBinding="DropDownlist1_DataBinding1" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" DataTextField="FullName" DataValueField="FullName" SelectedValue='<%# Bind("Official1") %>'>
    </asp:DropDownList>
</EditItemTemplate>

以下是我Textbox wihtin ASP的代码:

<EditItemTemplate>
    <asp:TextBox ID="O1IDText" runat="server" Text='<%# Bind("O1ID") %>'></asp:TextBox>
</EditItemTemplate>

最后我的代码落后于VB:

Protected Sub DropDownList1_SelectedIndexChanged(sender As Object, e As EventArgs)


    Dim ddList As DropDownList = CType(sender, DropDownList)
    RemoveHandler ddList.DataBinding, AddressOf DropDownlist1_DataBinding1
    Dim O1IDText As TextBox = TryCast(FindControl("O1IDText"), TextBox)


    Dim cmd As SqlCommand = con.CreateCommand()
    cmd.CommandType = System.Data.CommandType.Text
    cmd.CommandText = "Select ID from Official where [First Name] + ' ' + [Last Name]+ ' ' +[Email]+ ' ' +[Phone] = '" + ddList.SelectedValue + "'"
    Dim dr As SqlDataReader


    Try
        con.Open()
        dr = cmd.ExecuteReader()
        dr.Read()
        O1IDText.Text = dr("ID").ToString

    Catch ex As Exception
        con.Close()
    End Try

异常&#34;对象引用未设置为对象的实例&#34;在O1IDText.Text = dr("ID").ToString期间发生。

1 个答案:

答案 0 :(得分:0)

假设DropDownList和TextBox都在同一行或项目中,您可以这样获取TextBox:

Dim O1DText As TextBox = CType(ddList.NamingContainer.FindControl("O1DText"), TextBox)