I have a web page coded in c# and asp.net. On that page I have a
datagrid with and edit button link. I can click the edit button and
it selects the corret record, and even cancels correctly. However
when I enter some new information and click the update link, it
returns the old information and not the newly entered information.
Could you please take a look at the following code and see if you
could help? Thanks, and here is the code:
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;
namespace KcMedJobs
{
public class Customers : System.Web.UI.Page
{
protected System.Web.UI.WebControls.DataGrid dgCustomer;
protected System.Data.SqlClient.SqlCommand sqlSelectCommand1;
protected System.Data.SqlClient.SqlCommand sqlInsertCommand1;
protected System.Data.SqlClient.SqlCommand sqlUpdateCommand1;
protected System.Data.SqlClient.SqlCommand sqlDeleteCommand1;
protected System.Data.SqlClient.SqlConnection sqlConnection1;
protected System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
protected KcMedJobs.dsCustomer dsCustomer1;
protected System.Web.UI.WebControls.Label lblMessage;
private void Page_Load(object sender, System.EventArgs e)
{
sqlDataAdapter1.Fill(dsCustomer1);
dgCustomer.DataBind();
}
#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.sqlSelectCommand1 = new
System.Data.SqlClient.SqlCommand();
this.sqlInsertCommand1 = new
System.Data.SqlClient.SqlCommand();
this.sqlUpdateCommand1 = new
System.Data.SqlClient.SqlCommand();
this.sqlDeleteCommand1 = new
System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new
System.Data.SqlClient.SqlConnection();
this.sqlDataAdapter1 = new
System.Data.SqlClient.SqlDataAdapter();
this.dsCustomer1 = new KcMedJobs.dsCustomer();
((System.ComponentModel.ISupportInitialize)(this.dsCustomer1)).BeginInit();
this.dgCustomer.CancelCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgCustomer_CancelCommand);
this.dgCustomer.EditCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgCustomer_EditCommand);
this.dgCustomer.UpdateCommand += new
System.Web.UI.WebControls.DataGridCommandEventHandler(this.dgCustomer_UpdateCommand);
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT
CustomerId, FirstName,
LastName, CompanyName, EmailAddress, StreetAddress," +
" City, State, Zip, Phone, CustomerPassword
FROM tblCustomers";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = @"INSERT INTO
tblCustomers(FirstName, LastName, CompanyName, EmailAddress,
StreetAddress, City, State, Zip, Phone, CustomerPassword) VALUES
(@FirstName, @LastName, @CompanyName, @EmailAddress, @StreetAddress,
@City, @State, @Zip, @Phone, @CustomerPassword); SELECT CustomerId,
FirstName, LastName, CompanyName, EmailAddress, StreetAddress, City,
State, Zip, Phone, CustomerPassword FROM tblCustomers WHERE
(CustomerId = @@IDENTITY)";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@FirstName",
System.Data.SqlDbType.NVarChar, 50, "FirstName"));
this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@LastName",
System.Data.SqlDbType.NVarChar, 50, "LastName"));
this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@CompanyName",
System.Data.SqlDbType.NVarChar, 50, "CompanyName"));
this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@EmailAddress",
System.Data.SqlDbType.NVarChar, 100, "EmailAddress"));
this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@StreetAddress",
System.Data.SqlDbType.NVarChar, 150, "StreetAddress"));
this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@City",
System.Data.SqlDbType.NVarChar, 50, "City"));
this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@State",
System.Data.SqlDbType.NVarChar, 50, "State"));
this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Zip",
System.Data.SqlDbType.NVarChar, 50, "Zip"));
this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Phone",
System.Data.SqlDbType.NVarChar, 50, "Phone"));
this.sqlInsertCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@CustomerPassword",
System.Data.SqlDbType.NVarChar, 50, "CustomerPassword"));
//
// sqlUpdateCommand1
//
this.sqlUpdateCommand1.CommandText = @"UPDATE
tblCustomers SET
FirstName = @FirstName, LastName = @LastName, CompanyName =
@CompanyName, EmailAddress = @EmailAddress, StreetAddress =
@StreetAddress, City = @City, State = @State, Zip = @Zip, Phone =
@Phone, CustomerPassword = @CustomerPassword WHERE (CustomerId =
@Original_CustomerId) AND (City = @Original_City) AND (CompanyName =
@Original_CompanyName) AND (CustomerPassword =
@Original_CustomerPassword) AND (EmailAddress =
@Original_EmailAddress) AND (FirstName = @Original_FirstName) AND
(LastName = @Original_LastName) AND (Phone = @Original_Phone) AND
(State = @Original_State) AND (StreetAddress =
@Original_StreetAddress) AND (Zip = @Original_Zip); SELECT CustomerId,
FirstName, LastName, CompanyName, EmailAddress, StreetAddress, City,
State, Zip, Phone, CustomerPassword FROM tblCustomers WHERE
(CustomerId = @CustomerId)";
this.sqlUpdateCommand1.Connection = this.sqlConnection1;
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@FirstName",
System.Data.SqlDbType.NVarChar, 50, "FirstName"));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@LastName",
System.Data.SqlDbType.NVarChar, 50, "LastName"));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@CompanyName",
System.Data.SqlDbType.NVarChar, 50, "CompanyName"));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@EmailAddress",
System.Data.SqlDbType.NVarChar, 100, "EmailAddress"));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@StreetAddress",
System.Data.SqlDbType.NVarChar, 150, "StreetAddress"));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@City",
System.Data.SqlDbType.NVarChar, 50, "City"));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@State",
System.Data.SqlDbType.NVarChar, 50, "State"));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Zip",
System.Data.SqlDbType.NVarChar, 50, "Zip"));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Phone",
System.Data.SqlDbType.NVarChar, 50, "Phone"));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@CustomerPassword",
System.Data.SqlDbType.NVarChar, 50, "CustomerPassword"));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_CustomerId",
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerId",
System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_City",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "City", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_CompanyName",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "CompanyName",
System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_CustomerPassword",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "CustomerPassword",
System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_EmailAddress",
System.Data.SqlDbType.NVarChar, 100,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "EmailAddress",
System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_FirstName",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "FirstName", System.Data.DataRowVersion.Original,
null));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_LastName",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "LastName", System.Data.DataRowVersion.Original,
null));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_Phone",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "Phone", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_State",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "State", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_StreetAddress",
System.Data.SqlDbType.NVarChar, 150,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "StreetAddress",
System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_Zip",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "Zip", System.Data.DataRowVersion.Original, null));
this.sqlUpdateCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@CustomerId",
System.Data.SqlDbType.Int, 4, "CustomerId"));
//
// sqlDeleteCommand1
//
this.sqlDeleteCommand1.CommandText = @"DELETE FROM
tblCustomers
WHERE (CustomerId = @Original_CustomerId) AND (City = @Original_City)
AND (CompanyName = @Original_CompanyName) AND (CustomerPassword =
@Original_CustomerPassword) AND (EmailAddress =
@Original_EmailAddress) AND (FirstName = @Original_FirstName) AND
(LastName = @Original_LastName) AND (Phone = @Original_Phone) AND
(State = @Original_State) AND (StreetAddress =
@Original_StreetAddress) AND (Zip = @Original_Zip)";
this.sqlDeleteCommand1.Connection = this.sqlConnection1;
this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_CustomerId",
System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input,
false, ((System.Byte)(0)), ((System.Byte)(0)), "CustomerId",
System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_City",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "City", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_CompanyName",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "CompanyName",
System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_CustomerPassword",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "CustomerPassword",
System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_EmailAddress",
System.Data.SqlDbType.NVarChar, 100,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "EmailAddress",
System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_FirstName",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "FirstName", System.Data.DataRowVersion.Original,
null));
this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_LastName",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "LastName", System.Data.DataRowVersion.Original,
null));
this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_Phone",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "Phone", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_State",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "State", System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_StreetAddress",
System.Data.SqlDbType.NVarChar, 150,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "StreetAddress",
System.Data.DataRowVersion.Original, null));
this.sqlDeleteCommand1.Parameters.Add(new
System.Data.SqlClient.SqlParameter("@Original_Zip",
System.Data.SqlDbType.NVarChar, 50,
System.Data.ParameterDirection.Input, false, ((System.Byte)(0)),
((System.Byte)(0)), "Zip", System.Data.DataRowVersion.Original, null));
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "workstation
id=HOME;packet
size=4096;integrated security=SSPI;data source=HOME;pe" +
"rsist security info=False;initial
catalog=KcWeb";
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.DeleteCommand =
this.sqlDeleteCommand1;
this.sqlDataAdapter1.InsertCommand =
this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand =
this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new
System.Data.Common.DataTableMapping[] {
new
System.Data.Common.DataTableMapping("Table", "tblCustomers", new
System.Data.Common.DataColumnMapping[] {
new
System.Data.Common.DataColumnMapping("CustomerId", "CustomerId"),
new
System.Data.Common.DataColumnMapping("FirstName", "FirstName"),
new
System.Data.Common.DataColumnMapping("LastName", "LastName"),
new
System.Data.Common.DataColumnMapping("CompanyName", "CompanyName"),
new
System.Data.Common.DataColumnMapping("EmailAddress", "EmailAddress"),
new
System.Data.Common.DataColumnMapping("StreetAddress", "StreetAddress"),
new
System.Data.Common.DataColumnMapping("City", "City"),
new
System.Data.Common.DataColumnMapping("State", "State"),
new
System.Data.Common.DataColumnMapping("Zip", "Zip"),
new
System.Data.Common.DataColumnMapping("Phone", "Phone"),
new
System.Data.Common.DataColumnMapping("CustomerPassword",
"CustomerPassword")})});
this.sqlDataAdapter1.UpdateCommand =
this.sqlUpdateCommand1;
//
// dsCustomer1
//
this.dsCustomer1.DataSetName = "dsCustomer";
this.dsCustomer1.Locale = new
System.Globalization.CultureInfo("en-US");
this.Load += new System.EventHandler(this.Page_Load);
((System.ComponentModel.ISupportInitialize)(this.dsCustomer1)).EndInit();
}
#endregion
private void dgCustomer_EditCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgCustomer.EditItemIndex = e.Item.ItemIndex;
dgCustomer.DataBind();
}
private void dgCustomer_CancelCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
dgCustomer.EditItemIndex = -1;
dgCustomer.DataBind();
}
private void dgCustomer_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string key =
dgCustomer.DataKeys[e.Item.ItemIndex].ToString();
TextBox tb;
tb = (TextBox) e.Item.Cells[2].Controls[0];
string fname = tb.Text;
lblMessage.Text = fname;
tb = (TextBox) e.Item.Cells[3].Controls[0];
string lname = tb.Text;
tb = (TextBox) e.Item.Cells[4].Controls[0];
string cname = tb.Text;
tb = (TextBox) e.Item.Cells[5].Controls[0];
string email = tb.Text;
tb = (TextBox) e.Item.Cells[6].Controls[0];
string street = tb.Text;
tb = (TextBox) e.Item.Cells[7].Controls[0];
string city = tb.Text;
tb = (TextBox) e.Item.Cells[8].Controls[0];
string state = tb.Text;
tb = (TextBox) e.Item.Cells[9].Controls[0];
string zip = tb.Text;
tb = (TextBox) e.Item.Cells[10].Controls[0];
string phone = tb.Text;
tb = (TextBox) e.Item.Cells[11].Controls[0];
string pass = tb.Text;
dsCustomer.tblCustomersRow r;
r =
dsCustomer1.tblCustomers.FindByCustomerId(int.Parse(key));
r.FirstName = fname;
r.LastName = lname;
r.CompanyName = cname;
r.EmailAddress = email;
r.StreetAddress = street;
r.City = city;
r.State = state;
r.Zip = zip;
r.Phone = phone;
r.CustomerPassword = pass;
sqlDataAdapter1.Update(dsCustomer1);
dgCustomer.EditItemIndex = -1;
dgCustomer.DataBind();
}
}
}
------------------------ Yahoo! Groups Sponsor --------------------~-->
<font face=arial size=-1><a
href="http://us.ard.yahoo.com/SIG=12hstjd9d/M=362329.6886308.7839368.1510227/D=groups/S=1705006764:TM/Y=YAHOO/EXP=1124294355/A=2894321/R=0/SIG=11dvsfulr/*http://youthnoise.com/page.php?page_id=1992
">Fair play? Video games influencing politics. Click and talk back!</a>.</font>
--------------------------------------------------------------------~->
Yahoo! Groups Links
<*> To visit your group on the web, go to:
http://groups.yahoo.com/group/AspNetAnyQuestionIsOk/
<*> To unsubscribe from this group, send an email to:
[EMAIL PROTECTED]
<*> Your use of Yahoo! Groups is subject to:
http://docs.yahoo.com/info/terms/