是什么将删除表中的所有记录在此查询的问题! [英] what is the problem in this query it will delete all records of table !

查看:98
本文介绍了是什么将删除表中的所有记录在此查询的问题!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 保护小组GridView3_RowCommand(BYVAL发件人为对象,BYVAL E上System.Web.UI.WebControls.GridViewCommandEventArgs)处理GridView3.RowCommand
        对于每个myRow作为GridViewRow在GridView3.Rows
            查找复选框
            昏暗lab1中的作为标签= DirectCast(myRow.FindControl(标签1),标签)
            如果e.CommandName =萨米特然后
                DIM CMD作为新Data.SqlClient.SqlCommand
                昏暗CON作为新Data.SqlClient.SqlConnection(构造)
                尝试
                    昏暗STRSQL作为字符串=DELETE * FROM饭店里hotelid =&放大器; lab1.Text                    ------------
                    con.Open()
                    cmd.Connection = CON
                    cmd.CommandText = STRSQL
                    cmd.ExecuteNonQuery()
                抓住EX为例外
                    的Response.Write(ex.Message)
                最后
                    cmd.Dispose()
                    con.Dispose()
                结束Try
            万一
        下一个
        GridView3.DataBind()
    结束小组


解决方案

我相信你正在寻找的是这样的:

 保护小组GridView3_RowCommand(BYVAL发件人为对象,BYVAL E上System.Web.UI.WebControls.GridViewCommandEventArgs)处理GridView3.RowCommand
    昏暗myRow作为GridViewRow = DirectCast((System.Web.UI.Control)(发件人))。NamingContainer,GridViewRow)    查找复选框
    昏暗lab1中的作为标签= DirectCast(myRow.FindControl(标签1),标签)    如果e.CommandName =萨米特然后
        DIM CMD作为新Data.SqlClient.SqlCommand
        昏暗CON作为新Data.SqlClient.SqlConnection(构造)
        尝试
        昏暗STRSQL作为字符串=DELETE FROM饭店里hotelid =&放大器; lab1.Text        ------------
        con.Open()
        cmd.Connection = CON
        cmd.CommandText = STRSQL
        cmd.ExecuteNonQuery()
        抓住EX为例外
        的Response.Write(ex.Message)
        最后
        cmd.Dispose()
        con.Dispose()
                结束Try
            万一            GridView3.DataBind()
        结束小组

语法可能会关闭我不是太熟悉VB。还有倒不如通过酒店记录的ID在 e.CommandArgument 这样你就不必从页面上的标签检索。这是删除所有的记录,因为您是通过所有的行中的网格视图循环和删除每个记录。

 Protected Sub GridView3_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView3.RowCommand
        For Each myRow As GridViewRow In GridView3.Rows
            'Find the checkbox
            Dim lab1 As Label = DirectCast(myRow.FindControl("Label1"), Label)
            If e.CommandName = "Sumit" Then
                Dim cmd As New Data.SqlClient.SqlCommand
                Dim con As New Data.SqlClient.SqlConnection(constr)
                Try
                    Dim strSql As String = "DELETE * FROM hotels WHERE hotelid =" & lab1.Text

                    '------------"
                    con.Open()
                    cmd.Connection = con
                    cmd.CommandText = strSql
                    cmd.ExecuteNonQuery()
                Catch ex As Exception
                    Response.Write(ex.Message)
                Finally
                    cmd.Dispose()
                    con.Dispose()
                End Try
            End If
        Next
        GridView3.DataBind()
    End Sub

解决方案

I believe what you are looking for is this:

Protected Sub GridView3_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView3.RowCommand
    Dim myRow As GridViewRow = DirectCast((System.Web.UI.Control)(sender)).NamingContainer, GridViewRow)

    'Find the checkbox
    Dim lab1 As Label = DirectCast(myRow.FindControl("Label1"), Label)

    If e.CommandName = "Sumit" Then
        Dim cmd As New Data.SqlClient.SqlCommand
        Dim con As New Data.SqlClient.SqlConnection(constr)
        Try
        Dim strSql As String = "DELETE FROM hotels WHERE hotelid =" & lab1.Text

        '------------"
        con.Open()
        cmd.Connection = con
        cmd.CommandText = strSql
        cmd.ExecuteNonQuery()
        Catch ex As Exception
        Response.Write(ex.Message)
        Finally
        cmd.Dispose()
        con.Dispose()
                End Try
            End If

            GridView3.DataBind()
        End Sub

The syntax may be off I'm not too familiar with VB. As well it would be better to pass the ID of the hotel record in the e.CommandArgument thus you would not have to retrieve it from a label on the page. It is deleting all your records because you are looping through all your rows in the grid view and deleting each record.

这篇关于是什么将删除表中的所有记录在此查询的问题!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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