none
VB coding issue when updating RRS feed

  • Question

  • Hi Iam getting the following error on my vb code "Failed to convert parameter value from a String to a Int32." Please help

    My code is below

    Dim cmd As New SqlClient.SqlCommand()
    
            cmd.CommandType = CommandType.StoredProcedure
    
            'cmd.CommandText = "SMLineUpdate"
            cmd.CommandText = "SMLineUpdate_New"
            'cmd.CommandText = "SMLineUpdate3"
    
            cmd.Parameters.Add("@Id", SqlDbType.Int).Value = TextBox3.Text
    
            cmd.Parameters.Add("@Payroll_Id", SqlDbType.Int).Value = Txt1.Text.Trim()
    
            cmd.Parameters.Add("@ProductCode", SqlDbType.NVarChar).Value = Cmb3.Text.Trim()
    
            cmd.Parameters.Add("@Description", SqlDbType.NVarChar).Value = Des3.Text.Trim()
    
            cmd.Parameters.Add("@Qty", SqlDbType.NVarChar).Value = Qty3.Text.Trim()
    
    
    
    
    
            cmd.Connection = cn
    
            ' Try
    
            cn.Open()
    
            cmd.ExecuteNonQuery()
    
            ' lblMessage.Text = "Record inserted successfully"
    
            ' Catch ex As Exception
    
            'Throw ex
    
            ' Finally
    
            cn.Close()
    
            cn.Dispose()

    Monday, April 1, 2019 1:13 PM

Answers

  • A sign when getting

    Failed to convert parameter value from a String to a Int32.

    Means the value can not be converted to an Integer. You should

    1. Check for an empty string
    2. Check if the TextBox.Text value can be converted to a integer.
    Dim value As Integer = 0
    If Not String.IsNullOrWhiteSpace(TextBox3.Text) Then
        If Integer.TryParse(TextBox3.Text, value) Then
            ' valid integer, continue on with the value for "value"
        Else
            ' invalid integer
        End If
    Else
        ' no value given
    End If


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    • Marked as answer by randy1978 Monday, April 1, 2019 1:44 PM
    Monday, April 1, 2019 1:31 PM
    Moderator