The update command cannot operate in existing rows in database using System;using System.Collections.Generic;using System.Linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Data.SqlClient;using System.Web.UI.HtmlControls;using System.Configuration;using System.Collections;namespace WebApplication21{ public partial class WebForm2 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { SqlConnection MyConn; SqlCommand MyCmd; SqlDataReader MyRdr; String MyString = "Data Source= .\\SQLEXPRESS; AttachDbFilename=|DataDirectory|\\Databasebest.mdf; Integrated Security=true; User Instance=true"; MyConn = new SqlConnection(MyString); if (!IsPostBack) { MyConn.Open(); MyCmd = new SqlCommand("SELECT * FROM Catalogue", MyConn); MyRdr = MyCmd.ExecuteReader(); DropDownList1.DataSource = MyRdr; DropDownList1.DataValueField = "Price"; DropDownList1.DataTextField = "Code"; DropDownList1.DataBind(); MyRdr.Close(); } } protected void rebind() { } protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e) { SqlConnection MyConn; SqlCommand MyCmd; SqlDataReader MyRdr; String MyString = "Data Source= .\\SQLEXPRESS; AttachDbFilename=|DataDirectory|\\Databasebest.mdf; Integrated Security=true; User Instance=true"; MyConn = new SqlConnection(MyString); MyCmd = new SqlCommand("SELECT * FROM Catalogue WHERE [Code] LIKE @Code", MyConn); MyCmd.Parameters.AddWithValue("@Code", DropDownList1.SelectedItem.Value); MyConn.Open(); MyRdr = MyCmd.ExecuteReader(); while (MyRdr.Read()) { product_NamTB.Text = (string)MyRdr["Product_Name"]; pricTB.Text = (string)MyRdr["Price"]; } MyRdr.Close(); MyConn.Close(); } protected void Button1_Click(object sender, EventArgs e) { SqlConnection MyConn; SqlCommand MyCmd; String MyString = "Data Source= .\\SQLEXPRESS; AttachDbFilename=|DataDirectory|\\Databasebest.mdf; Integrated Security=true; User Instance=true"; MyConn = new SqlConnection(MyString); MyCmd = new SqlCommand("INSERT INTO Catalogue (Product_Name, Code, Price) VALUES (@Product_Name, @Code, @Price)", MyConn);// update the command with the parameters from the text box’s MyCmd.Parameters.AddWithValue("@product_Name", product_NameTB.Text); MyCmd.Parameters.AddWithValue("@Code", CodeTB.Text); MyCmd.Parameters.AddWithValue("@Price", PriceTB.Text); MyConn.Open(); // open the connection// run the query, this method is used as it does not return a value MyCmd.ExecuteNonQuery(); MyConn.Close(); // close the connection } protected void Button2_Click(object sender, EventArgs e) { SqlConnection MyConn; SqlCommand MyCmd; String MyString = "Data Source= .\\SQLEXPRESS; AttachDbFilename=|DataDirectory|\\Databasebest.mdf; Integrated Security=true; User Instance=true"; MyConn = new SqlConnection(MyString); MyCmd = new SqlCommand("UPDATE Catalogue SET [Product_Name] =@Product_Name, [Price] =@Price WHERE Code LIKE @Code", MyConn); MyCmd.Parameters.AddWithValue("@code", DropDownList1.SelectedItem.Value); MyCmd.Parameters.AddWithValue("@Product_Name", product_NamTB.Text); MyCmd.Parameters.AddWithValue("@Price", pricTB.Text); MyConn.Open(); MyCmd.ExecuteNonQuery(); MyConn.Close(); Response.Redirect("WebForm2.aspx"); } protected void Button3_Click(object sender, EventArgs e) { SqlConnection MyConn; SqlCommand MyCmd; String MyString = "Data Source= .\\SQLEXPRESS; AttachDbFilename=|DataDirectory|\\Databasebest.mdf; Integrated Security=true; User Instance=true"; MyConn = new SqlConnection(MyString); MyCmd = new SqlCommand("DELETE FROM Catalogue WHERE [Code] LIKE @Code ", MyConn); MyCmd.Parameters.AddWithValue("@code", DropDownList1.SelectedItem.Value); MyConn.Open(); // run the query, this method is used as it does not return a value MyCmd.ExecuteNonQuery(); MyConn.Close(); } }}
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication21.WebForm2" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head runat="server"><title></title></head><body> <form id="form1" runat="server"> <div> Product_Name<br /> <asp:TextBox ID="product_NameTB" runat="server" > </asp:TextBox> <br />Code<br /> <asp:TextBox ID="CodeTB" runat="server" > </asp:TextBox> <br />Price<br /> <asp:TextBox ID="PriceTB" runat="server" > </asp:TextBox> <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Add" /> <br /> <br /> <br /> <asp:DropDownList ID="DropDownList1" runat="server" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"> </asp:DropDownList> <br />Product_Name<br /> <asp:TextBox ID="product_NamTB" runat="server"></asp:TextBox> <br />Price<br /> <asp:TextBox ID="pricTB" runat="server"></asp:TextBox> <asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Update" /> <br /> <br /> <br /> <asp:Button ID="Button3" runat="server" OnClick="Button3_Click" Text="Delete" /> </div> </form></body></html>
Dimi