2017-04-03 93 views
0

在这里,我得到了一些代码,我尝试了很多方法,我只是能够通过使用Select查询将数据显示到我的表单中,但我无法更新它,它更新但它刚刚更新旧值不是新值i在文本框中ASP.NET C#MySql选择和更新表格

这里变的是形式

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

这里是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 selectqurey = "SELECT * FROM product WHERE id=" + @id; 
     MySqlCommand cmd = new MySqlCommand(selectqurey); 
     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(); 



     } 
    } 
} 
} 

他在这里的更新页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 [email protected] ,[email protected] ,[email protected] ,[email protected]_price ,[email protected] ,[email protected] ,[email protected] ,[email protected] ,[email protected] ,[email protected] 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(); 


    } 

} 

回答

0

你的按钮

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

需要一个OnClick setting,并在您的event handler代码隐藏cs文件来处理它。

Webforms/dot net与其他一些交互式网页框架的工作方式不同,因为大多数控件都回发到同一页面,而不是张贴到其他页面。所以,你不需要单独的更新页面,并且事实上在正常的事件处理过程中实际上不能使用它。

+0

我的错误其实是我的'OnClick =“button_click'在它,它只是给出这样的错误'你的SQL语法有错误;检查手册,对应于你的MariaDB服务器版本的正确语法使用附近'.WebControls.TextBox,car_model = System.Web.UI.WebControls.TextBox,UnitPrice = Sys'在第1行'而不是该框的值,它试图更新这些东西 – Fussionweb