Results 1 to 5 of 5

Thread: conversion from string to integer is not valid

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2022
    Posts
    1

    conversion from string to integer is not valid

    Please can u help me out here cos i have been trying and keep getting error when i try to update with try catch saying conversion from string to integer is not valid
    Code:
     Try
    
                    SqlQuery = "Update tb_login set FullName = @FullName, Email = @Email, Gender = @Gender, DoB = @DoB, Username = @Username, Password = @Password, RoleType = @RoleType, Status = @Status, Image = @Image WHERE  ID = '" & txtID.Text & "'"
    
                    cmd = New MySqlCommand(SqlQuery, conn)
                    'cmd.Parameters.AddWithValue("@ID", txtID.Text)
                    cmd.Parameters.AddWithValue("@FullName", txtFullName.Text)
                    cmd.Parameters.AddWithValue("@Email", txtUserEmail.Text)
                    cmd.Parameters.AddWithValue("@Gender", cboGender.Text)
                    cmd.Parameters.AddWithValue("@DoB", dtpUserDOB.Value)
                    cmd.Parameters.AddWithValue("@Username", txtUserName.Text)
                    cmd.Parameters.AddWithValue("@Password", txtConfirmP.Text)
                    cmd.Parameters.AddWithValue("@RoleType", cboRole.Text)
                    cmd.Parameters.AddWithValue("@Status", cboStatus.Text)
                    'cmd.Parameters.AddWithValue("@Image", arrImage)
                    Dim mstream As New System.IO.MemoryStream()
                    pboPic.Image.Save(mstream, pboPic.Image.RawFormat)
                    arrImage = mstream.GetBuffer()
                    Dim FileSize As UInt32
                    FileSize = mstream.Length
                    Dim para As New MySqlParameter("@Image", SqlDbType.Image)
                    para.Value = arrImage
                    cmd.Parameters.Add(para)
    
                    Dim r As Integer
                    r = cmd.ExecuteNonQuery()
                    If r > 1 Then
                        MsgBox("Records of " & txtFullName.Text & " UPDATED Successfully", MsgBoxStyle.Information)
    
                        txtFullName.Clear()
                        txtUserEmail.Clear()
                        cboGender.Text = String.Empty
                        dtpUserDOB.ResetText()
                        txtUserName.Clear()
                        txtPassword.Clear()
                        txtConfirmP.Clear()
                        cboRole.Text = String.Empty
                        cboStatus.Text = String.Empty
                        pboPic.Image = Nothing
                    Else
                        MsgBox("Records of " & txtFullName.Text & " is not UPDATED", MsgBoxStyle.Information)
                    End If
                    cmd.Parameters.Clear()
    
                Catch ex As Exception
                    MsgBox(ex.Message, "Unable to Save", vbExclamation)
                End Try
    Last edited by Shaggy Hiker; Oct 6th, 2022 at 09:16 AM. Reason: Added CODE tags.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: conversion from string to integer is not valid

    Please don't post unformatted code snippets. They are much harder to read, especially as they get longer.
    Code:
                Try
    
                    SqlQuery = "Update tb_login set FullName = @FullName, Email = @Email, Gender = @Gender, DoB = @DoB, Username = @Username, Password = @Password, RoleType = @RoleType, Status = @Status, Image = @Image WHERE  ID = '" & txtID.Text & "'"
    
                    cmd = New MySqlCommand(SqlQuery, conn)
                    'cmd.Parameters.AddWithValue("@ID", txtID.Text)
                    cmd.Parameters.AddWithValue("@FullName", txtFullName.Text)
                    cmd.Parameters.AddWithValue("@Email", txtUserEmail.Text)
                    cmd.Parameters.AddWithValue("@Gender", cboGender.Text)
                    cmd.Parameters.AddWithValue("@DoB", dtpUserDOB.Value)
                    cmd.Parameters.AddWithValue("@Username", txtUserName.Text)
                    cmd.Parameters.AddWithValue("@Password", txtConfirmP.Text)
                    cmd.Parameters.AddWithValue("@RoleType", cboRole.Text)
                    cmd.Parameters.AddWithValue("@Status", cboStatus.Text)
                    'cmd.Parameters.AddWithValue("@Image", arrImage)
                    Dim mstream As New System.IO.MemoryStream()
                    pboPic.Image.Save(mstream, pboPic.Image.RawFormat)
                    arrImage = mstream.GetBuffer()
                    Dim FileSize As UInt32
                    FileSize = mstream.Length
                    Dim para As New MySqlParameter("@Image", SqlDbType.Image)
                    para.Value = arrImage
                    cmd.Parameters.Add(para)
    
                    Dim r As Integer
                    r = cmd.ExecuteNonQuery()
                    If r > 1 Then
                        MsgBox("Records of " & txtFullName.Text & " UPDATED Successfully", MsgBoxStyle.Information)
    
                        txtFullName.Clear()
                        txtUserEmail.Clear()
                        cboGender.Text = String.Empty
                        dtpUserDOB.ResetText()
                        txtUserName.Clear()
                        txtPassword.Clear()
                        txtConfirmP.Clear()
                        cboRole.Text = String.Empty
                        cboStatus.Text = String.Empty
                        pboPic.Image = Nothing
                    Else
                        MsgBox("Records of " & txtFullName.Text & " is not UPDATED", MsgBoxStyle.Information)
                    End If
                    cmd.Parameters.Clear()
    
                Catch ex As Exception
                    MsgBox(ex.Message, "Unable to Save", vbExclamation)
                End Try
    Also, please don't leave wads of leading whitespace on your code. You can hold down the Alt key as you select in VS to select an arbitrary block of code, trimming the unnecessary leading whitespace and keeping only what's required for indenting.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: conversion from string to integer is not valid

    As for the issue, where is the exception thrown? You need to provide ALL the relevant information and where an exception is thrown is obviously relevant. We shouldn't have to guess or work out stuff that you already know. Also, what string is invalid? You should know and be able to tell us. If you don't know then it means that you haven't actually made any attempt to solve the issue yourself before posting here. You need to do what you can for yourself first and looking to see what data is invalid is the least of that. If you still can't work out the problem, at least you can give us all the relevant information.

  4. #4
    Fanatic Member
    Join Date
    Aug 2004
    Location
    Essex, UK
    Posts
    750

    Re: conversion from string to integer is not valid

    Start by taking out the Try - Catch - End Try block so at least you will see which line is causing the error.

  5. #5
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    4,415

    Re: conversion from string to integer is not valid

    *sigh*
    Code:
    SqlQuery = "Update tb_login set FullName = @FullName, Email = @Email,  Gender = @Gender, DoB = @DoB, Username = @Username, Password =  @Password, RoleType = @RoleType, Status = @Status, Image = @Image 
    WHERE   ID = '" & txtID.Text & "'"
    
    cmd = New MySqlCommand(SqlQuery, conn)
    'This one is commented out. Why?
     'cmd.Parameters.AddWithValue("@ID", txtID.Text)
    Is ID in your DB-Table a String or Integer?
    is txtID.text empty or has it a valid Value?
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width