简体   繁体   English

使用Telerik OpenAccess后面的代码中的文本框的简单绑定值

[英]Simple bind value to textbox in code behind using Telerik OpenAccess

I cannot find a complete example. 我找不到一个完整的例子。 Found tons on grid and combobox, 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. 此测试是从具有TypeCode =“0”的UserPhoneType表中查找“PhoneTypeName”,并将该第一个值分配给asp.net文本框。

Currently, I am getting “Object reference not set to an instance of an object” when setting the text box to "phonetype.FirstOrDefault.PhoneTypeName.ToString" 目前,当我将文本框设置为“phonetype.FirstOrDefault.PhoneTypeName.ToString”时,我得到“对象引用未设置为对象的实例”

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

----EDIT---- - - 编辑 - -

I changed as suggested. 我改变了建议。 I ALSO successfully bound the entire list of PhoneTypes to a droplist control...to confirm the data is accessible. 我还成功地将整个PhoneTypes列表绑定到一个droplist控件...以确认数据是否可访问。 It must be the way I am going about querying the table for a single record here. 它必须是我在这里查询表中的单个记录的方式。

I get the same error, but at "Dim type = phonetype.First..." The record is in the table, but it does not appear to be extracted with my code. 我得到了相同的错误,但在“Dim type = phonetype.First ...”记录在表中,但它似乎没有用我的代码提取。

Dim phonetype As IEnumerable(Of User_PhoneType) = dbContext1.User_PhoneTypes.Where(Function(c) c.PhoneTypeCode = "M")
Dim type = phonetype.FirstOrDefault
If Object.ReferenceEquals(type, Nothing) = False And Object.ReferenceEquals(type.PhoneTypeName, Nothing) = False Then
   mytextbox.Text = type.PhoneTypeName.ToString
End If

In general there are the following two possible reasons for getting this exception: 通常,获得此异常有以下两种可能的原因:

1) The phonetype list is empty and the FirstOrDefault method is returning a Nothing value. 1) phonetype列表为空, FirstOrDefault方法返回Nothing值。

2) The PhoneTypeName property of the first element of the phonetype list has a Nothing value. 2) phonetype列表的第一个元素的PhoneTypeName属性具有Nothing值。

In order to make sure that you will not get the Object reference not set to an instance of an object exception I suggest you add a check for Nothing before setting the TextBox value. 为了确保您不会将Object引用设置为对象异常的实例,我建议您在设置TextBox值之前添加Nothing检查。 It could be similar to the one below: 它可能类似于下面的那个:

Dim type = phonetype.FirstOrDefault
If Object.ReferenceEquals(type, Nothing) = False And Object.ReferenceEquals(type.PhoneTypeName, Nothing) = False Then
    mytextbox.Text = type.PhoneTypeName.ToString
End If

Fixed it. 修复。

I was able to view the SQL string being generated by using this: mytextbox.text = phonetype.tostring 我能够使用以下方法查看生成的SQL字符串:mytextbox.text = phonetype.tostring

I saw that the SQL contained "NULL= 'O'" 我看到SQL包含“NULL ='O'”

I did it like the example?!? 我做的就像这样的例子?!? However, when I added .ToString to the field being queried, it worked. 但是,当我将.ToString添加到被查询的字段时,它起作用了。

So the final looks like this: 所以决赛看起来像这样:

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

BTW, Dimitar point to check for null first is good advice (+1). BTW,Dimitar指向首先检查null是好建议(+1)。 The value was nothing as he said. 他所说的价值并不算什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM