M
MasterChief
I am trying to learn how to delete multple items in a database using
the code behind file. I would just like somebody to tell me what is
wrong with my code. I am new to connecting to the database through a
code-behind. I have a gridview called gridview1. When the user checks a
bunch of plans then should be able to hit delete and for each checked
box it should delete the plan and update the gridview1.
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class _Default
Inherits System.Web.UI.Page
Protected WithEvents gvName As System.Web.UI.WebControls.GridView
Protected Function GetPlanURL(ByVal plan_id As Int32) As String
Dim url As String
url = "~/EditItem.aspx?plan_id=" + plan_id.ToString()
Return url
End Function
Protected Sub Delete_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Delete.Click
Dim CheckBox As CheckBox
Dim KeyId As Int32
Dim strConnection As String =
System.Configuration.ConfigurationManager.AppSettings("draftinglogConnectionString1")
Dim objConnection As New SqlConnection(strConnection)
For Each GridViewRow As GridViewRow In GridView1.Rows
CheckBox = CType(GridViewRow.FindControl("chkDelete"),
CheckBox)
If CheckBox.Checked = True Then
KeyId =
CType(GridView1.DataKeys.Item(GridViewRow.RowIndex).Value, Int32)
Dim strSql As String = "delete from plans WHERE
plans.plan_id=" + KeyId.ToString()
Dim objCommand As New SqlCommand(strSql, objConnection)
objConnection.Open()
gvName.DataSource = objCommand.ExecuteReader()
gvName.DataBind()
objConnection.Close()
End If
Next
End Sub
End Class
the code behind file. I would just like somebody to tell me what is
wrong with my code. I am new to connecting to the database through a
code-behind. I have a gridview called gridview1. When the user checks a
bunch of plans then should be able to hit delete and for each checked
box it should delete the plan and update the gridview1.
Imports System.Data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Configuration
Partial Class _Default
Inherits System.Web.UI.Page
Protected WithEvents gvName As System.Web.UI.WebControls.GridView
Protected Function GetPlanURL(ByVal plan_id As Int32) As String
Dim url As String
url = "~/EditItem.aspx?plan_id=" + plan_id.ToString()
Return url
End Function
Protected Sub Delete_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Delete.Click
Dim CheckBox As CheckBox
Dim KeyId As Int32
Dim strConnection As String =
System.Configuration.ConfigurationManager.AppSettings("draftinglogConnectionString1")
Dim objConnection As New SqlConnection(strConnection)
For Each GridViewRow As GridViewRow In GridView1.Rows
CheckBox = CType(GridViewRow.FindControl("chkDelete"),
CheckBox)
If CheckBox.Checked = True Then
KeyId =
CType(GridView1.DataKeys.Item(GridViewRow.RowIndex).Value, Int32)
Dim strSql As String = "delete from plans WHERE
plans.plan_id=" + KeyId.ToString()
Dim objCommand As New SqlCommand(strSql, objConnection)
objConnection.Open()
gvName.DataSource = objCommand.ExecuteReader()
gvName.DataBind()
objConnection.Close()
End If
Next
End Sub
End Class