简体   繁体   English

ASP.NET C#MySql选择并更新表单

[英]ASP.NET C# MySql Select and Update a form

Here i Got some code i tried lot of method, I just able to display the data into my form by using Select query but Am not able to update it, it updating but it just updating old value not new value i changed in textbox 在这里,我有一些代码,我尝试了很多方法,我只能使用Select查询将数据显示到表单中,但无法更新,它会更新,但它只会更新旧值,而不是我在文本框中更改的新值

Here is the form 这是表格

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Editcar.aspx.cs" MasterPageFile="MasterPage2.master" Inherits ="Editcar" %>


<asp:Content ID="formContent" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">



 <br />
 <br />
 <br />

<div class="row container">

 <form id="form1" runat="server" class="col-md-10" action="update.aspx" methode="post" >


    <asp:Table ID="GridView1" class="nav-justified" runat="server" AutoGenerateColumns="false" Height="628px" Width="763px">
        <asp:TableRow>
           <asp:TableCell>
                <h4> Car name:</h4>

           </asp:TableCell>
           <asp:TableCell>
                <asp:TextBox ID="id" runat="server" name="id" Width="301px" Text='<%# Eval("id") %>' Visible="False" CssClass="form-control"></asp:TextBox>
                <asp:TextBox ID="carmake" runat="server" Font-Names="carmake" Width="301px" Text='<%#  Eval("car_make") %>' CssClass="form-control"></asp:TextBox>

           </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow>
           <asp:TableCell>
                <h4> Car model:</h4>
           </asp:TableCell>
           <asp:TableCell>
                <asp:TextBox ID="carmodel" runat="server" name="carmodel" Text='<%#  Eval("car_model") %>' Width="301px" CssClass="form-control"></asp:TextBox>
           </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow>
           <asp:TableCell>
                <h4> Price: </h4>
           </asp:TableCell>
           <asp:TableCell>
                <asp:TextBox ID="price"  name="price" runat="server" Width="301px"  CssClass="form-control"></asp:TextBox>
           </asp:TableCell>
        </asp:TableRow>
        <asp:TableRow>
           <asp:TableCell>
                <h4> Discounted Price If: </h4>
           </asp:TableCell>
           <asp:TableCell>
                <asp:TextBox ID="d_price" name="d_price" runat="server" Width="301px"  CssClass="form-control"/>
           </asp:TableCell></asp:TableRow><asp:TableRow>
           <asp:TableCell> <h4>Car image (Type url)</h4></asp:TableCell><asp:TableCell>
                <asp:TextBox CssClass="form-control" ID="image"  name="image" runat="server" />Just Location
            </asp:TableCell></asp:TableRow><asp:TableRow>
           <asp:TableCell><h4>Avilability</h4></asp:TableCell><asp:TableCell>
                 <asp:TextBox CssClass="form-control" ID="avail"  name="avail" runat="server" />Just Location
           </asp:TableCell></asp:TableRow><asp:TableRow>
           <asp:TableCell><h4>Quantity</h4></asp:TableCell><asp:TableCell>
                 <asp:TextBox CssClass="form-control" ID="quantity"   name="quantity" runat="server" />Just Location
           </asp:TableCell></asp:TableRow><asp:TableRow>
           <asp:TableCell>
                <h4>Long description </h4>
           </asp:TableCell><asp:TableCell>

                <asp:TextBox ID="details" name="details" runat="server" Width="295px"  CssClass="form-control" Height="81px" TextMode="MultiLine"></asp:TextBox>
           </asp:TableCell></asp:TableRow><asp:TableRow>
           <asp:TableCell>
                <h4>Year </h4>
           </asp:TableCell><asp:TableCell>
                <asp:TextBox ID="year" name="year" runat="server" Width="295px"  CssClass="form-control" ></asp:TextBox>
           </asp:TableCell></asp:TableRow><asp:TableRow>
           <asp:TableCell>
                <h4>Special Discounted(0 0r 1) </h4>
           </asp:TableCell><asp:TableCell>
                <asp:TextBox ID="special" name="special"  runat="server" Width="295px"  CssClass="form-control"  ></asp:TextBox>
           </asp:TableCell></asp:TableRow></asp:Table><asp:Button ID="button"  runat="server"  Cssclass="btn btn-primary btn-lg btn-block" Text="Update the car" />


<br />
   <br />

   </form>


</div>
</asp:Content>

Here is the CS file 这是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 MySql.Data.MySqlClient;

public partial class Editcar : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;

        using (MySqlConnection con = new MySqlConnection(constr))
    {
        var id = Request.QueryString["id"];
        string selectquery = "SELECT * FROM product WHERE id=" + @id;
        MySqlCommand cmd = new MySqlCommand(selectquery);
        cmd.Connection = con;
        cmd.CommandType = CommandType.Text;
        con.Open();
        cmd.ExecuteNonQuery();

        MySqlDataAdapter da = new MySqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);

        foreach (DataRow dr in dt.Rows)

        {

            id = dr["id"].ToString();
            carmake.Text = dr["car_make"].ToString();
            carmodel.Text = dr["car_model"].ToString();
            price.Text = dr["unitprice"].ToString();
            d_price.Text = dr["discountprice"].ToString();
            image.Text = dr["image"].ToString();
            quality.Text = dr["quantity"].ToString();
            avil.Text = dr["availability"].ToString();
            details.Text = dr["details"].ToString();
            year.Text = dr["year"].ToString();
            special.Text = dr["special"].ToString();



        }
    }
}
}

Here his the update page cs 这是他的更新页面

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using MySql.Data.MySqlClient;

public partial class AdminGroup_Update: System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        string constor = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        MySqlConnection conn = new MySqlConnection(constor);
        var id = Request.QueryString["id"];
        var carmake = Request.QueryString["carmake"];
        var carmodel = Request.QueryString["carmodel"];
        var price = Request.QueryString["price"];
        var d_price = Request.QueryString["d_price"];
        var image = Request.QueryString["image"];
        var quantity = Request.QueryString["quantity"];
        var avail = Request.QueryString["avail"];
        var details = Request.QueryString["details"];
        var year = Request.QueryString["year"];
        var special = Request.QueryString["special"];

        string sql = "Update product SET car_make=@carmake ,car_model=@carmodel ,UnitPrice=@price ,Discountprice=@d_price ,image=@image ,Quantity=@quality ,availability=@avil ,details=@details ,year=@year ,special=@special  WHERE id= @id";



        var cmd = new MySqlCommand(sql, conn);


            conn.Open();
            cmd.CommandType = CommandType.Text;
            var param = new MySqlParameter[10];

            param[0] = new MySqlParameter("@carmake", MySqlDbType.VarChar, 100);
            param[1] = new MySqlParameter("@carmodel", MySqlDbType.VarChar, 100);
            param[2] = new MySqlParameter("@price", MySqlDbType.VarChar, 100);
            param[3] = new MySqlParameter("@d_price", MySqlDbType.VarChar, 100); // put zero if no discount
            param[4] = new MySqlParameter("@image", MySqlDbType.VarChar, 300);
            param[5] = new MySqlParameter("@quantity", MySqlDbType.VarChar, 300);
            param[6] = new MySqlParameter("@avail", MySqlDbType.VarChar, 2);
            param[7] = new MySqlParameter("@details", MySqlDbType.VarChar, 2000);
            param[8] = new MySqlParameter("@year", MySqlDbType.VarChar, 4);
            param[9] = new MySqlParameter("@special", MySqlDbType.VarChar, 2);
            param[10] = new MySqlParameter("@id", MySqlDbType.VarChar, 2);

            param[0].Value = carmake;
            param[1].Value = carmodel;
            param[2].Value = price;
            param[3].Value = d_price;
            param[4].Value = image;
            param[5].Value = quality;
            param[6].Value = avil;
            param[7].Value = details;
            param[8].Value = year;
            param[9].Value = special;
            param[10].Value =id;

            var ex = cmd.ExecuteNonQuery();

            if (ex == 1)
            {
                Response.Redirect("AdminList.aspx");
            }
            else
            {
                Response.Write("Error");
            }
            conn.Close();


    }

}

Your button 您的按钮

 <asp:Button ID="button"  runat="server"  
             Cssclass="btn btn-primary btn-lg btn-block" 
             Text="Update the car" />

needs an OnClick setting and an event handler in your code-behind .cs file to handle it. 在您的代码隐藏.cs文件中需要一个OnClick设置和一个事件处理程序来进行处理。

Webforms / dot net works differently from some other interactive web page frameworks in that most controls post back to the same page rather than posting to another page. Webforms /点网的工作方式不同于其他一些交互式网页框架,因为大多数控件都发回到同一页而不是发回到另一页。 So, you don't need a separate update page, and indeed won't be able to use it in the normal course of event processing. 因此,您不需要单独的更新页面,并且确实无法在事件处理的正常过程中使用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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