 |
| ASP.NET 1.0 and 1.1 Professional For advanced ASP.NET 1.x coders. Beginning-level questions will be redirected to other forums. NOT for "classic" ASP 3 or the newer ASP.NET 2.0 and 3.5 |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.0 and 1.1 Professional section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

June 20th, 2007, 03:39 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Well
You will also have to add the event for Datagrid's EditCommand so that it can be determined which row is being updated, also do not forget to bind the grid in this event too, e.g. as below:
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
BindData();
}
Regards
Mike
Don't expect too much, too soon.
|
|

June 20th, 2007, 04:04 AM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I have already added code in DataGrid1_EditCommand. Still chages are not updated in the grid as well as database.
Thanks
Sams
|
|

June 20th, 2007, 04:18 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
|
|
ok, can u post the entire HTML and Codebehind code here so that we can check.....
Regards
Mike
Don't expect too much, too soon.
|
|

June 20th, 2007, 04:31 AM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
The code is shown below:
* * * *
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.Configuration ;
using System.Data.SqlClient;
using System.Configuration;
namespace Practice
{
/// <summary>
/// Summary description for WebForm2.
/// </summary>
public class WebForm2 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
if(!Page.IsPostBack)
{
BindData();
}
}
public void BindData()
{
string connstr = (string)ConfigurationSettings.AppSettings["ConString"];
SqlConnection con = new SqlConnection();
con.ConnectionString=connstr;
con.Open();
SqlCommand myCommand = new SqlCommand("select_prod", con);
myCommand.CommandType = CommandType.StoredProcedure;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
DataSet ds = new DataSet();
myAdapter.Fill(ds,"prod");
//con.Open();
int n=myCommand.ExecuteNonQuery();
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
con.Close();
}
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_UpdateCommand);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHand ler(this.DataGrid1_DeleteCommand);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex= e.Item.ItemIndex ;
BindData();
}
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex=-1;
BindData();
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
TextBox cname=new TextBox();
cname=(TextBox) e.Item.Cells[1].Controls[0];
string connstr = (string)ConfigurationSettings.AppSettings["ConString"];
SqlConnection con = new SqlConnection();
con.ConnectionString=connstr;
SqlCommand myCommand = new SqlCommand("update_prod", con);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add(new SqlParameter("@ProductName",SqlDbType.NVarChar,50) );
myCommand.Parameters["@ProductName"].Value = cname.Text;
con.Open();
myCommand.ExecuteNonQuery();
con.Close();
DataGrid1.EditItemIndex=-1;
BindData();
}
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string connstr = (string)ConfigurationSettings.AppSettings["ConString"];
SqlConnection con = new SqlConnection();
con.ConnectionString=connstr;
con.Open();
int s = Convert.ToInt16(e.Item.Cells[0].Text);
SqlCommand myCommand = new SqlCommand("DELETE FROM products WHERE productid="+s,con);
myCommand.CommandType = CommandType.Text;
myCommand.ExecuteNonQuery();
con.Close();
// Binds the grid so that change can be seen
BindData();
}
}
}
* * * *
The HTML code is shown below:
* * * *
<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="Practice.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm2</title>
<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
<meta content="C#" name="CODE_LANGUAGE">
<meta content="JavaScript" name="vs_defaultClientScript">
<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<asp:datagrid id="DataGrid1" style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 27px" runat="server"
GridLines="None" CellPadding="3" BackColor="White" BorderWidth="2px" CellSpacing="1" BorderStyle="Ridge"
BorderColor="White" Height="268px" Width="432px" AutoGenerateColumns="False">
<SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#9471DE"></SelectedItemStyle>
<ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#E7E7FF" BackColor="#4A3C8C"></HeaderStyle>
<FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
<Columns>
<asp:BoundColumn HeaderText="Product Id" DataField="ProductID"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Name" DataField="ProductName"></asp:BoundColumn>
<asp:BoundColumn HeaderText="Price" DataField="UnitPrice"></asp:BoundColumn>
<asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
<asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn>
</Columns>
<PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#C6C3C6"></PagerStyle>
</asp:datagrid></form>
</body>
</HTML>
* * * *
Please help me. :(
Thanks
|
|

June 20th, 2007, 05:20 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Ok, look at the stored procedure that you have created, you have written:
UPDATE products SET ProductName = @ProductName WHERE ProductName = @ProductName
which means that you sending the new updated product name to the parameter @ProductName. e.g. if you have changed the product name "Computer" to "Laptop", the SP will try to update
UPDATE products SET ProductName = 'Laptop' WHERE ProductName = 'Laptop'
But there is no row in the DB with the name 'Laptop', the row in database is still 'Computer'???
This won't be a good approach, so what you can do is send the ProductID for the product and update on the ID basis, e.g. modify your SP as below:
ALTER PROCEDURE SP_UpdateProd
@ProductID int,
@ProductName nvarchar(50)
AS
UPDATE products SET ProductName = @ProductName WHERE ProductID = @ProductID
To fetch the productid from the datagrid, use the DataKeyField property of datagrid like below:
<asp:datagrid id="DataGrid1" AutoGenerateColumns="False" Runat="server" DataKeyField="ProductID">.
This can later be fetched in the DataGrid1_UpdateCommand event as marked in Red Bold color as below:
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
TextBox cname=new TextBox();
cname=(TextBox) e.Item.Cells[1].Controls[0];
int ProductID = Convert.ToInt32(DataGrid1.DataKeys[e.Item.ItemIndex]);
string connstr = (string)ConfigurationSettings.AppSettings["ConnectionString"];
SqlConnection con = new SqlConnection();
con.ConnectionString=connstr;
SqlCommand myCommand = new SqlCommand("SP_UpdateProd", con);
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.Add(new SqlParameter("@ProductName",SqlDbType.NVarChar,50) );
myCommand.Parameters["@ProductName"].Value = cname.Text;
myCommand.Parameters.Add(new SqlParameter("@ProductID",SqlDbType.Int,8));
myCommand.Parameters["@ProductID"].Value = ProductID;
con.Open();
myCommand.ExecuteNonQuery();
con.Close();
DataGrid1.EditItemIndex=-1;
BindData();
}
Regards
Mike
Don't expect too much, too soon.
|
|

June 20th, 2007, 06:46 AM
|
|
Authorized User
|
|
Join Date: Jun 2007
Posts: 22
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
Thanks alot for solving my problem by sending me the corrections required.
Sams
|
|

June 20th, 2007, 07:19 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2004
Posts: 550
Thanks: 0
Thanked 1 Time in 1 Post
|
|
Glad I could be of help!!!
Regards
Mike
Don't expect too much, too soon.
|
|
 |