Convert from string to textbox object - asp.net

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"

Related

How to add message box in vb?

How to add message box using vb?
I get an error below when using MsgBox()
VB doesn't automatically assign a string type so maybe there's the problem...
Try this:
Dim msg$ = "text..."
The above is a short version of:
Dim msg As String = "text..."
If you disable the Option Explicit compile option, then you can just write:
msg$ = "text..."

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.

Referencing a label in my asp.net page from code behind

Using VS 2013 VB.
I have the following line of code
Dim myLabel As Label = CType(Me.Controls("lbladd"), Label)
Whenever I run the page I get the following error
Conversion from string to type integer is not valid
I have several labels on my asp.net page each with a number at the end of the id which increases by one. My eventual aim is to loop through each label and add a string to each one using something like the following
For i = 0 To splitAddress.Count - 1
Dim myLabel As Label = CType(Me.Controls("lbladdress" & i + 1), Label)
myLabel.Text = splitAddress(i)
Next
Where splitaddress is a list of strings.
I just don't know why its throwing the error and mentioning an integer.
Me.Controls is of type ControlCollection and it is expecting a parameter of type integer, but you are providing a parameter of type string.
To find a control on the page you can use a method FindControl of class Page. You can see the info in MSDN.
You can update your code to use this method:
Dim myLabel As Label = CType(Me.FindControl("lbladdress" & (i + 1).ToString()), Label)

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.

Resources