-1

I am trying to fill a text box with an ID of O1IDText when the selected value of a drop down list is changed. I get an error "Object reference not set to an instance of an object".

Here is my code in ASP of the drop down list:

<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>

Below is the code of my Textbox wihtin ASP:

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

And Finally my code behind in 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

The exception "Object reference not set to an instance of an object" happens during O1IDText.Text = dr("ID").ToString.

Dijkgraaf
  • 11,049
  • 17
  • 42
  • 54
DannyBoy
  • 93
  • 7
  • given error means that dr("ID").ToString doesn't exist. Make sure conenction is open properly and your command text is correct – Claudius Apr 14 '16 at 18:50
  • you might also want to check if O1IDText is not nothing. FindControl does not search recursively. You could use something like this: http://stackoverflow.com/questions/4955769/better-way-to-find-control-in-asp-net – schudel Apr 14 '16 at 19:58

1 Answers1

0

Assuming that the DropDownList and the TextBox are both in the same row or item, you can get the TextBox this way:

Dim O1DText As TextBox = CType(ddList.NamingContainer.FindControl("O1DText"), TextBox)
ConnorsFan
  • 70,558
  • 13
  • 122
  • 146
  • If they are in separate containers, my suggestion cannot work. Do you know the row or item containing `O1DText`? And by the way, what kind of controls are we talking about? GridViews, ListViews, or two different kind of controls? – ConnorsFan Apr 15 '16 at 12:51
  • They are not in the same row. They are actually in two separate tables. I used your code and it actually works. I think because with how you declared O1IDText is was not null anymore so when it got down further in code it was not null anymore. and it did the calculation that was causing the exception before. Is there a better way of declaring so that it is not null? Thanks – DannyBoy Apr 15 '16 at 12:52
  • Which field do you show in the DropDownList? The `WHERE` clause of the query is incorrect but I don't know what condition you want to use to retrieve the ID. – ConnorsFan Apr 15 '16 at 13:14
  • Another note: I would recommend using a parameterized query (http://forums.asp.net/t/1568268.aspx?SQL+Injection+And+Parameterized+Queries) instead of passing the parameter in plain text. – ConnorsFan Apr 15 '16 at 13:30
  • The Where clause is working just fine the way it is. I have everything working now. I will look into the parameterized query you mentioned. My error message from OP all stemmed from how I originally declared O1IDText. What you gave me works but I believe there may be an even more correct way to declare O1IDText. Thanks alot for all your help!! – DannyBoy Apr 15 '16 at 14:05
  • OK, I see. The `WHERE` clause is based on a string concatenation of multiple fields. Sorry for the misunderstanding. If you think that this answer actually solved your problem, you can mark it as accepted (with the check mark). – ConnorsFan Apr 15 '16 at 14:09