VB.net

Hi

In VB.net I need to make sure that a number being read off an Excel spreadsheet will be an integer (int) in my SQL database.

Is the following line in the code below right for this:
    xPresent = Convert.ToInt16(reader("Present"))
If Not reader("Present").Equals(DBNull.Value) Then
                    'xPresent = Convert.ToString(reader("Present"))
                    xPresent = Convert.ToInt16(reader("Present"))
                    '** Validation
                    If xPresent <> "1" Then
                        blnLineError = True
                        blnMasterError = True
                        sErrorDescription = sErrorDescription & vbCrLf & "# Line " & CStr(oExcelLine) & " Present: '" & xPresent & "' can only be a 1 or nothing"
                    End If
                End If

Open in new window

Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAsked:
Who is Participating?
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.

Luis PérezSoftware Architect in .NetCommented:
You must use ToInt32. Int16 is a small integer, which in sql server matches with the tintyint datatype, not to the int type.

Hope that helps.
0

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
RainverseCommented:
Yes.  You can use shorthand as well: CInt(reader("Present"))

-MJC
0
RainverseCommented:
Gunslinger: I've used Int16 with Int many times.  As long as it doesn't go over the limit it never gives me any issue.  Is that bad practice?

-MJC
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Luis PérezSoftware Architect in .NetCommented:
Of course, any value stored in a Int16 field will store without problems into an int SQL field, because it is bigger. But the program logic says that if in your database you use a Int datatype, it's because you need to store values bigger that 65535; if it's not true and all your values are lower than that, why use an Int datatype (4 bytes) instead a tinyint datatype (2 bytes)?

Hope that helps.
0
RainverseCommented:
Fair enough

-MJC
0
Murray BrownMicrosoft Cloud Azure/Excel Solution DeveloperAuthor Commented:
Thanks very much
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.