Asp.net updates the specified record implementation program

Source: Internet
Author: User

Let's first look at the html page

The code is as follows: Copy code

<% @ 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 no." HeaderText = "product no."/>
<Asp: BoundField DataField = "product name" HeaderText = "product name"/>
<Asp: BoundField DataField = "item quantity" HeaderText = "item quantity"/>
<Asp: BoundField DataField = "product price" HeaderText = "product price"/>
<Asp: HyperLinkField DataNavigateUrlFields = "product no." DataNavigateUrlFormatString = "Default. aspx? Item No. = {0 }"
HeaderText = "update" Text = "update"/>
</Columns>
<FooterStyle BackColor = "# 5D7B9D" Font-Bold = "True" ForeColor = "White"/>
<RowStyle BackColor = "# F7F6F3" ForeColor = "#333333"/>
<EditRowStyle BackColor = "#999999" type = "regxph" text = "yourobjectname"/>
<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 = "item quantity:"> </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 = "product 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>

The data submitted on the preceding page is accepted and then updated using SQL.

The code is as follows: Copy code

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) // when the Page is executed for the first time
        {
GridViewBind (); // bind the custom method GridViewBind
If (Request. QueryString ["item no."]! = Null) // judge. If you can obtain the id value, perform the following operations:
            {
SqlConnection con = new SqlConnection (ConfigurationSettings. Deleettings ["strCon"]);
Con. Open ();
SqlDataAdapter ada = new SqlDataAdapter ("select * from tb_shopping05 where Item No. =" + Request. QueryString ["item no."] + "", con );
DataSet ds = new DataSet ();
Ada. Fill (ds, "tb_shopping05 ");
DataRowView drv = ds. Tables ["tb_shopping05"]. DefaultView [0];
This. TxtName. Text = drv ["product name"]. ToString ();
This. TxtNum. Text = drv ["item quantity"]. ToString ();
This. TxtPrice. Text = drv ["product unit price"]. ToString ();
            }
        }
    }
Public void GridViewBind () // custom method for binding the GridView control
    {
SqlConnection con = new SqlConnection (ConfigurationSettings. Deleettings ["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. Deleettings ["strCon"]);
Con. Open ();
SqlCommand com = new SqlCommand ("update tb_shopping05 set item name = '" + this. txtName. text + "', item quantity ='" + this. txtNum. text + "', product unit price ='" + this. txtPrice. text + "'Where product No. =" + Request ["product no."], con );
Com. ExecuteNonQuery ();
GridViewBind ();
Response. Write ("<script language = javascript> alert ('Congratulations! Information updated! ') </Script> ");
        }
Catch
        {
Response. Write ("<script language = javascript> alert ('Sorry! An error occurred while updating the information! ') </Script> ");
        }
    }
}

The principle is that, when we click on the edited data, an ID will be sent, and then we can use SQL to update the accepted data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.