0

我在更新 SQL Server 中的行时遇到问题

当我想使用存储过程将网格中的列更新到 SQL Server 数据库中的表时,onclick按钮(用于更新)出现问题。它将调用一个RowUpdating事件...问题是无法访问该事件...因为我在第一行放了一个 MsgBox ..什么也没发生

这是代码:

asp.net 代码:

<asp:GridView ID="DossierGV" runat="server" AllowPaging="True" 
              AllowSorting="True"  AutoGenerateColumns="False" 
              DataKeyNames="ID_Dossier" onrowupdating="DossierGV_RowUpdating" >
   <Columns>
      <%--       <asp:BoundField DataField="Nom_Giac" HeaderText="Nom_Giac" 
                          SortExpression="Nom_Giac" />--%>
      <asp:CommandField ShowEditButton="True" UpdateText="Modifier "></asp:CommandField>
      <asp:CommandField ShowSelectButton="True" />
      <asp:TemplateField HeaderText="ID_Dossier" SortExpression="ID_Dossier">
         <EditItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%# Eval("ID_Dossier") %>'></asp:Label>
         </EditItemTemplate>
         <ItemTemplate>
            <asp:Label ID="Label1" runat="server" Text='<%# Bind("ID_Dossier") %>'></asp:Label>
         </ItemTemplate>
      </asp:TemplateField>
      <asp:TemplateField HeaderText="ID_Entreprise" SortExpression="ID_Entreprise">
         <EditItemTemplate>
            <asp:Label ID="ID_Entrepriselbl_Edit" runat="server" Text='<%# Bind("ID_Entreprise") %>'></asp:Label>
         </EditItemTemplate>
         <ItemTemplate>
            <asp:Label ID="ID_Entrepriselbl_Item" runat="server" Text='<%# Bind("ID_Entreprise") %>'></asp:Label>
         </ItemTemplate>
      </asp:TemplateField>
    ..... (lots more <asp:TemplateField) ..........
   </Columns>
   <SelectedRowStyle BackColor="#F2E488" Font-Bold="True" />

这是服务器代码:

Imports System.Data.SqlClient
Imports System.Data
Imports System.Configuration

Partial Class GIAC_Gestion_des_dossiers_Les_Dossiers_Finance
    Inherits System.Web.UI.Page
    Dim sqlcon As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("OfficeConnectionString").ConnectionString)
    Dim sqlcmd As SqlCommand
    Dim da As SqlDataAdapter
    Dim dt As DataTable = New DataTable
    Dim dt1 As DataTable = New DataTable

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        RecherhcerComboBox.SelectedValue = RecherhcerComboBox.Items(0).Value
        If Not Page.IsPostBack Then
            GridData()
        End If
    End Sub

    Private Sub GridData()
        sqlcmd = New SqlCommand("DossierPF", sqlcon)
        sqlcon.Open()
        sqlcmd.CommandType = CommandType.StoredProcedure
        sqlcmd.Parameters.Add("@nom", SqlDbType.VarChar).Value = Session("Nom_GIAC")
        da = New SqlDataAdapter(sqlcmd)
        dt.Clear()
        da.Fill(dt)
        If (dt.Rows.Count > 0) Then
            DossierGV.DataSource = dt
            DossierGV.DataBind()
        End If
    End Sub

    Protected Sub DossierGV_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DossierGV.SelectedIndexChanged
        GridData()
    End Sub

    Protected Sub DossierGV_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles DossierGV.PageIndexChanging
        DossierGV.EditIndex = -1
        DossierGV.SelectedIndex = -1
        DossierGV.PageIndex = e.NewPageIndex
        GridData()
    End Sub

    Protected Sub DossierGV_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles DossierGV.RowCancelingEdit
        DossierGV.EditIndex = -1
        GridData()
    End Sub

    Protected Sub DossierGV_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles DossierGV.RowEditing
        DossierGV.EditIndex = e.NewEditIndex
        GridData()
    End Sub

    Protected Sub DossierGV_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles DossierGV.RowUpdating
        MsgBox("walo")
        Dim row As GridViewRow = DossierGV.Rows(e.RowIndex)
        Dim ID_Dossier As Integer = DossierGV.DataKeys(e.RowIndex).Value
        Dim Montant_Demander As TextBox = CType(row.FindControl("Montant_DemanderTXT"), TextBox)
        Dim Duree As TextBox = CType(row.FindControl("DureeTXT"), TextBox)
        Dim Date_Debut As TextBox = CType(row.FindControl("Date_DebutTXT"), TextBox)
        Dim Date_Fin As TextBox = CType(row.FindControl("Date_FinTXT"), TextBox)

        sqlcmd = New SqlCommand("UPDATE_DOSSIER", sqlcon)
        DossierGV.EditIndex = -1
        sqlcon.Open()
        sqlcmd.CommandType = CommandType.StoredProcedure
        sqlcmd.Parameters.Add("@ID_DOSSIER", SqlDbType.Int).Value = ID_Dossier
        sqlcmd.Parameters.Add("@Montant_Demander", SqlDbType.Int).Value = Convert.ToInt64(Montant_Demander.Text)
        sqlcmd.Parameters.Add("@@Duree", SqlDbType.Int).Value = Convert.ToInt64(Duree.Text)
        sqlcmd.Parameters.Add("@Date_Debut", SqlDbType.Int).Value = Convert.ToDateTime(Date_Debut.Text)
        sqlcmd.Parameters.Add("@Date_Fin", SqlDbType.Int).Value = Convert.ToDateTime(Date_Fin.Text)
        MsgBox(ID_Dossier & " , " & Montant_Demander.Text & " , " & Duree.Text & " , " & Date_Debut.Text & " , " & Date_Fin.Text)
        sqlcmd.ExecuteNonQuery()
        sqlcon.Close()

        GridData()
    End Sub
End Class

存储过程:

CREATE PROCEDURE UPDATE_DOSSIER 
 @ID_DOSSIER integer,
 @Montant_Demander integer,
 @Duree integer,
 @Date_Debut datetime,
 @Date_Fin datetime
AS 
BEGIN
   update dbo.Dossier
   set Montant_Demander = @Montant_Demander,
       Duree = @Duree,
       Date_Debut = @Date_Debut,
       Date_Fin = @Date_Fin
   where 
       ID_Dossier = @ID_DOSSIER
END

exec UPDATE_DOSSIER 1,400,5,'2007-01-11 00:00:00.000','2012-01-11 00:00:00.000'
4

0 回答 0