2016-12-01 72 views
-1

我有這個錯誤錯誤:轉換失敗的轉換VARCHAR值時,到整數

Conversion failed when converting the varchar value to Integer

的代碼很難似乎做工精細,我用它在別人,但唯一不同的是主鍵我使用的是,這個我使用了一個整數,而其他的我使用nvarchar作爲他們的主鍵。提前感謝任何答案。

這裏是我的代碼:

 Private Sub deleteIndv() 

    Dim result 
    Dim id As String = Me.DataGridView2.CurrentRow.Cells(0).Value 

    Using cmd As New SqlClient.SqlCommand 

     result = MsgBox(" Are You Sure You Want to Delete the Selected Row ? ", MsgBoxStyle.YesNo) 

     TextBox2.Text = Me.DataGridView2.CurrentRow.Cells(0).Value.ToString 

     ' Try 

     If result = Windows.Forms.DialogResult.No Then 

      Exit Sub 

     Else 

      If con.State = ConnectionState.Closed Then 

       con.Open() 

      End If 

      cmd.Connection = con 

      cmd.CommandText = "DELETE FROM IndvReports WHERE Id_indv = " & id 

      cmd.Parameters.AddWithValue("@id", id) 

      cmd.ExecuteNonQuery() 

      Me.DataGridView2.Rows.Remove(Me.DataGridView2.CurrentRow) 

      MsgBox(" Record Successfully Deleted! ", MsgBoxStyle.Information) 

      cmd.Parameters.Clear() 

     End If 

     'Catch ex As Exception 

     ' MsgBox(" Error Deleting Records! ", MsgBoxStyle.Exclamation) 

     ' End Try 

     cmd.Parameters.Clear() 

    End Using 

    If con.State = ConnectionState.Open Then 

     con.Close() 

    End If 
+0

id是什麼值? –

+0

我沒有完全得到你,因爲我已經瞭解你的問題,這是id的值「Dim id As String = Me.DataGridView2.CurrentRow.Cells(0).Value」while on sql the value of我使用的ID是一個自動增量值 – Ralph

+1

[SQL注入警報](http://msdn.microsoft.com/en-us/library/ms161953%28v=sql.105%29.aspx) - 你應該不** **連接在一起你的SQL語句 - 使用**參數化查詢**,而不是SQL注入 –

回答

0

由SQL產生的錯誤。 你肯定低於實際返回線和整數?:

Dim id As String = Me.DataGridView2.CurrentRow.Cells(0).Value 

測試它第一次(沿下面的例子的東西):

Dim g As String 
Try 
     Dim l As Integer = Integer.Parse(g) 
     'If successful then save action' 

Catch ex As Exception 
     'Warn about error' 

End Try 
+0

我認爲..它沒有。在我運行該程序後,顯示我在「autos」中看到的錯誤,即我爲要存儲的「indv_id」設置的「id」不是indv_id,而是sql表數據上的第1列-_- – Ralph

+0

以及我只遇到這裏有一個問題。在我創建的其他刪除函數中,Id是一個nvarchar,我的代碼與上面提到的完全一樣。 – Ralph

+0

如果你想保存一個整數,你應該做它應該是一個整數..測試它並處理任何相關的錯誤是第一步。我可能會遇到一個無法成功轉換爲整數的字符:例如:an,(逗號)當a。 (點)預計,nbsp(非破壞性的空間[認爲字符(160)]時,預計空間等 –

0

我解決我的問題。問題出在這個「Dim id As String = Me.DataGridView2.CurrentRow.Cells(0).Value」我只是指定了表Id_indv的確切單元號,我將它從(0)更改爲(18)列我的PK位於。感謝那些幫助我解決這個問題的人。 以下是他人的代理人蔘考:

Dim result 
    Dim id As String = Me.DataGridView2.CurrentRow.Cells(18).Value 

    Using cmd As New SqlClient.SqlCommand 

     result = MsgBox(" Are You Sure You Want to Delete the Selected Row ? ", MsgBoxStyle.YesNo) 

     ' Try 

     If result = Windows.Forms.DialogResult.No Then 

      Exit Sub 

     Else 

      If con.State = ConnectionState.Closed Then 

       con.Open() 

      End If 

      cmd.Connection = con 

      cmd.CommandText = "DELETE FROM IndvReports WHERE Id_indv = " & id 

      cmd.Parameters.AddWithValue("@id", id) 

      cmd.ExecuteNonQuery() 

      Me.DataGridView2.Rows.Remove(Me.DataGridView2.CurrentRow) 

      MsgBox(" Record Successfully Deleted! ", MsgBoxStyle.Information) 

      cmd.Parameters.Clear() 

     End If 

     'Catch ex As Exception 

     ' MsgBox(" Error Deleting Records! ", MsgBoxStyle.Exclamation) 

     ' End Try 

     cmd.Parameters.Clear() 

    End Using 

    If con.State = ConnectionState.Open Then 

     con.Close() 

    End If 
相關問題