This is a migrated thread and some comments may be shown as answers.

Bind value to textbox? Should be easy, right?

1 Answer 206 Views
Getting Started
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Ted
Top achievements
Rank 1
Ted asked on 27 Jun 2013, 01:03 AM
UPDATE: FIXED

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 asp.net 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 control...to 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

1 Answer, 1 is accepted

Sort by
0
Doroteya
Telerik team
answered on 01 Jul 2013, 08:53 AM
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.


Regards,
Doroteya
Telerik
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.
Tags
Getting Started
Asked by
Ted
Top achievements
Rank 1
Answers by
Doroteya
Telerik team
Share this question
or