如何从gridview插入数据并选中复选框? [英] How to insert data from gridview with checkbox selected?

查看:56
本文介绍了如何从gridview插入数据并选中复选框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




我从Gridview检查项目中插入数据时遇到问题

如果我勾选多行,首先勾选的数据只插入到表格中。 />


请帮帮我



我的尝试:



我试过这个



Hi
I have problem to insert data from Gridview checked item
If i tick multiple line, first ticked data only inserted into table.

Pls help me

What I have tried:

I have tried this

cmd = New SqlCommand
cmd.Connection = conn
For Each gvrow As GridViewRow In gvSubjects.Rows
    Dim chkSelect As CheckBox = DirectCast(gvrow.FindControl("chkSelect"), CheckBox)
    If chkSelect.Checked Then

        cmd.CommandText = "insert into ADM_Faculty_Subject (Code, Name, Classes, SubName, Charges, Location) values ('" + Me.txtCode.Text + _
                                  "','" + Me.txtName.Text + _
                                  "','" + gvrow.Cells(2).Text + _
                                  "','" + gvrow.Cells(3).Text + _
                                  "','" + gvrow.Cells(4).Text + _
                                  "','" + Me.cboLocation.SelectedValue + "')"

        cmd.CommandType = CommandType.Text
        conn.Open()
        cmd.ExecuteNonQuery()
        cmd = Nothing
        conn.Close()
    End If
Next

推荐答案

首先修复 SQL注入 [ ^ ]漏洞的代码。



然后,您需要为每次迭代删除将 cmd 对象设置为 Nothing 的行。 />


您可能还想将整个块包装在一个事务中阻止部分插入。

Start by fixing the SQL Injection[^] vulnerability in your code.

Then, you need to remove the line that's setting the cmd object to Nothing for each iteration.

You'll probably also want to wrap the entire block in a transaction, to prevent partial inserts.
Using conn As New SqlConnection("YOUR CONNECTION STRING HERE")
    conn.Open()
    
    Using transaction As SqlTransaction = conn.BeginTransaction()
        Using cmd As New SqlCommand("insert into ADM_Faculty_Subject (Code, Name, Classes, SubName, Charges, Location) values (@Code, @Name, @Classes, @SubName, @Charges, @Location)", conn, transaction)
            
            cmd.CommandType = CommandType.Text
            
            ' TODO: Correct data types and sizes for the parameters to match the columns:
            Dim pCode As SqlParameter = cmd.Parameters.Add("@Code", SqlDbType.VarChar, 50)
            Dim pName As SqlParameter = cmd.Parameters.Add("@Name", SqlDbType.VarChar, 50)
            Dim pClasses As SqlParameter = cmd.Parameters.Add("@Classes", SqlDbType.VarChar, 50)
            Dim pSubName As SqlParameter = cmd.Parameters.Add("@SubName", SqlDbType.VarChar, 50)
            Dim pCharges As SqlParameter = cmd.Parameters.Add("@Charges", SqlDbType.VarChar, 50)
            Dim pLocation As SqlParameter = cmd.Parameters.Add("@Location", SqlDbType.VarChar, 50)
            
            pCode.Value = Me.txtCode.Text
            pName.Value = Me.txtName.Text
            pLocation.Value = Me.cboLocation.SelectedValue
            
            For Each gvrow As GridViewRow In gvSubjects.Rows
                Dim chkSelect As CheckBox = DirectCast(gvrow.FindControl("chkSelect"), CheckBox)
                If chkSelect.Checked Then
                    pClasses.Value = gvrow.Cells(2).Text
                    pSubName.Value = gvrow.Cells(3).Text
                    pCharges.Value = gvrow.Cells(4).Text
                    cmd.ExecuteNonQuery()
                End If
            Next
        End Using
        
        transaction.Commit()
    End Using
End Using


这篇关于如何从gridview插入数据并选中复选框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆