单击按钮后更改SQL数据gridview

时间:2018-06-25 01:40:23

标签: c# asp.net sql-server

我正在尝试更改网格视图中显示的SQL中的状态数据,但无法执行。我要在此处执行的操作是,每当按下批准按钮时,便能够将所选行的状态值更改为“ A”。我可以知道怎么做吗?

这是我的界面的预览,更深一层是我的代码:

enter image description here

aspx文件:

<%@ Page Title="" Language="C#" MasterPageFile="~/AdminTemplate.Master" AutoEventWireup="true" CodeBehind="ViewRequests.aspx.cs" Inherits="webAssignment_P05_Group2.ViewRequests" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<style type="text/css">
    .auto-style2 {
        height: 388px;
        color: #000000;
        background-color: #FFFFFF;
    }
    .auto-style3 {
        width: 96%
    }
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<table class="auto-style3">
    <tr>
        <td class="auto-style2">
            <asp:GridView ID="gv_ParentRequests" runat="server" AllowPaging="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Vertical" Height="263px" Width="819px" AutoGenerateSelectButton="True" OnSelectedIndexChanged="gv_ParentRequests_SelectedIndexChanged" DataKeyNames="ViewingRequestID">
                <AlternatingRowStyle BackColor="White" />
                <Columns>
                    <asp:BoundField DataField="ViewingRequestID" HeaderText="Request ID" />
                    <asp:BoundField DataField="ParentName" HeaderText="ParentName" />
                    <asp:BoundField DataField="StudentName" HeaderText="StudentName" />
                    <asp:BoundField DataField="StudentID" HeaderText="StudentID" />
                    <asp:BoundField DataField="Status" HeaderText="Status" />
                    <asp:BoundField DataField="DateCreated" HeaderText="Date Created" />
                </Columns>
                <EmptyDataTemplate>
                    No record found!<br />
                </EmptyDataTemplate>
                <FooterStyle BackColor="#CCCC99" />
                <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
                <PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
                <RowStyle BackColor="#F7F7DE" />
                <SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
                <SortedAscendingCellStyle BackColor="#FBFBF2" />
                <SortedAscendingHeaderStyle BackColor="#848384" />
                <SortedDescendingCellStyle BackColor="#EAEAD3" />
                <SortedDescendingHeaderStyle BackColor="#575357" />
            </asp:GridView>
        &nbsp;</td>
    </tr>
    <tr>
        <td>
            <asp:Button ID="btn_ApproveViewRequest" runat="server" Text="Approve" Width="143px" OnClick="btn_ApproveViewRequest_Click" />
            <asp:Button ID="btn_RejectViewRequest" runat="server" Text="Reject" Width="143px" OnClick="btn_ApproveViewRequest_Click" />
        </td>
    </tr>
</table>
</asp:Content>

Aspx.cs文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using web_assignment_P05_group2;

namespace webAssignment_P05_Group2
{
    public partial class ViewRequests : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                 displayParentReq();
            }
        }

        protected void displayParentReq()
        {
             string strConn = ConfigurationManager.ConnectionStrings["StudentEPortfolioConnectionString"].ToString();
             // Instantiate a SqlCOnnection object with the connection string read.
             SqlConnection conn = new SqlConnection(strConn);
             SqlCommand cmd = new SqlCommand("SELECT ViewingRequestID, ParentName, StudentName," +
                                             "StudentID, Status, DateCreated" +
                                             " FROM ViewingRequest INNER JOIN Parent" +
                                             " ON ViewingRequest.ParentID = Parent.ParentID" +
                                             " ORDER BY DateCreated DESC", conn);

             SqlDataAdapter daRequest = new SqlDataAdapter(cmd);
             DataSet result = new DataSet();

             conn.Open();
             daRequest.Fill(result, "viewingRequest");
             conn.Close();

             gv_ParentRequests.DataSource = result.Tables["viewingRequest"];
             gv_ParentRequests.DataBind();
        }

        protected void gv_ParentRequests_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            // Set the page index to the page clicked by user.
            gv_ParentRequests.PageIndex = e.NewPageIndex;

           // display records on the new page.
           displayParentReq();
        }

        protected void gv_ParentRequests_SelectedIndexChanged(object sender, EventArgs e)
        {
            int selectedRequestNo = Convert.ToInt32(gv_ParentRequests.SelectedDataKey[0]);
            ParentRequests objRequest = new ParentRequests();//

            DataSet result = new DataSet();
            objRequest.viewrequestid = selectedRequestNo;
        }
     }
}

ParentRequest.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

namespace web_assignment_P05_group2
{
    public class ParentRequests
    {
        public int viewrequestid;
        public int parentid;
        public string studentname;
        public int studentid;
        public char status;
        public DateTime datecreated;

        public int ApproveReq(ref Dataset result)
        {
            string strConn = ConfigurationManager.ConnectionStrings
                             ["StudentEPortfolioConnectionString"].ToString();

            // Instantiate a SqlCOnnection object with the connection string read.
            SqlConnection conn = new SqlConnection(strConn);
            SqlCommand cmd = new SqlCommand("UPDATE ViewingRequest " +
        "SET Status = 'A' WHERE ViewingRequestID = @selectedViewingRequestID ", conn);

            SqlDataAdapter daRequest = new SqlDataAdapter(cmd);
            DataSet requests = new DataSet();

            conn.Open();
            daRequest.Fill(requests, "viewingRequest");
            conn.Close();

            return 0;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

尝试一下:

public int ApproveReq(ref Dataset result)
        {
            string strConn = ConfigurationManager.ConnectionStrings
                             ["StudentEPortfolioConnectionString"].ToString();

            // Instantiate a SqlCOnnection object with the connection string read.
            SqlConnection conn = new SqlConnection(strConn);
            SqlCommand cmd = new SqlCommand("UPDATE ViewingRequest " +
        "SET Status = 'A' WHERE ViewingRequestID = @selectedViewingRequestID ", conn);
cmd.parameters.add("@selectedViewingRequestID",Pass_selectedid);

            SqlDataAdapter daRequest = new SqlDataAdapter(cmd);
            DataSet requests = new DataSet();

            conn.Open();
            daRequest.Fill(requests, "viewingRequest");
            conn.Close();

            return 0;
        }