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);
        }
    }
}

Take a look at following code:

aspx markup

<form id="form1" runat="server">
    <div>
       <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            onrowcancelingedit="GridView1_RowCancelingEdit" 
            onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing" 
            onrowupdating="GridView1_RowUpdating" 
            onselectedindexchanged="GridView1_SelectedIndexChanged">
            <Columns>
                <asp:BoundField DataField="eno" HeaderText="No" />
                <asp:BoundField DataField="ename" HeaderText="Name" />
                <asp:BoundField DataField="edate" DataFormatString="{0:dd-MMM-yyyy}" 
                    HeaderText="DateJoined" />
                <asp:CommandField ShowEditButton="True" />
                <asp:CommandField ShowSelectButton="True" />
                <asp:ButtonField CommandName="delete" Text="Delete" />
            </Columns>
        </asp:GridView>
    </form>

.cs - code-behind

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Show();
        }
    }

    void Show()
    {
        SqlConnection cn = new SqlConnection(@"put_connStr");
        SqlDataAdapter adp = new SqlDataAdapter("select * from emp", cn);
        DataTable dt = new DataTable();

        adp.Fill(dt);

        GridView1.DataSource = dt;
        GridView1.DataBind();

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

        GridView1.EditIndex = e.NewEditIndex ;
        Show();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;

        Show();
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        TextBox t1, t2, t3;

        t1 =(TextBox) GridView1.Rows[e.RowIndex].Cells[0].Controls[0];
        t2 = (TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0];
        t3 = (TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0];

     
 
        SqlConnection cn = new SqlConnection(@"put_connStr");
        SqlCommand cmd = new SqlCommand("update emp set ename=@p1,edate=@p2 where eno=@p3",cn);
        cmd.Parameters.AddWithValue("@p1", t2.Text);
        cmd.Parameters.AddWithValue("@p2", t3.Text);
        cmd.Parameters.AddWithValue("@p3", t1.Text);

        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();

        GridView1.EditIndex = -1;
        Show();

    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string eno = GridView1.Rows[e.RowIndex].Cells[0].Text;

        SqlConnection cn = new SqlConnection(@"put_connStr");
        SqlCommand cmd = new SqlCommand("delete from emp where eno=@p1", cn);
        cmd.Parameters.AddWithValue("@p1", eno);
        

        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
        
        Show();
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string eno, ename, edate;

        eno = GridView1.SelectedRow.Cells[0].Text;
        ename = GridView1.SelectedRow.Cells[1].Text;
        edate = GridView1.SelectedRow.Cells[2].Text;

        Response.Write(eno + " " + ename + " " + edate);

    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.