Enable datagrid dynamic data at runtime - asp.net

I have a data grid using dynamic data. I 'enable' dynamic data on the page_init event for the page containing the data grid. I would like to be able to set the type of the dynamic data at run time. I have the name of the class to set, as a string. I can't quite figure out how to do this.
I set the dynamic data like this:
Dim myGrid As GridView = DirectCast(retrieveGrid.FindControl("gridResults"), GridView)
myGrid.EnableDynamicData(GetType(*MyEntityNameAsAString*)
Obviously this does not work because I cannot provide my entity name a s a string. How can I convert the string to the entity type? I tried:
Type.GetType(entityname)
And
Type.GetType(AssemblyName.entityname)
And neither seems to work. That is, I can't get the type with either of these statements.

OK, solved it like this... I created a function to get the entity object from the object name:
Public Function GetEntity(ByVal entityName As String) As Object
'Get the assembly
Dim assem As Assembly = Nothing
assem = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory & "/bin/AsbestosEntities.dll")
'Get all classes in the assembly
Dim AllEntities As Type() = assem.GetTypes()
Return AllEntities.FirstOrDefault(Function(e) e.FullName = entityName)
End Function
Then set the grid enable dynamic data based on the result of the function:
Dim EntityType As Type = GetEntity(general_retrieve.gr_entity_set_name)
myGrid.EnableDynamicData(EntityType)

Related

Convert JSon to dynamic object in VB.Net

I am using VB.Net and calling salesforce API. It returns very ugly JSON which I am not able to deserialize. I have a following code using JSON.Net
Dim objDescription As Object = JsonConvert.DeserializeObject(Of Object)(result)
objDescription contains many properties, one on=f them in fields. But when I write something like objDescription.fields it gives me error.
objDescription.fields Public member 'fields' on type 'JObject' not found. Object
I am not very sure but I think it C# allow to convert any JSON to dynamic object. How can I use it in VB.Net?
You can turn Option Strict Off and use ExpandoObject which is recognized by JSON.NET. In order to leverage the dynamic features you can use a variable of type object.
Option Strict Off
Sub Main
Dim jsonData As Object = JsonConvert.DeserializeObject(Of System.Dynamic.ExpandoObject)("{""Id"":25}")
Dim test As Integer = jsonData.Id
Console.WriteLine(test)
End Sub
If you would like to use JObject because you need some of its features, you can index the JObject instead.
Sub Main
Dim jsonData As Object = JsonConvert.DeserializeObject(Of Object)("{""Id"":25}")
Dim test = jsonData("Id")
Console.WriteLine(test)
End Sub

How to obtain values from list

Hi Guys
Can anyone help me out with this problem i have a list of object that is formatted in the image attached above and i have to get the IDNO, Affected Id and the date values
Since deserialization will return anonymous type(in your case), you have two options: Either deserialize it to a strong type (by defining a type) or else fetch the value using Reflection since types and properties are not known.
In your case you can get the value through reflection like this:-
Dim _data As List(Of Object) = ...
Dim firstObject = _data.FirstOrDefault()
Dim type As Type = firstObject.GetType()
Dim idmoValue = type.GetProperty("IDMO").GetValue(firstObject)
Sample Fiddle.

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.
Currently, I am getting “Object reference not set to an instance of an object” when setting the text box to "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. 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 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.
2) The PhoneTypeName property of the first element of the phonetype list has a Nothing value.
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. 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
I saw that the SQL contained "NULL= 'O'"
I did it like the example?!? However, when I added .ToString to the field being queried, it worked.
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). The value was nothing as he said.

Dynamically Reference an Object Property Using a String

I'm trying to reference a public property from a string. How can this be done in vb.net?
I have the text value of "FirstName" stored in strucParam(i).TxtPropertyName.
This is what I'm currently doing:
Dim tmpValue As String
Dim ucAppName As UserControl_appName = CType(Parent.FindControl(strucParam(i).ParentFindControl), UserControl_appName)
tmpValue = ucAppName.FirstName.Text
How can I use the value in strucParam(i).TxtPropertyName so that I can remove ".FirstName" from my code? Thanks!
This is basically a duplicate of this question, but I'll answer it for you since you're a VB user and probably didn't consider C# in your searches.
Suppose you have an object of any type stored in a variable called objObject, and the name of the property stored in a variable called strPropertyName. You do the following:
tmpValue = objObject.GetType().GetProperty(strPropertyName).GetValue(objObject, Nothing)
As a final note: please, please consider dropping pseudo-Hungarian notation. It's of no value when working with a statically typed language like VB.NET.
Edit:
The FirstName property is in reality a text box. So don't I need to somehow reference .Text in the code?
tmpFirstName = ucAppName.GetType().GetProperty(strucParam(i).PropertyName).GetValue(objAppNav, Nothing)
Try this:
Dim textBox as TextBox
Dim tmpValue as String
textBox = CType(ucAppName.GetType().GetProperty(strucParam(1).PropertyName).GetValue(objAppNav, Nothing), TextBox)
tmpValue = textBox.Text
Basically, you have to cast the value of the property to a TextBox type, then grab the Text property from it.

Find locally declared Procedures and Methods in a webform code behind (GetMethods)

I am trying to get a subset of locally declared methods in a webform code behind using GetMethods but cannot figure out the proper BindingFlags settings (see code below)....and some further specific questions:
1) Is it possible to differentiate between Procedures and Functions?
2) Furthermore, I only want to fetch a certain subset of these....is there some way I can decorate the ones I want with an attribute, and then further filter based on that?
Private Sub LoadFunctions()
Dim thisClass As Type = Me.GetType
For Each method As MethodInfo In thisClass.GetMethods(BindingFlags.Instance Or BindingFlags.Public Or BindingFlags.DeclaredOnly)
If method.Name.Substring(0, 3) = "Get" Then
Me.ddlCodeSamples.Items.Add(method.Name)
End If
Next
End Sub
a) The real source of the problem seemed to be reflecting on the wrong class.....from a webform, you must do:
Dim thisClass As Type = GetType(yourWebFormName)
...not:
Dim thisClass As Type = Me.GetType
b) I think method.ReturnType could be examined to differentiate between a procedure or method.
c) Here is how custom attributes could be used:
More or less working code:
Private Sub LoadFunctions()
'....When run from a webform that you want to reflect on the locally defined functions:
' This is *incorrect*:
' Dim thisClass As Type = Me.GetType
' This is *correct* (I'm not sure why though):
Dim thisClass As Type = GetType(CodeSamples) '<-- "CodeSamples" is the webform name
For Each method As MethodInfo In thisClass.GetMethods(BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.DeclaredOnly)
If Not method.IsSpecialName Then '<-- to exclude property getter/setters, etc
' Rather than filtering on function name, could use custom attributes as discussed here:
' http://www.codeguru.com/vb/gen/vb_general/attributes/article.php/c6073
If method.Name.Substring(0, 3) = "Get" Then
Me.ddlCodeSamples.Items.Add(method.Name)
End If
End If
Next
End Sub

Resources