Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

SQL
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
Posted
Updated 2-Aug-16 5:25am
Comments

1 solution

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.
VB.NET
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
 
Share this answer
 
Comments
Maideen Abdul Kader 2-Aug-16 19:50pm    
Thank you very much..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900