每當我運行它時,此異常將出現「nvarchar值'columnvalue'轉換爲數據類型int時轉換失敗。任何想法爲什麼?這也是一種永久性的刪除行的方式嗎?感謝您的未來建議!如何從datagridview中刪除選定的行
Dim dataindex As Integer = Me.DataGridView1.CurrentRow.Index
Try
con = New SqlConnection
con.ConnectionString = Login.constring
Dim cmd As New SqlCommand("DELETE FROM tblmovies where movietitle = @a ", con)
cmd.Parameters.Add(New SqlParameter("@a", dataindex))
con.Open()
If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.Yes Then
cmd.ExecuteNonQuery()
con.Close()
Exit Sub
Else
con.Close()
Exit Sub
End If
Catch ex As SqlException
MessageBox.Show(ex.Message)
End Try
movietitle似乎更像是一個電影的名字,但你比較它從您的datagridview的currentrow得出的指數。你可能想把你的參數設置爲在你的currentrow上保存你的movietitle的列值,或者改爲傳遞該行的主鍵(如果你可以從你的currentrow訪問它) –