vb.net sql insert with parameters is not working when data type is int

I have been working on this for a few days now.. discovered most of the problems I created for myself.
I think I have those problems worked out now.

I have an asp.net project with vb.net code behind.. I am taking values from a bunch of controls on the page and updating a sql table with the information... everything  is working until I come to the first value that is an integer.

Here are two examples - one is for a varchar and the other is for the int..
I have verified that the parameter does hold the correct value when it hits
I have also verified that the datatype in the sql db is an int
any help would be great.. thanks

            If txtGLCode.Text = "" Then
                cmd.Parameters.Add("@GL_Code", SqlDbType.VarChar).Value = DBNull.Value
                cmd.Parameters.Add("@GL_Code", SqlDbType.VarChar).Value = txtGLCode.Text
            End If

            If txtSubAccount.Text = "" Then
                cmd.Parameters.Add("@SubAccount", SqlDbType.Int).Value = DBNull.Value
                cmd.Parameters.Add("@SubAccount", SqlDbType.Int).Value = txtSubAccount.Text
            End If

Open in new window

David ModugnoAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fernando SotoRetiredCommented:
The issue is that you are trying to assign a text string to a Integer in the database. First convert it to an Integer then assign it. Also make sure that the column in the database is nullable.
If txtSubAccount.Text = "" Then
        cmd.Parameters.Add("@SubAccount", SqlDbType.Int).Value = DBNull.Value
        cmd.Parameters.Add("@SubAccount", SqlDbType.Int).Value = Integer.Parse(txtSubAccount.Text)
End If

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
You also might want to add a check for isnumeric too so casting as integer won't throw an error
  If txtSubAccount.Text = "" Then
                cmd.Parameters.Add("@SubAccount", SqlDbType.Int).Value = DBNull.Value
            Elseif not isnumeric(txtSubAccount.Text) then
messagebox.show("non-numeric text")
exit sub
                cmd.Parameters.Add("@SubAccount", SqlDbType.Int).Value = cint(txtSubAccount.Text)
            End If
David ModugnoAuthor Commented:
thank you solved my problem perfectly
Fernando SotoRetiredCommented:
Not a problem David, glad I was able to help.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.