gpt4 book ai didi

c# - ASP.NET C# MySql 选择和更新表单

转载 作者:行者123 更新时间:2023-11-29 19:09:41 25 4
gpt4 key购买 nike

这里我得到了一些代码,我尝试了很多方法,我只能使用选择查询将数据显示到我的表单中,但我无法更新它,它更新但它只是更新旧值而不是我更改的新值文本框

这是表格

 <%@ 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 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();



}
}
}
}

这是更新页面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();


}

}

最佳答案

你的按钮

 <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 的工作方式与其他一些交互式网页框架不同,因为大多数控件都会发布回同一页面,而不是发布到另一个页面。因此,您不需要单独的更新页面,并且实际上无法在正常的事件处理过程中使用它。

关于c# - ASP.NET C# MySql 选择和更新表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43185726/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com