GridView通过代码隐藏更新存储过程

时间:2013-02-19 16:39:31

标签: c# asp.net sql-server visual-studio

有人能帮助我吗?我已经设法让RowEditing的功能工作,我现在正试图让RowUpdating继续。我的gridview设置是以下

<asp:GridView ID="CountryGridView" runat="server" AutoGenerateColumns="false" CssClass="mGrid" OnRowEditing="CountryGridView_RowEditing" OnRowCancelingEdit="CountryGridView_RowCancelingEdit" >
                    <Columns>
                        <asp:BoundField ReadOnly="true" DataField="i_SK_Accom" HeaderText="i_SK_Accom" Visible="false" />
                        <asp:BoundField ReadOnly="true" DataField="Accom_Code" HeaderText="Accom Code" />
                        <asp:BoundField ReadOnly="true" DataField="Accom_Name" HeaderText="Accom Name" />
                        <asp:BoundField DataField="OP49_Required" HeaderText="OP49 Required?" />
                        <asp:BoundField DataField="Weekly" HeaderText="Weekly" />
                        <asp:BoundField DataField="Daily" HeaderText="Daily" />
                        <asp:CommandField ShowEditButton="true" ButtonType="Image" EditImageUrl="~/Images/Edit.gif" UpdateImageUrl="~/Images/save.png" CancelImageUrl="~/Images/cancel.png" ControlStyle-CssClass="ImageButton" />
                    </Columns>
                </asp:GridView>

我正在尝试让RowUpdating通过要更新为MS SQL上预编译存储过程的参数的列。通常的方法如下所示,但我正在努力让gridview工作。

SqlCommand commEditConsultant = new SqlCommand("IFACE_JFA_ACCOM", conn.sbConn);
    commEditConsultant.CommandType = CommandType.StoredProcedure;

    try
    {
        commEditConsultant.Parameters.Add("@Statement", SqlDbType.VarChar).Value = "AccomUpdate";
        commEditConsultant.Parameters.Add("@Page", SqlDbType.VarChar).Value = "OP49";
        commEditConsultant.Parameters.Add("@PC_Username", SqlDbType.VarChar).Value = HttpContext.Current.User.Identity.Name.ToString();
        commEditConsultant.Parameters.Add("@Season_Name", SqlDbType.VarChar).Value = Season.Text;
        commEditConsultant.Parameters.Add("@i_SK_Accom", SqlDbType.VarChar).Value = Request["i_SK_Accom"].Trim().ToString();
        commEditConsultant.Parameters.Add("@OP49_Required", SqlDbType.VarChar).Value = ddl_OP49Required.SelectedItem.Value;
        commEditConsultant.Parameters.Add("@Weekly", SqlDbType.VarChar).Value = ddl_OP49Weekly.SelectedItem.Value;
        commEditConsultant.Parameters.Add("@Daily", SqlDbType.VarChar).Value = ddl_OP49Daily.SelectedItem.Value;
    }

上面指定的字段与gridview相同,但我很难将gridview rRowEditing值转换为变量/参数以传递给SQLCommand。

感兴趣的ID:

  DataBinder - BindCountryGrid
  DataReader - CountryGridSelectReader

1 个答案:

答案 0 :(得分:0)

最终解决了这个问题:

后面的代码需要重新审视以下内容:

protected void CountryGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Label Textbox_Accom = (Label)gv_CountryGridView.Rows[e.RowIndex].FindControl("lbl_Accom_Code");
            Label Textbox_Accom_Name = (Label)gv_CountryGridView.Rows[e.RowIndex].FindControl("lbl_Accom_Name");
            DropDownList Textbox_OP49 = (DropDownList)gv_CountryGridView.Rows[e.RowIndex].FindControl("ddl_OP49");
            DropDownList Textbox_Weekly = (DropDownList)gv_CountryGridView.Rows[e.RowIndex].FindControl("ddl_Weekly");
            DropDownList Textbox_Daily = (DropDownList)gv_CountryGridView.Rows[e.RowIndex].FindControl("ddl_Daily");
            TextBox Textbox_FRRStartDate = (TextBox)gv_CountryGridView.Rows[e.RowIndex].FindControl("txt_FRRStartDate");

            SqlCommand commEditConsultant = new SqlCommand("IFACE_JFA_ACCOM", ConnJFA);
            commEditConsultant.CommandType = CommandType.StoredProcedure;

            commEditConsultant.Parameters.Add("@Statement", SqlDbType.VarChar).Value = "AccomGridUpdate";
            commEditConsultant.Parameters.Add("@Page", SqlDbType.VarChar).Value = "OP49";
            commEditConsultant.Parameters.Add("@PC_Username", SqlDbType.VarChar).Value = HttpContext.Current.User.Identity.Name.ToUpper().ToString();
            commEditConsultant.Parameters.Add("@Season_Name", SqlDbType.VarChar).Value = txt_Season.Text;
            commEditConsultant.Parameters.Add("@Accom_Code", SqlDbType.VarChar).Value = Textbox_Accom.Text;
            commEditConsultant.Parameters.Add("@i_FK_SeasonID", SqlDbType.VarChar).Value = Request["i_FK_SeasonID"].Trim().ToString();
            commEditConsultant.Parameters.Add("@OP49_Required", SqlDbType.VarChar).Value = Textbox_OP49.Text;
            commEditConsultant.Parameters.Add("@Weekly", SqlDbType.VarChar).Value = Textbox_Weekly.Text;
            commEditConsultant.Parameters.Add("@Daily", SqlDbType.VarChar).Value = Textbox_Daily.Text;
            commEditConsultant.Parameters.Add("@FRR_StartDate", SqlDbType.VarChar).Value = Textbox_FRRStartDate.Text;

            ConnJFA.Open();
            commEditConsultant.ExecuteNonQuery();
            gv_CountryGridView.EditIndex = -1;
            Error_Dashboard.Text = Textbox_Accom.Text + " - " + Textbox_Accom_Name.Text + " Updated Successfully!";
            ConnJFA.Close();
            BindCountryGrid();


        }