Dynamically Reference an Object Property Using a String - asp.net

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.

Related

Retrieving a Date value from a GridView in VB

I am trying to retrieve a value from a GridView and save it as a Date variable, using the following code:
Dim invoice_date As Date = Convert.ToDateTime((TryCast(row.Cells(2).Controls(0), TextBox)).Text)
I am getting an error:
System.Web.UI.WebControls.TextBox) returned Nothing.
From what I have read, if the TryCast fails, it will return a value of nothing.
I have a similar command for an Integer, which works fine on another column.
Dim order_value As Integer = Convert.ToInt32((TryCast(row.Cells(3).Controls(0), TextBox)).Text)
Can somebody please help with the correct syntax to work with a Date value?
You are right that TryCast returns Nothing if it cannot cast to the target type. So, the problem that you have is most likely that row.Cells(2).Controls(0) is not a TextBox. To debug this, split the line:
Dim invoice_date As Date = Convert.ToDateTime((TryCast(row.Cells(2).Controls(0), TextBox)).Text)
like this:
Dim textbox As TextBox = TryCast(row.Cells(2).Controls(0), TextBox)
Dim invoice_date As Date = Convert.ToDateTime(textbox.Text)
and look at the value of textbox.
Try and use DirectCast() and see if that works.

Convert from string to textbox object

I am trying to convert a string to textbox, but I am getting an error that it cannot convert to integer?!
I have the following code:
Dim curr As String
curr = "Detail_0107"
Dim NEWTEXT As TextBox = TryCast(Me.Controls(curr), TextBox)
NEWTEXT.Text = "test"
On the TryCast, I get the following error:
Conversion from string "Detail_0107" to type 'Integer' is not valid
Detail_0107 is a textbox on my form. Can I do this?
Thanks
Your problem seems to be that you're setting Detail_0107 as a string. If you want to set the text of Detail_0107, all you need to do is the following:
Detail_0107.Text = "test"
As you said Detail_0107 is already a textbox on your form, there shoud already be an object for it.
Try using Me.Controls.Find(curr) instead. Additionally, every control has a .Text property. It's part of the base Control type, and therefore you have no need to cast to TextBox. If you're very sure the Detail_0107 control does exist in that collection, you can get the code down to just this:
Me.Controls.Find("Detail_0107").Text = "test"

Enable datagrid dynamic data at runtime

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)

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.

How to use findcontrol in createuserwizard1 complete step ...?

Is this the right declaration of findcontrol for complete step of createuserwizard1 ?
Dim UserName As TextBox = CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Label11")
But when i use it its shows the error object expected !
What was the problem ?
You can't set the value of a Textbox to a generic control (especially one that is really a label). Plus you forgot the New keyword when initializing UserName. Depending on exactly what you're trying to do, you may want to try the following...
'This will set the textbox's Text to the label's text.
Dim UserName As New Textbox
UserName.Text = CType(CreateUserWizard1.CreateUserStep.ContentTemplateContainer.FindControl("Label11"), Label).Text
Or
'This will cast the label to a new label control you define in code.
Dim UserName as New Label
UserName = CType(CreateUserWizard2.CreateUserStep.ContentTemplateContainer.FindControl("Label11"), Label)

Resources