Can't Insert records into a MS SQL SERVER IN VB.NET

i HAVE A WINFORM I'm trying to get my code to insert textboxes into a MS SQL SERVER.

i GET THIS AS AN ERROR
ExecuteNonQuery: CommandText property has not been initialized   at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at SDDSPlayers.frmPlayerupdate.Insertdata() in C:\Users\tpowers\Desktop\sdds .net\SDDSPlayers\SDDSPlayers\frmPlayerupdate.vb:line 52System.InvalidOperationException: ExecuteNonQuery: CommandText property has not been initialized
   at System.Data.SqlClient.SqlCommand.ValidateCommand(String method, Boolean async)
   at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite)
   at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()
   at SDDSPlayers.frmPlayerupdate.Insertdata() in C:\Users\tpowers\Desktop\sdds .net\SDDSPlayers\SDDSPlayers\frmPlayerupdate.vb:line 52

Open in new window



Using this in my winform
 Public Sub Insertdata()

        Dim strFirstName As String = CStr(txtFname.Text)
        Dim strLastname As String = CStr(txtLname.Text)
        Dim strTeamID As String = CStr(txtTeamID.Text)
        Dim strJersey As String = CStr(txtJersey.Text)
        Dim strPosition As String = CStr(cmbPosition.Text)
        Dim strStatus As String = CStr(cmbStatus.Text)
        Dim strHeight As String = CStr(txtHeight.Text)
        Dim strWeight As String = CStr(txtWeight.Text)
        Dim strBorn As String = CStr(dtp1.Text)
        Dim strCollege As String = CStr(txtCollege.Text)
        Dim strBioHTML As String = CStr(txtPlayerNote.Text)
        Dim query As String = String.Empty
        Using conn As New SqlConnection("Data Source=tsnappdev01;Initial Catalog=TSN2;User ID=sa;Password=xxxxxxxxx")
            
                Using comm As New SqlCommand()
                    With comm
                        .Connection = conn
                        .CommandType = CommandType.Text
                        .CommandText = query

                    query &= "INSERT INTO NFLPlayers (FirstName, Lastname,"
                    query &= "TeamID,Jersey,Position,Status,Height,Weight,Born,College,BioHTML"
                    query &= "VALUES (@FirstName,@Lastname,@TeamID,@Jersey,@Position,@Status,@Height,@Born,@College,@BioHTML)"
                    .Parameters.AddWithValue("@FirstName", strFirstName)
                    .Parameters.AddWithValue("@Lastname", strLastname)
                    .Parameters.AddWithValue("@TeamID", strTeamID)
                    .Parameters.AddWithValue("@Jersey", strJersey)
                    .Parameters.AddWithValue("@Position", strPosition)
                    .Parameters.AddWithValue("@Status", strStatus)
                    .Parameters.AddWithValue("@Height", strHeight)
                    .Parameters.AddWithValue("@Weight", strWeight)
                    .Parameters.AddWithValue("@Born", strBorn)
                    .Parameters.AddWithValue("@College", strCollege)
                    .Parameters.AddWithValue("@BioHTML", strBioHTML)

                End With
                Try
                    conn.Open()
                    comm.ExecuteNonQuery()
                Catch ex As Exception
                    txtPlayerNote.Text = ex.Message & ex.StackTrace & ex.ToString
                End Try
                conn.Close()
            End Using
        End Using
    End Sub

Open in new window

powerztomAsked:
Who is Participating?
 
Fernando SotoRetiredCommented:
Hi powerztom;

Change the order of the CommandText property till after the query is built as shown below.

query &= "INSERT INTO NFLPlayers (FirstName, Lastname,"
query &= "TeamID,Jersey,Position,Status,Height,Weight,Born,College,BioHTML"
query &= "VALUES (@FirstName,@Lastname,@TeamID,@Jersey,@Position,@Status,@Height,@Born,@College,@BioHTML)"
.CommandText = query

Open in new window

0
 
powerztomAuthor Commented:
Good Eye man Good Eye! Very Happy you spotted that
0
 
Fernando SotoRetiredCommented:
Glad I was able to help out powerztom.
0
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.

All Courses

From novice to tech pro — start learning today.