Solved

the wrong data type in vb.net to do an  a insert into sql server

Posted on 2014-11-26
8
301 Views
Last Modified: 2014-11-26
I am getting An unhandled exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll

Additional information: Conversion from string "Regular" to type 'Integer' is not valid.

I tried String then Integer the thing is it's just a combobox with two choices NFC OR AFC. WHY IS THIS HAPPENING HERE IS THE INSERT
 Public Sub Insertdata()
        Dim strplayerid As Integer = CInt(txtPlayerID.Text)
        Dim strTEAMID As String = CStr(txtTeamID.Text)
        Dim strSEASON As String = CStr(txtSeason.Text)
        Dim strPOSTSEASON As Integer = CInt(cmbPassSeason.Text)
        Dim strConference As String = CStr(txtConf.Text)
        Dim strAttempts As String = CStr(txtatt.Text)
        Dim strLong As String = CStr(txtlong.Text)
        Dim strYards As String = CStr(txtYds.Text)
        Dim strTD As String = CStr(txttds.Text)
        Dim strInterception As String = CStr(txtInter.Text)
        Dim strSacks As String = CStr(txtsacks.Text)
        Dim strSacksyds As String = CStr(txtsackyds.Text)
        Dim strRating As Integer = CInt(txtQBRating.Text)
        '  Dim strCollyr As String = CStr(cmbyrsinCol.Text)
        '  Dim query As String = String.Empty


        Using conn As New SqlConnection("Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=sportsrus")

            Using comm As New SqlCommand()
                With comm
                    .Connection = conn
                    .CommandType = CommandType.Text



                    .Parameters.AddWithValue("@PlayerID", strplayerid)
                    .Parameters.AddWithValue("@Team", strTEAMID)
                    .Parameters.AddWithValue("@Season", strTEAMID)
                    .Parameters.AddWithValue("@SeasonType", strPOSTSEASON)
                    .Parameters.AddWithValue("@Comp", strConference)
                    .Parameters.AddWithValue("@Att", strAttempts)
                    .Parameters.AddWithValue("@Yards", strYards)
                    .Parameters.AddWithValue("@Long", strLong)
                    .Parameters.AddWithValue("@TD", strTD)
                    .Parameters.AddWithValue("@Inter", strInterception)
                    .Parameters.AddWithValue("@Sacks", strSacks)
                    .Parameters.AddWithValue("@SKYds", strSacksyds)
                    .Parameters.AddWithValue("@Rating", strRating)
                    Dim query As String = String.Empty

                    query &= "INSERT INTO NFLPlayerspass (PlayerID, Team,"
                    query &= "Season,SeasonType,Comp,Att,Yards,Long,TD,Inter,Sacks,SKYds,Rating)"
                    query &= "VALUES (@PlayerID,@Team,@Season,@SeasonType,@Comp,@Att,@Yards,@Long,@TD,@Inter,@Sacks,@SKYds,@Rating)"

                    .CommandText = query
                End With
                Try
                    conn.Open()
                    comm.ExecuteNonQuery()
                    MessageBox.Show("Record Added!")
                Catch ex As SqlException
                    MessageBox.Show(ex.ToString)
                Finally
                    conn.Close()
                End Try

                txtPlayerID.Visible = True
               

            End Using
        End Using
    End Sub

Open in new window


HEY IS ERROR MESSAGEBOX
INSERT ERROR MESSAGE  Thanks EE as always
 Dim strPOSTSEASON As Integer = CInt(cmbPassSeason.Text) I know it's not a integer but tried string
In the SQL Database the field season type is varchar(10) hopefully a clue for you guys
0
Comment
Question by:powerztom
  • 3
  • 2
  • 2
  • +1
8 Comments
 
LVL 24

Expert Comment

by:Phillip Burton
ID: 40467153
I don't know where the problem is - VB or MS SQL Server.

Assuming the problem is with SQL Server, try one of the following:

CAST(<<my expression>> as smallint)
CONVERT(smallint, <<my expression>>)

If you are using SQL Server 2012 or 2014, then use the following:

TRY_CAST(<<my expression>> as smallint)
TRY_CONVERT(smallint, <<my expression>>)

It will convert it to NULL if the value is not an integer.
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 40467155
It looks like the table field storing your value AFC or NFC is set to store an integer - and AFC/NFC is not an integer.  Check in the table definition in the database.
0
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 300 total points
ID: 40467170
Just a wild guess, but since you are storing player stats, should you be storing conference in the Comp field? Isn't that Completions?
0
 

Author Comment

by:powerztom
ID: 40467232
Comp is completions conference is conference Phillip Burton were at sql 2005
I am vb guy I know sql queries and a little dba CAST(<<my expression>> as smallint)
CONVERT(smallint, <<my expression>>) I have no idea what to do that would I do that in sql how or vb
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 26

Assisted Solution

by:Shaun Kline
Shaun Kline earned 300 total points
ID: 40467354
In your posted code above, line 32, you are assigning strConference (which I am inferring is NFL/AFL) to @Comp (which you say is completions, an integer value). Is that what you wanted?
0
 
LVL 44

Assisted Solution

by:AndyAinscow
AndyAinscow earned 200 total points
ID: 40467369
To repeat myself:

It looks like the table field storing your value AFC or NFC is set to store an integer - and AFC/NFC is not an integer.  Check in the table definition in the database.
0
 

Author Closing Comment

by:powerztom
ID: 40467466
Some of these answers helped me write a query  that works. Check it out on problempage I'll post
0
 

Author Comment

by:powerztom
ID: 40467468
 Public Sub Insertdata()
        Dim strplayerid As Integer = CInt(txtPlayerID.Text)
        Dim strTEAMID As String = CStr(txtTeamID.Text)
        Dim strSEASON As Integer = CInt(txtSeason.Text)
        Dim strPOSTSEASON As String = CStr(cmbPassSeason.Text)
        Dim strConference As String = CStr(txtConf.Text)
        Dim strComp As Integer = CInt(txtComp.Text)
        Dim strAttempts As Integer = CInt(txtatt.Text)
        Dim strLong As Integer = CInt(txtlong.Text)
        Dim strYards As Integer = CInt(txtYds.Text)
        Dim strTD As Integer = CInt(txttds.Text)
        Dim strInterception As Integer = CInt(txtInter.Text)
        Dim strSacks As Integer = CInt(txtsacks.Text)
        Dim strSacksyds As Integer = CInt(txtsackyds.Text)
        Dim strRating As Decimal = CDec(txtQBRating.Text)
        '  Dim strCollyr As String = CStr(cmbyrsinCol.Text)
        '  Dim query As String = String.Empty


        Using conn As New SqlConnection("Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=sportsrus")

            Using comm As New SqlCommand()
                With comm
                    .Connection = conn
                    .CommandType = CommandType.Text

                    .Parameters.AddWithValue("@PlayerID", SqlDbType.Int).Value = strplayerid
                    .Parameters.AddWithValue("@Team", SqlDbType.Char).Value = strTEAMID
                    .Parameters.AddWithValue("@Season", SqlDbType.Char).Value = strSEASON
                    .Parameters.AddWithValue("@SeasonType", SqlDbType.VarChar).Value = strPOSTSEASON
                    .Parameters.AddWithValue("@Conference", SqlDbType.Char).Value = strConference
                    .Parameters.AddWithValue("@Att", SqlDbType.SmallInt).Value = strAttempts
                    .Parameters.AddWithValue("@Comp", SqlDbType.SmallInt).Value = strComp
                    .Parameters.AddWithValue("@Yards", SqlDbType.SmallInt).Value = strYards
                    .Parameters.AddWithValue("@Long", SqlDbType.SmallInt).Value = strLong
                    .Parameters.AddWithValue("@TD", SqlDbType.TinyInt).Value = strTD
                    .Parameters.AddWithValue("@Inter", SqlDbType.TinyInt).Value = strInterception
                    .Parameters.AddWithValue("@Sacks", SqlDbType.TinyInt).Value = strSacks
                    .Parameters.AddWithValue("@SKYds", SqlDbType.SmallInt).Value = strSacksyds
                    .Parameters.AddWithValue("@Rating", SqlDbType.Real).Value = strRating
                    Dim query As String = String.Empty

                    '.Parameters.AddWithValue("@PlayerID", strplayerid)
                    '.Parameters.AddWithValue("@Team", strTEAMID)
                    ' .Parameters.AddWithValue("@Season", strSEASON)
                    ' .Parameters.AddWithValue("@SeasonType", strPOSTSEASON)
                    '.Parameters.AddWithValue("@Conference", strConference)
                    ' .Parameters.AddWithValue("@Att", strAttempts)
                    ' .Parameters.AddWithValue("@Comp", strComp)
                    '.Parameters.AddWithValue("@Yards", strYards)
                    '.Parameters.AddWithValue("@Long", strLong)
                    ' .Parameters.AddWithValue("@TD", strTD)
                    '.Parameters.AddWithValue("@Inter", strInterception)
                    ' .Parameters.AddWithValue("@Sacks", strSacks)
                    ' .Parameters.AddWithValue("@SKYds", strSacksyds)
                    ' .Parameters.AddWithValue("@Rating", strRating)
                    ' Dim query As String = String.Empty

                    query &= "INSERT INTO NFLPlayerspass (PlayerID, Team,"
                    query &= "Season,SeasonType,Conference,Comp,Att,Yards,Long,TD,Inter,Sacks,SKYds,Rating)"
                    query &= "VALUES (@PlayerID,@Team,@Season,@SeasonType,@Conference,@Att,@Comp,@Yards,@Long,@TD,@Inter,@Sacks,@SKYds,@Rating)"

                    .CommandText = query
                End With
                Try
                    conn.Open()
                    comm.ExecuteNonQuery()
                    MessageBox.Show("Record Added!")
                Catch ex As SqlException
                    MessageBox.Show(ex.ToString)
                Finally
                    conn.Close()
                End Try
                GetpassingNfl()
                txtPlayerID.Visible = True
               

            End Using
        End Using
    End Sub

Open in new window

0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

759 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now