|
|
|
กดปุ่ม update แต่ยัง ไม่เข้าคำสั่ง cmd.ExecuteNonQuery(); แต่ข้อมูลมีการ update ที่ดาต้าเบสแล้ว |
|
|
|
|
|
|
|
สวัสดี พี่ๆ ทุกท่านครับ กำลังศึกษาการเขียนเว็บ asp.net (c#) ครับ โดยอ้างจากเว็บ เพื่อนบ้านครับ
http://greatfriends.biz/webboards/msg.asp?id=129281
ตามภาพ ผม กด ปุ่ม stop debug ก่อนที่จะมีการ update ข้อมูล แต่กับกลายเป็นว่า ข้อมูลมีการ update ที่ ดาต้าเบสแล้ว
โค้ดหน้า UpdateCustomer.aspx.cs
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Domain;
using Microsoft.Practices.Unity;
namespace OOP_Layer_DI
{
public partial class UpdateCustomer : System.Web.UI.Page
{
[Dependency]
public ICustomerService custService { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//get customer detail by customerid with form Default.aspx
if (Request.Cookies["ckCust"] != null)
{
//new List<customers> : businessobject.customers
List<BusinessEntities.Customers> objCustomer = new List<BusinessEntities.Customers>();
//asign
string _strCustId = Request.Cookies["ckCust"]["CustomerID"].ToString();
//ger customer by id
objCustomer = this.custService.GetCustomerById(_strCustId);
if (objCustomer != null)
{
this.txtCustomerId.Text = objCustomer[0].CustomerId;
this.txtCompanyName.Text = objCustomer[0].CompanyName;
this.txtContactName.Text = objCustomer[0].ContactName;
this.txtContacTitle.Text = objCustomer[0].ContactTitle;
this.txtAddress.Text = objCustomer[0].Address;
this.txtCity.Text = objCustomer[0].City;
this.txtRegion.Text = objCustomer[0].Region;
this.txtPostalCode.Text = objCustomer[0].PostalCode;
this.txtCountry.Text = objCustomer[0].Country;
this.txtPhone.Text = objCustomer[0].Phone;
this.txtFax.Text = objCustomer[0].Fax;
}
}
}
}
protected void btnUpdate_Click(object sender, EventArgs e)
{
//if txtCustomerId <> ""
if (this.txtCustomerId.Text != "")
{
//new object Customers : businessobject.customers
BusinessEntities.Customers oCustomers = new BusinessEntities.Customers();
//asign
oCustomers.CustomerId = this.txtCustomerId.Text;
oCustomers.CompanyName = this.txtCompanyName.Text;
oCustomers.ContactName = this.txtContactName.Text;
oCustomers.ContactTitle = this.txtContacTitle.Text;
oCustomers.Address = this.txtAddress.Text;
oCustomers.City = this.txtCity.Text;
oCustomers.Region = this.txtRegion.Text;
oCustomers.PostalCode = this.txtPostalCode.Text;
oCustomers.Country = this.txtCountry.Text;
oCustomers.Phone = this.txtPhone.Text;
oCustomers.Fax = this.txtFax.Text;
//update data : ICustomerService.UpdateData(object customers)
if (this.custService.UpdateData(oCustomers))
{
this.lblUpdate.Text = "Update Data Complete !! ";
Response.Redirect("Default.aspx");
this.ClearDataObject();
}
else
{
this.lblUpdate.Text = "Can not Update Data !! : " + this.custService.Error();
}
}
}
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("Default.aspx");
}
private void ClearDataObject()
{
Control myForm = Page.FindControl("Form1");
foreach (Control ctl in myForm.Controls)
{
if (ctl.GetType().ToString().Equals("System.Web.UI.WebControls.TextBox"))
{
((TextBox)ctl).Text = "";
}
}
}
}
}
โค้ดหน้า DalCustomerManager.cs
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using BusinessEntities;
using Domain;
namespace DataAcessLayer
{
public class DalCustomerManager : ICustomerRepository
{
#region Properties / Variable
StringBuilder sb = new StringBuilder();
public string _Error { get; set; }
#endregion
#region ICustomerRepository Members
public List< BusinessEntities.Customers> GetCustomersById(string CustId)
{
if (!string.IsNullOrEmpty(CustId))
{
using (SqlConnection con = new SqlConnection(AppConfiguration.ConnectionString))
{
con.Open();
StringBuilder sb = new StringBuilder();
List<Customers> Customers = new List<Customers>();
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
{
cmd.Connection = con ;
sb.Remove (0,sb.Length );
sb.Append(" Select CustomerID, CompanyName, ContactName, ContactTitle, Address, ");
sb.Append (" City, Region, PostalCode, Country, Phone, Fax ");
sb.Append (" FROM Customers ");
sb.Append (" where customerid = '" + CustId + "' ");
cmd.CommandText = sb.ToString ();
System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
{
if (reader != null)
{
while (reader.Read())
{
Customers oCustomers = new BusinessEntities.Customers();
oCustomers.CustomerId = reader.IsDBNull(0) ? null : reader.GetString(0);
oCustomers.CompanyName = reader.IsDBNull(1) ? null : reader.GetString(1);
oCustomers.ContactName = reader.IsDBNull(2) ? null : reader.GetString(2);
oCustomers.ContactTitle = reader.IsDBNull(3) ? null : reader.GetString(3);
oCustomers.Address = reader.IsDBNull(4) ? null : reader.GetString(4);
oCustomers.City = reader.IsDBNull(5) ? null : reader.GetString(5);
oCustomers.Region = reader.IsDBNull(6) ? null : reader.GetString(6);
oCustomers.PostalCode = reader.IsDBNull(7) ? null : reader.GetString(7);
oCustomers.Country = reader.IsDBNull(8) ? null : reader.GetString(8);
oCustomers.Phone = reader.IsDBNull(9) ? null : reader.GetString(9);
oCustomers.Fax = reader.IsDBNull(10) ? null : reader.GetString(10);
Customers.Add(oCustomers);
}
}
}
}
return Customers;
}
}
return null;
}
public List<BusinessEntities.Customers> GetAllCustomers()
{
List<BusinessEntities.Customers> lstCust = new List<BusinessEntities.Customers>();
using (SqlConnection con = new SqlConnection(AppConfiguration.ConnectionString))
{
con.Open();
StringBuilder sb = new StringBuilder();
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
{
cmd.Connection = con;
sb.Remove(0, sb.Length);
sb.Append(" SELECT * FROM CUSTOMERs ");
cmd.CommandText = sb.ToString();
System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
{
if (reader.HasRows)
{
while (reader.Read())
{
Customers oCustomers = new Customers();
oCustomers.CustomerId = reader.IsDBNull(0) ? null : reader.GetString(0);
oCustomers.CompanyName = reader.IsDBNull(1) ? null : reader.GetString(1);
oCustomers.ContactName = reader.IsDBNull(2) ? null : reader.GetString(2);
oCustomers.ContactTitle = reader.IsDBNull(3) ? null : reader.GetString(3);
oCustomers.Address = reader.IsDBNull(4) ? null : reader.GetString(4);
oCustomers.City = reader.IsDBNull(5) ? null : reader.GetString(5);
oCustomers.Region = reader.IsDBNull(6) ? null : reader.GetString(6);
oCustomers.PostalCode = reader.IsDBNull(7) ? null : reader.GetString(7);
oCustomers.Country = reader.IsDBNull(8) ? null : reader.GetString(8);
oCustomers.Phone = reader.IsDBNull(9) ? null : reader.GetString(9);
oCustomers.Fax = reader.IsDBNull(10) ? null : reader.GetString(10);
lstCust.Add(oCustomers);
}
}
else
{
return null;
}
}
}
}
return lstCust;
}
public bool DeleteData(BusinessEntities.Customers objCust)
{
if (objCust != null)
{
using (SqlConnection con = new SqlConnection (AppConfiguration.ConnectionString))
{
con.Open();
SqlTransaction tr = con.BeginTransaction();
try
{
sb.Remove(0, sb.Length);
sb.Append(" DELETE FROM Customers WHERE CUSTOMERID=@pCOSTOMERID ");
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = sb.ToString();
cmd.Parameters.AddWithValue("@pCOSTOMERID", objCust.CustomerId.ToString ());
cmd.Connection = con;
cmd.Transaction = tr;
cmd.ExecuteNonQuery();
}
tr.Commit();
}
catch (Exception ex)
{
this._Error = ex.ToString();
tr.Rollback();
return false;
}
}
}
else
return false;
return true;
}
public bool InsertData(Customers objCust)
{
if (objCust != null)
{
using (SqlConnection con = new SqlConnection(AppConfiguration.ConnectionString))
{
con.Open();
SqlTransaction tr = con.BeginTransaction();
try
{
using (SqlCommand cmd = new SqlCommand())
{
sb.Remove(0, sb.Length);
sb.Append(" INSERT INTO Customers (CustomerID, CompanyName, ContactName,");
sb.Append(" ContactTitle, Address, City, Region, PostalCode, Country, Phone, Fax) ");
sb.Append(" VALUES (@pCustomerID, @pCompanyName, @pContactName, @pContactTitle, ");
sb.Append(" @pAddress, @pCity, @pRegion, @pPostalCode, @pCountry, @pPhone, @pFax) ");
cmd.Connection = con;
cmd.CommandText = sb.ToString();
cmd.Parameters.AddWithValue("@pCustomerID", objCust.CustomerId);
cmd.Parameters.AddWithValue("@pCompanyName", objCust.CompanyName);
cmd.Parameters.AddWithValue("@pContactName", objCust.ContactName);
cmd.Parameters.AddWithValue("@pContactTitle", objCust.ContactTitle);
cmd.Parameters.AddWithValue("@pAddress", objCust.Address);
cmd.Parameters.AddWithValue("@pCity", objCust.City);
cmd.Parameters.AddWithValue("@pRegion", objCust.Region);
cmd.Parameters.AddWithValue("@pPostalCode", objCust.PostalCode);
cmd.Parameters.AddWithValue("@pCountry", objCust.Country);
cmd.Parameters.AddWithValue("@pPhone", objCust.Phone);
cmd.Parameters.AddWithValue("@pFax", objCust.Fax);
cmd.Transaction = tr;
cmd.ExecuteNonQuery();
}
tr.Commit();
}
catch (Exception ex)
{
this._Error = ex.ToString();
tr.Rollback();
return false;
}
}
}
else
return false;
return true;
}
public bool UpdateData(Customers objCust)
{
if (objCust != null)
{
//new sqlconnection()
using (SqlConnection con = new SqlConnection(AppConfiguration.ConnectionString))
{
con.Open();
SqlTransaction tr = con.BeginTransaction();
try
{
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
{
cmd.Connection = con;
sb.Remove(0, sb.Length);
sb.Append(" UPDATE Customers SET CompanyName = @pCompanyName, ");
sb.Append(" ContactName = @pContactName, ");
sb.Append(" ContactTitle = @pContactTitle, ");
sb.Append(" Address = @pAddress,");
sb.Append(" City = @pCity, ");
sb.Append(" Region = @pRegion, ");
sb.Append(" PostalCode = @pPostalCode,");
sb.Append(" Country = @pCountry, ");
sb.Append(" Phone = @pPhone,");
sb.Append(" Fax = @pFax ");
sb.Append(" WHERE CustomerID = @pCustomerID ");
cmd.CommandText = sb.ToString();
cmd.Parameters.AddWithValue("@pCustomerID", objCust.CustomerId);
cmd.Parameters.AddWithValue("@pCompanyName", objCust.CompanyName);
cmd.Parameters.AddWithValue("@pContactName", objCust.ContactName);
cmd.Parameters.AddWithValue("@pContactTitle", objCust.ContactTitle);
cmd.Parameters.AddWithValue("@pAddress", objCust.Address);
cmd.Parameters.AddWithValue("@pCity", objCust.City);
cmd.Parameters.AddWithValue("@pRegion", objCust.Region);
cmd.Parameters.AddWithValue("@pPostalCode", objCust.PostalCode);
cmd.Parameters.AddWithValue("@pCountry", objCust.Country);
cmd.Parameters.AddWithValue("@pPhone", objCust.Phone);
cmd.Parameters.AddWithValue("@pFax", objCust.Fax);
cmd.Transaction = tr;
cmd.ExecuteNonQuery();
}
tr.Commit();
}
catch (Exception ex)
{
this._Error = ex.ToString();
tr.Rollback();
return false;
}
}
}
else
return false;
return true;
}
public string Error()
{
return this._Error.ToString();
}
#endregion
}
}
ผม ต้องแก้ไข เพิ่มเติม ตรงไหน บ้างครับ
ขอบพระคุณครับ
Tag : .NET, Ms SQL Server 2008, Web (ASP.NET), C#, VS 2010 (.NET 4.x)
|
|
|
|
|
|
Date :
2013-04-01 16:53:24 |
By :
tee |
View :
997 |
Reply :
3 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ถ้าอย่างงั้นอาจจะต้อง Debug ทั้งแต่ Page_Load() ครับ
|
|
|
|
|
Date :
2013-04-01 17:29:36 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
ขอบคุณ พี่ TC Admin มากครับ ค้นหาใน google เค้าให้คลิก stop debug แบบ Terminate All
แทน stop debugging ครับ
|
|
|
|
|
Date :
2013-04-02 09:22:25 |
By :
tee |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
สงสัยผมจะอ่านไม่ละเอียด
|
|
|
|
|
Date :
2013-04-02 09:29:44 |
By :
mr.win |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 04
|