Pls help me , i can not run it correctly with "AddNew, Update, Update, Delete, Cxl" , i have tried many ways but no effect ?
My database has 5 tables , i choose one which has 7 columns , the 7th col (name = "ID") is identity number (1/1). This code run by web_form with gridview control .
Thanks a lot for all your help !
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Booking : System.Web.UI.Page
{
SqlConnection sqlconAcmeyTravel;
protected void Page_Load(object sender, EventArgs e)
{
try
{
sqlconAcmeyTravel = new SqlConnection("Data Source=Mata;Database=customer;Integrated Security=true");
sqlconAcmeyTravel.Open();
if(!Page.IsPostBack)
{
FillBookingDetails();
}
}catch(Exception ex)
{
Response.Write("error:" + ex.Message);
}
}
private void FillBookingDetails()
{
try
{
SqlCommand sqlcomSelect = new SqlCommand("Select * from khachhang", sqlconAcmeyTravel);
gvBookingDetails.DataSource = sqlcomSelect.ExecuteReader();
gvBookingDetails.DataBind();
}
catch(Exception ex)
{
Response.Write("error :" + ex.Message);
}
}
protected void gvBookingDetails_RowEditing(object sender, GridViewEditEventArgs e)
{
gvBookingDetails.EditIndex = e.NewEditIndex;
FillBookingDetails();
((TextBox)gvBookingDetails.Rows[gvBookingDetails.EditIndex].Cells[1].Controls[0]).Enabled = true;
btnAddNew.Visible = false;
}
protected void gvBookingDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
try
{
string custCode;
if (btnAddNew.Enabled)
{
custCode = gvBookingDetails.Rows[e.RowIndex].Cells[1].Text;
}
else
{
custCode = ((TextBox)gvBookingDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string sqlQuery = "DELETE FROM khachhang WHERE Ten='" + custCode + "'";
SqlCommand sqlcomDelete = new SqlCommand(sqlQuery, sqlconAcmeyTravel);
sqlcomDelete.ExecuteNonQuery();
FillBookingDetails();
}
}catch(Exception ex)
{
Response.Write("error:" + ex.Message);
}
}
protected void gvBookingDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
//int row = e.RowIndex;
//for (row = 0; row < gvBookingDetails.Rows.Count; row++)
//{
string custCode = ((TextBox)gvBookingDetails.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string custAd = ((TextBox)gvBookingDetails.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string custCity = ((TextBox)gvBookingDetails.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string custCounty = ((TextBox)gvBookingDetails.Rows[e.RowIndex].Cells[4].Controls[0]).Text;
string custPhone = ((TextBox)gvBookingDetails.Rows[e.RowIndex].Cells[5].Controls[0]).Text;
string custZip = ((TextBox)gvBookingDetails.Rows[e.RowIndex].Cells[6].Controls[0]).Text;
string sqlQuery = "INSERT INTO khachhang(Ten,Ad,City,County,Phone,Zip) VALUES('" + custCode + "','" +
custAd + "','" + custCity + "','" + custCounty + "','" + custPhone + "','" + custZip + "')";
//OR:
//string sqlQuery = "UPDATE khachhang SET Ten='" + custCode + "' , Ad='" +
// custAd + "' , City='" + custCity + "' , County='" + custCounty + "' , Phone='" +
// custPhone + "' , Zip='" + custZip + "' WHERE Ten='" + custCode + "'";
//OR:
//string sqlQuery = "UPDATE khachhang SET Ten='" + custCode + "' , ";
//sqlQuery += "Ad='" + custAd + "' , City='" + custCity + "', ";
//sqlQuery += "County='" + custCounty + "' , Phone='" + custPhone + "', ";
//sqlQuery += "Zip='" + custZip + "'";
//sqlQuery += " WHERE Ten='" + custCode + "'";
SqlCommand sqlcomUpdate = new SqlCommand(sqlQuery, sqlconAcmeyTravel);
sqlcomUpdate.ExecuteNonQuery();
gvBookingDetails.EditIndex = -1;
FillBookingDetails();
btnAddNew.Visible = true;
btnAddNew.Enabled = true;
//}
}catch(Exception ex)
{
Response.Write("error:" + ex.Message);
}
}
protected void gvBookingDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
gvBookingDetails.EditIndex = -1;
if (!btnAddNew.Enabled)
gvBookingDetails.DeleteRow(e.RowIndex);
else
FillBookingDetails();
btnAddNew.Visible = true;
btnAddNew.Enabled = true;
}
protected void btnAddNew_Click(object sender, EventArgs e)
{
try
{
// string custID = gvBookingDetails.Rows[gvBookingDetails.Rows.Count - 1].Cells[1].Text;
// int newCustID = Convert.ToInt32(custID.Remove(0, 1)) + 1;
string sqlQuery = "INSERT INTO khachhang VALUES('','','','','','')";
SqlCommand sqlcomInsert = new SqlCommand(sqlQuery, sqlconAcmeyTravel);
sqlcomInsert.ExecuteNonQuery();
gvBookingDetails.EditIndex = gvBookingDetails.Rows.Count;
FillBookingDetails();
((TextBox)gvBookingDetails.Rows[gvBookingDetails.EditIndex].Cells[1].Controls[0]).Enabled = true;
btnAddNew.Enabled = false;
}catch(Exception ex)
{
Response.Write("error:" + ex.Message);
}
}
}