asp. net Updates the method for a specified record

  • 2021-01-22 04:59:07
  • OfStack

This article illustrates how asp.net updates a specified record. Share with you for your reference. The specific methods are as follows:

Let's start with the html page:

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 <form id="form1" runat="server">
        &nbsp;
        <div style="text-align: center">
            <table style="width: 302px; height: 246px;">
                <tr>
                    <td colspan="2" style="width: 496px">
                        <asp:Label ID="Label2" runat="server" Text=" Update specified data " Font-Bold="True" ForeColor="Blue" Width="132px"></asp:Label></td>
                </tr>
                <tr>
                    <td colspan="2" style="width: 496px">
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" Font-Size="Smaller" ForeColor="#333333" GridLines="None">
            <Columns>
                <asp:BoundField DataField=" Product id " HeaderText=" Product id " />
                <asp:BoundField DataField=" Name of commodity " HeaderText=" Name of commodity " />
                <asp:BoundField DataField=" The number " HeaderText=" The number " />
                <asp:BoundField DataField=" Commodity price " HeaderText=" Commodity price " />
                <asp:HyperLinkField DataNavigateUrlFields=" Product id " DataNavigateUrlFormatString="Default.aspx? Product id ={0}"
                    HeaderText=" update " Text=" update " />
            </Columns>
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <EditRowStyle BackColor="#999999" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>
                    </td>
                </tr>
                <tr>
                    <td colspan="2" style="width: 496px" align="center">
                        &nbsp;</td>
                </tr>
                <tr>
                    <td colspan="2" style="width: 496px">
                        <asp:Label ID="Label3" runat="server" Font-Size="Smaller" Text=" Product Name: " Width="65px"></asp:Label><asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
                    </td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Label ID="Label4" runat="server" Font-Size="Smaller" Text=" Quantity of Goods: "></asp:Label>
        <asp:TextBox ID="TxtNum" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td colspan="2">
                        <asp:Label ID="Label5" runat="server" Font-Size="Smaller" Text=" Unit price: "></asp:Label>
        <asp:TextBox ID="TxtPrice" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td colspan="2" style="width: 496px">
                        &nbsp;<asp:Button ID="BtnUpdate" runat="server" OnClick="BtnUpdate_Click" Text=" update " Width="55px" /></td>
                </tr>
            </table>
        </div>
    </form>

We accept the data submitted from the above page and then perform the database update using sql
View Code
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)// The first time the page is executed
        {
            GridViewBind();// Bind custom methods GridViewBind
            if (Request.QueryString[" Product id "] != null)// Determine if it is available id Value of, do the following
            {
                SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
                con.Open();
                SqlDataAdapter ada = new SqlDataAdapter("select * from tb_shopping05 where Product id =" + Request.QueryString[" Product id "] + "", con);
                DataSet ds = new DataSet();
                ada.Fill(ds, "tb_shopping05");
                DataRowView drv = ds.Tables["tb_shopping05"].DefaultView[0];
                this.TxtName.Text = drv[" Name of commodity "].ToString();
                this.TxtNum.Text = drv[" The number "].ToString();
                this.TxtPrice.Text = drv[" Commodity price "].ToString();
            }
        }
    }
    public void GridViewBind()// The binding GridView Control the custom method
    {
        SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
        con.Open();
        SqlDataAdapter ada = new SqlDataAdapter("select * from tb_shopping05", con);
        DataSet ds = new DataSet();
        ada.Fill(ds);
        GridView1.DataSource = ds;
        GridView1.DataBind();
        con.Close();
    }
    protected void BtnUpdate_Click(object sender, EventArgs e)
    {
        try
        {
            SqlConnection con = new SqlConnection(ConfigurationSettings.AppSettings["strCon"]);
            con.Open();
            SqlCommand com = new SqlCommand("update tb_shopping05 set Name of commodity ='" + this.TxtName.Text + "', The number ='" + this.TxtNum.Text + "', Commodity price ='" + this.TxtPrice.Text + "' where Product id =" + Request[" Product id "], con);
            com.ExecuteNonQuery();
            GridViewBind();
            Response.Write("<script language=javascript>alert(' Congratulations to you! Information updated successfully! ')</script>");
        }
        catch
        {
            Response.Write("<script language=javascript>alert(' I regret! Message update failed! ')</script>");
        }
    }
}

The principle is like this, when we click on the edited data, an ID will be sent to us, and then we can use sql to carry out update on the received data.

I hope this article is helpful to the asp.net program design.


Related articles: