简体   繁体   中英

Change SQL data gridview upon button click

I am trying to change the data of status in my SQL shown in my gridview but I am unable to do it. What I want to do here is to be able to change the value of status of selected row to 'A' whenever the approve button is pressed. May I know how to do it?

Here is a preview of my interface and further down is my code :

在此处输入图片说明

Aspx file :

<%@ 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 file:

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;
        }
    }
}

Try this:

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;
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM