| |

VerySource

 Forgot password?
 Register
Search
View: 852|Reply: 9

Why can't I delete it. . DataGrid CheckBox selection delete problem

[Copy link]

2

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

Post time: 2020-1-4 16:00:02
| Show all posts |Read mode
Dim i As Integer
        Dim ChBox As CheckBox
        For i = 0 To DataGrid1.Items.Count-1
            ChBox = CType (DataGrid1.Items (i) .Cells (0) .FindControl ("CheckBox1"), CheckBox)
            If ChBox.Checked = True Then
                Dim conn As New SqlConnection
                conn.ConnectionString = "server = 127.0.0.1; uid = sa; pwd =; database = netexam"
                conn.Open ()
                Dim sql As String
                sql = "delete from question where id = ChBox"
                Dim cmd As New SqlCommand (sql, conn)
                cmd.ExecuteNonQuery ()
                bind ()
            End If
        Next
Reply

Use magic Report

0

Threads

64

Posts

35.00

Credits

Newbie

Rank: 1

Credits
35.00

 China

Post time: 2020-1-4 16:42:01
| Show all posts
Remove bind ().

If there are still problems, you should debug, and then follow the debugging to give a more clear description of how you judged that you can't delete it.
Reply

Use magic Report

0

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-4 22:42:01
| Show all posts
sql = "delete from question where id = ChBox"
There is a problem with this statement. You did not assign the ID correctly. There is no id = ChBox data in the database. You should define a hidden template column and define a Label to help determine the id field.
<asp: TemplateColumn>
<ItemTemplate>
<asp: Label runat = "server" Text = '<% # DataBinder.Eval (Container, "DataItem.id")%>'>
</ asp: Label>
</ ItemTemplate>
</ asp: TemplateColumn>

then

 Dim i As Integer
 Dim ChBox As CheckBox
 Dim label1 As Label

For i = 0 To DataGrid1.Items.Count-1
            ChBox = CType (DataGrid1.Items (i) .Cells (0) .FindControl ("CheckBox1"), CheckBox)

label1 = CType (DataGrid1.Items (1) .FindControl ("label1"), Label)
If ChBox.Checked = True Then
                Dim conn As New SqlConnection
                conn.ConnectionString = "server = 127.0.0.1; uid = sa; pwd =; database = netexam"
                conn.Open ()
                Dim sql As String
                sql = "delete from question where id = '" + label1.Text + "'";
                Dim cmd As New SqlCommand (sql, conn)
                cmd.ExecuteNonQuery ()
                bind ()
            End If
        Next
Reply

Use magic Report

0

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-4 23:03:01
| Show all posts
The definition of the template column should be as follows, almost wrong
<asp: TemplateColumn>
<ItemTemplate>
<asp: Label runat = "server" ID = "label1" Text = '<% # DataBinder.Eval (Container, "DataItem.id")%>'>
</ asp: Label>
</ ItemTemplate>
</ asp: TemplateColumn>
Reply

Use magic Report

2

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-1-6 10:39:01
| Show all posts
Thanksxinyu5460for the answer. . But there are still errors! I posted the code to help me see what went wrong? My thought is to use the DataGrid to do the test paper review function of the test system. . Use the outer CheckBox to select the CheckBox inside the DataGrid. . Then use the button to submit or delete the selected questions.
-------------------------------------------------- ----------------
<Columns>
<asp: TemplateColumn>
<ItemTemplate>
<asp: CheckBox id = "CheckBox1" runat = "server" AutoPostBack = "True"> </ asp: CheckBox>
</ ItemTemplate>
</ asp: TemplateColumn>
<asp: TemplateColumn>
<ItemTemplate>
<asp: Label runat = "server" ID = "Label1" Text = '<% # DataBinder.Eval (Container, "DataItem.id")%>'>
</ asp: Label>
</ ItemTemplate>
</ asp: TemplateColumn>
<asp: BoundColumn DataField = "question" HeaderText = "Question </ </ :: BoundColumn>
<asp: BoundColumn DataField = "A" HeaderText = "Option A"> </ asp: BoundColumn>
<asp: BoundColumn DataField = "B" HeaderText = "Option B"> </ asp: BoundColumn>
<asp: BoundColumn DataField = "C" HeaderText = "OptionC"> </ asp: BoundColumn>
<asp: BoundColumn DataField = "D" HeaderText = "Option D"> </ asp: BoundColumn>
<asp: BoundColumn DataField = "answer" HeaderText = "answer"> </ asp: BoundColumn>
<asp: BoundColumn DataField = "type" HeaderText = "Exam Question Type"> </ asp: BoundColumn>
<asp: BoundColumn DataField = "subjectname" HeaderText = "Examination Subjects"> </ asp: BoundColumn>
</ Columns>
-------------------------------------------------- ---------------------
        Dim i As Integer
        Dim ChBox As CheckBox
        Dim label1 As Label

        For i = 0 To DataGrid1.Items.Count-1
            ChBox = CType (DataGrid1.Items (i) .Cells (0) .FindControl ("CheckBox1"), CheckBox)

            label1 = CType (DataGrid1.Items (1) .FindControl ("label1"), Label)
            If ChBox.Checked = True Then
                Dim conn As New SqlConnection
                conn.ConnectionString = "server = 127.0.0.1; uid = sa; pwd =; database = netexam"
                conn.Open ()
                Dim sql As String
                sql = "delete from question where id = '" + label1.Text + "'"
                Dim cmd As New SqlCommand (sql, conn)
                cmd.ExecuteNonQuery ()
                bind ()
            End If
        Next
Reply

Use magic Report

0

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-26 18:54:01
| Show all posts
Because you did not call the Bind () method at the end of the loop, if you select something other than the first line, the following CheckBox selections are all ineffective. You only need to put the Bind () call after the For loop and it is OK , as follows.
Dim i As Integer
        Dim ChBox As CheckBox
        Dim label1 As Label

        For i = 0 To DataGrid1.Items.Count-1
            ChBox = CType (DataGrid1.Items (i) .Cells (0) .FindControl ("CheckBox1"), CheckBox)

            label1 = CType (DataGrid1.Items (1) .FindControl ("label1"), Label)
            If ChBox.Checked = True Then
                Dim conn As New SqlConnection
                conn.ConnectionString = "server = 127.0.0.1; uid = sa; pwd =; database = netexam"
                conn.Open ()
                Dim sql As String
                sql = "delete from question where id = '" + label1.Text + "'"
                Dim cmd As New SqlCommand (sql, conn)
                cmd.ExecuteNonQuery ()
            End If
        Next
  bind ()
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 United States

Post time: 2020-1-26 21:45:01
| Show all posts
sql = "delete from question where id = ChBox"
There is a problem with this statement
Reply

Use magic Report

0

Threads

3

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-26 21:54:02
| Show all posts
Better learn to debug by yourself
Reply

Use magic Report

2

Threads

6

Posts

4.00

Credits

Newbie

Rank: 1

Credits
4.00

 China

 Author| Post time: 2020-2-11 17:00:02
| Show all posts
There is another problem. . How to submit the selected topic of the CheckBox in the DataGrid to another table in the database?
Reply

Use magic Report

0

Threads

10

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-3-22 11:00:01
| Show all posts
I don't quite understand what your problem is! You have to use the selected data to generate data in another table. Use the Insert statement.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list