Here I got a web form I used select Query and displayed it a form and i need it to update using Query I tried lost of method. I couldn't update,it just updating but passing the old value instead of new changed value.
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">
<form id="form1" runat="server" class="col-md-10" 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" OnClick="button_click" runat="server" Cssclass="btn btn-primary btn-lg btn-block" Text="Update the car" />
</form>
HERE is the code fore cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
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)
{
newid.Text = 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();
quantity.Text = dr["quantity"].ToString();
avail.Text = dr["availability"].ToString();
details.Text = dr["details"].ToString();
year.Text = dr["year"].ToString();
special.Text = dr["special"].ToString();
}
}
}
public void button_click(object sender, EventArgs e)
{
string constor = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
MySqlConnection conn = new MySqlConnection(constor);
string sql = "Update product SET car_make=@carmake, car_model=@carmodel, UnitPrice=@price, Discountprice=@d_price, image=@image, Quantity=@quantity, availability=@avail, details=@details, year=@year, special=@special WHERE id= @id";
var cmd = new MySqlCommand(sql, conn);
conn.Open();
cmd.Parameters.AddWithValue("@id", newid.Text);
cmd.Parameters.AddWithValue("@carmake", carmake.Text);
cmd.Parameters.AddWithValue("@carmodel", carmodel.Text);
cmd.Parameters.AddWithValue("@price", price.Text);
cmd.Parameters.AddWithValue("@d_price", d_price.Text); // put zero if no discount
cmd.Parameters.AddWithValue("@image", image.Text);
cmd.Parameters.AddWithValue("@quantity", quantity.Text);
cmd.Parameters.AddWithValue("@avail", avail.Text);
cmd.Parameters.AddWithValue("@details", details.Text);
cmd.Parameters.AddWithValue("@year", year.Text);
cmd.Parameters.AddWithValue("@special", special.Text);
cmd.Parameters.AddWithValue("@id", newid.Text);
var ex = cmd.ExecuteNonQuery();
if (ex == 1)
{
Response.Redirect("AdminList.aspx");
}
else
{
Response.Write("Error");
}
conn.Close();
}
}
It's Just updating but not getting the table value
I used ajax too it didn't work
cmd.Parameters.Add(param[0]);for instance, and repeat for each parameter (or directly declare the new parameter object inside the Add brackets, for brevity). Repeat for all your required parameters. Or evencmd.Parameters.AddWithValue()could save you even more code, then you just specify the name and value and let .NET infer the data type.