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

Dealing with converting empty string value to Integer

2 Answers 467 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ida
Top achievements
Rank 1
Ida asked on 22 Oct 2013, 01:25 PM
Hi,

With the textboxes in a RadGrid, it is possible to deal with an empty string value, that needs to be stored to an Integer value?

I am assigning a texbox to a Integer variable, though that textbox can be blank:

Dim

 

 

CodEnd As Integer? = TryCast(edit("CodEnd").Controls(0), TextBox).Text

The variable CodEnd will then be used as parameter to pass into an UPDATE SQL query.

I tried setting ConvertEmptyStringToNull="True" in the textbox, though I still got the following error:

Conversion from string "" to type 'Integer' is not valid

Any advice please?

Thank you, Ida

 

2 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 23 Oct 2013, 05:55 AM
Hi Ida,

I guess you want to store a string as Int,and convert the empty string to 0.Please try the following.

VB:
Protected Sub RadGrid1_UpdateCommand(sender As Object, e As GridCommandEventArgs)
    If TypeOf e.Item Is GridEditableItem Then
        Dim edit As GridEditableItem = DirectCast(e.Item, GridEditableItem)
        Dim id As String = TryCast(edit("CodEnd").Controls(0), TextBox).Text
        If id = String.Empty Then
            id = Nothing
        End If
            'Value converted Int
        Dim value As Integer = Convert.ToInt32(id)
    End If
End Sub

Hope this helps,let me know if any concern.
Thanks,
Shinu
0
Ida
Top achievements
Rank 1
answered on 23 Oct 2013, 03:35 PM
Thank you Shinu.
Tags
Grid
Asked by
Ida
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Ida
Top achievements
Rank 1
Share this question
or