Bind value to textbox? Should be easy, right?

Thread is closed for posting
2 posts, 0 answers
  1. Ted
    Ted avatar
    5 posts
    Member since:
    May 2013

    Posted 26 Jun 2013 Link to this post


    Had to add "ToString" where indicated below.

    ... = dbContext.User_PhoneTypes.Where(Function(c) c.PhoneTypeCode.**ToString** = "O")

    ---------------- Original -------------------------
    Radgrid turned out to be easy. Thought textbox would be the easiest. I cannot find a complete example. Found tons on grid and droplist but not textbox. This test is to lookup a “PhoneTypeName” from a UserPhoneType table with TypeCode = “0” and assign that first value to a textbox.

    Currently, I am getting “Object reference not set to an instance of an object” when setting the text box to "phonetype.FirstOrDefault.PhoneTypeName.ToString"

    NOTE: I DID successfully bind the entire list of PhoneTypes to a droplist confirm the data is accessible. It must be the way I am going about querying the table for a single record here.

    Using dbContext As New EntitiesModel()
         Dim phonetype As IEnumerable(Of User_PhoneType) = dbContext.User_PhoneTypes.Where(Function(c) c.PhoneTypeCode = "O")
         mytextbox.Text = phonetype.FirstOrDefault.PhoneTypeName.ToString
    End Using
  2. Doroteya
    Doroteya avatar
    498 posts

    Posted 01 Jul 2013 Link to this post

    Hello Ted,

    I am glad that currently the issue is fixed on your side.

    Let me just add that the NullReferenceException you were experiencing was due to the fact that the mytextbox.Text property was taking its value from a property of an object equal to NULL. The reason for that is that the FirstOrDefault() extension method returns the default value for the source type, if the collection it is executed on is empty. And since User_PhoneTypes is a referential type, its default value is NULL.

    In other words, if User_PhoneTypes does not contain objects that match the condition specified in the Where() method, the phonetype collection will be empty and the FirstOrDefault() method will return an object of type User_PhoneTypes which value will be NULL. Later when you try to access the PhoneTypeName property of that object the NullReferenceException will be thrown.

    Since you have already found one of the possible solutions in this situation, let me just suggest to you another one. You can check in the domain model the datatype of PhoneTypeCode and supply the comparison value according to it in the Where() method. For example, if PhoneTypeCode is Integer, you can compose the statement like this:
    dbContext.User_PhoneTypes.Where(Function(c) c.PhoneTypeCode = 0)

    I hope you find this feasible. If you have other questions or experience difficulties, do not hesitate to get back to us.

    OpenAccess ORM Q2 2013 brings you a more powerful code generation and a unique Bulk Operations support with LINQ syntax. Check out the list of new functionality and improvements shipped with this release.
Back to Top