删除Datagrid中的问题 [英] Delete Problem in Datagrid
本文介绍了删除Datagrid中的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试使用删除按钮删除数据库中选定的行,该行由数据网格显示在vb.net中...这是我的代码。我一直有错误
i am try to use the delete button to delete a selected row in the database which is displayed in vb.net by a datagrid...here is my code .i keep having an error
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btndelete.Click
'check for the selected item in list
If Me.dgvdata.Rows.Count > 0 Then
If Me.dgvdata.SelectedRows.Count > 0 Then
Dim prdname As String = Me.dgvdata.SelectedRows(0).Cells("name").Value
'open connection
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
'delete data
Dim cmd As New SqlCommand
cmd.Connection = cnn
cmd.CommandText = "DELETE FROM stock WHERE product_name=" & Name
cmd.ExecuteNonQuery()
'refresh data
Me.RefreshData()
'close connection
cnn.Close()
End If
End If
End Sub
推荐答案
查看代码我认为问题在你的查询中,
Looking at your code i think the problem is in your query,
cmd.CommandText = "DELETE FROM stock WHERE product_name=" & Name
但是我没有在任何地方看到姓名,
我想你想在你的查询中使用 prdname
没有错误信息,很难确定,但第一次让我想到的是:
Without the error message, it''s difficult to be sure, but the first thing that springs to mind is this:
Dim prdname As String = Me.dgvdata.SelectedRows(0).Cells("name").Value
...
cmd.CommandText = "DELETE FROM stock WHERE product_name=" & Name
您是否要在查询中使用姓名
?不应该是 prdname
?
BTW:不要连接用于构建SQL命令的字符串。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。
Did you mean to use Name
in the query? Shouldn''t it be prdname
?
BTW: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
完整正确的代码是:
the full correct code is :
Imports System.Data
Imports System.Data.SqlClient
Public Class sedit
Dim ds, ds1, ds2 As DataSet
Dim da, da2, da11, da3 As SqlDataAdapter
Dim cmd, cmd1, cmd2, cmd3 As SqlCommand
Dim cb As SqlCommandBuilder
Dim drow, drow1 As DataRow
Dim dr, dr1, dr2 As SqlDataReader
Dim cnn As New SqlClient.SqlConnection
Private Sub sedit_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cnn = New SqlClient.SqlConnection
cnn.ConnectionString = ("server=.;uid=sa;pwd=june;database=pos;integrated security=false")
Me.RefreshData()
Timer1.Interval = 100
Timer1.Start()
End Sub
Private Sub timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = System.DateTime.Now
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim cmd As New SqlCommand("select * from stock where product_name like ''%" + TextBox1.Text + "%''", cnn)
Dim Adpt As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
If (Adpt.Fill(ds, "stock")) Then
dgvdata.DataSource = ds.Tables(0)
MessageBox.Show("match found")
Else
MessageBox.Show("match not found")
End If
End Sub
Private Sub Button6_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnclose.Click
Me.Close()
End Sub
Private Sub DataGridView1_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvdata.CellContentClick
End Sub
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btndelete.Click
''check for the selected item in list
If Me.dgvdata.Rows.Count > 0 Then
If Me.dgvdata.SelectedRows.Count > 0 Then
Dim prdname As String = Me.dgvdata.SelectedRows(0).Cells("name").Value
''open connection
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
''delete data
Dim cmd As New SqlCommand
cmd.Connection = cnn
cmd.CommandText = "DELETE FROM stock WHERE product_name=''" & prdname & "''"
cmd.ExecuteNonQuery()
''refresh data
Me.RefreshData()
''close connection
cnn.Close()
End If
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnclear.Click
Me.Txtproductname.Text = ""
Me.Txtproductid.Text = ""
Me.txtunit.Text = ""
Me.txtprice.Text = ""
Me.txtdate_in.Text = ""
Me.Txtproductname.Tag = ""
''enable button edit
Me.btnEdit.Enabled = True
''set button add to add label
Me.btnAdd.Text = "Add"
''
Me.Txtproductname.Focus()
End Sub
Private Sub RefreshData()
If Not cnn.State = ConnectionState.Open Then
''open connection
cnn.Open()
End If
Dim da4 As New SqlClient.SqlDataAdapter("SELECT product_name as [name], " & _
"product_id as [id], unit, Price, date_in " & _
" FROM stock ORDER BY product_name", cnn)
Dim dt As New DataTable
''fill data to datatable
da4.Fill(dt)
''offer data in data table into datagridview
Me.dgvData.DataSource = dt
''close connection
cnn.Close()
End Sub
Private Sub Btnadd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnadd.Click
Dim cmd As New SqlCommand
If Not cnn.State = ConnectionState.Open Then
''open connection if it is not yet open
cnn.Open()
End If
cmd.Connection = cnn
''check whether add new or update
If Me.Txtproductname.Tag & "" = "" Then
''add new
''add data to table
cmd.CommandText = "INSERT INTO stock(product_name, product_id, unit,price, item_type, date_in) " & _
" VALUES(''" & Me.Txtproductname.Text & "'',''" & Me.Txtproductid.Text & "'',''" & _
Me.txtunit.Text & "'',''" & Me.txtprice.Text & "'',''" & _
Me.Txtitem_type.Text & "'',''" & Me.txtdate_in.Text & "'')"
cmd.ExecuteNonQuery()
Else
''update data in table
cmd.CommandText = "UPDATE stock " & _
" SET product_name=@prodName " & _
", product_id=@prodID" & _
", unit=@unit" & _
", price=@price" & _
", item_type=@itemType" & _
", date_in=@date" & _
" WHERE product_name=@prodTag"
cmd.Parameters.AddWithValue("@prodName", Me.Txtproductname.Text)
cmd.Parameters.AddWithValue("@prodID", Me.Txtproductid.Text)
cmd.Parameters.AddWithValue("@unit", Me.txtunit.Text)
cmd.Parameters.AddWithValue("@price", Me.txtprice.Text)
cmd.Parameters.AddWithValue("@itemType", Me.Txtitem_type.Text)
cmd.Parameters.AddWithValue("@date", Me.txtdate_in.Text)
cmd.Parameters.AddWithValue("@prodTag", Me.Txtproductname.Tag)
cmd.ExecuteNonQuery()
End If
''refresh data in list
RefreshData()
''clear form
Me.Btnclear.PerformClick()
''close connection
cnn.Close()
End Sub
Private Sub Btnedit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Btnedit.Click
If Me.dgvdata.Rows.Count > 0 Then
If Me.dgvdata.SelectedRows.Count > 0 Then
Dim product_name1 As String = Me.dgvdata.SelectedRows(0).Cells("name").Value
''get data from database followed by product_name
''open connection
If Not cnn.State = ConnectionState.Open Then
cnn.Open()
End If
''get data into datatable
Dim da11 As New SqlClient.SqlDataAdapter("SELECT * FROM stock where product_name= ''" & product_name1 & "''", cnn)
Dim dt As New DataTable
da11.Fill(dt)
Me.Txtproductname.Text = product_name1
Me.Txtproductid.Text = dt.Rows(0).Item("product_id")
Me.txtunit.Text = dt.Rows(0).Item("unit")
Me.txtprice.Text = dt.Rows(0).Item("price")
Me.Txtitem_type.Text = dt.Rows(0).Item("item_type")
Me.txtdate_in.Text = dt.Rows(0).Item("date_in")
Me.Txtproductname.Tag = product_name1
''change button add to update
Me.Btnadd.Text = "Update"
''disable button edit
Me.Btnedit.Enabled = False
''close connection
cnn.Close()
End If
End If
End Sub
End Class
这篇关于删除Datagrid中的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文