Converting string to integer VB.NET

Converting string to integer VB.NET

You can convert String to int by doing like this:

  1. For C#: int val = Int32.Parse(12);
  2. For VB.NET: Dim val as Integer = CInt(12)

Here you can find all Type Conversion Functions (Visual Basic).

Here you have How to: Convert a String to a Number (C# Programming Guide).

You can convert string from a TextBox to an integer using:

Dim myInt as Integer = CInt(Textbox1.Text)

Otherwise, if you are asking how to ensure that an integer is appended into an SQL file or Access file using VB.NET, then thats a different question.

Converting string to integer VB.NET

I solved it:

Dim productID As Integer

If (Integer.TryParse(txtID.Text, productID)) Then

    cmd.Parameters.Add(New OleDbParameter(@ProductID, productID))
Else
    Throw New ArgumentException(Entered ProductID is not a valid integer.)
End If


cmd.Parameters.Add(New OleDbParameter(@Product, txtProd.Text))

Dim price As Double
If (Double.TryParse(txtPrice.Text, price)) Then

    cmd.Parameters.Add(New OleDbParameter(@UnitPrice, txtPrice.Text))
Else
    Throw New ArgumentException(Invalid.)
End If

cmd.Parameters.Add(New OleDbParameter(@CategoryName, txtCategory.Text))

Dim id As Integer
If (Integer.TryParse(txtCatID.Text, id)) Then
    cmd.Parameters.Add(New OleDbParameter(@CategoryID, id))
Else
    Throw New ArgumentException(Entered Category ID is Invalid.)
End If

Leave a Reply

Your email address will not be published.