Click here to Skip to main content
15,922,696 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
 private void filladapter()
        {
           da.DeleteCommand = new SqlCommand();
                da.DeleteCommand.Connection = con;
                da.DeleteCommand.CommandText = "DELETE FROM [dbo].[Customer] WHERE ([CustomerId] = @CustomerId)";
                da.DeleteCommand.CommandType = CommandType.Text;
                da.DeleteCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@CustomerId", System.Data.SqlDbType.BigInt, 0, System.Data.ParameterDirection.Input, 0, 0, "CustomerId", System.Data.DataRowVersion.Current, false, null, "", "", ""));
                da.InsertCommand = new SqlCommand();
}
private void showAllCustomer()
        {
            SqlConnection con = new SqlConnection(Connection.getConnectionSource());
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from Customer";
            cmd.Connection = con;
            da.SelectCommand = cmd;
            da.Fill(dt);
            bsource.DataSource = dt;
            datagdvCustomer.DataSource = bsource;

        }
//..........................delete commnad...................................................//
string ctid = datagdvCustomer.Rows[e.RowIndex].Cells["CustomerId"].Value.ToString();
                    filladapter();
                    con.Open();
                    da.DeleteCommand.Parameters["@CustomerId"].Value = ctid;
                    da.DeleteCommand.ExecuteNonQuery();
                    con.Close();
                    datagdvCustomer.DataSource = null;
                    showAllCustomer();
It is showing records twice,and that too without updation ....backend is updation is correct
Posted
Updated 15-Aug-13 19:28pm
v2
Comments
TrushnaK 16-Aug-13 2:00am    
Bind your grid view again after deletion or you can use update panel.

You can use
OnRowDeleting property of GRIDVIEW
C#
//Function To delete records from grid view
 protected void Countrylist_Rowdelete(object sender, GridViewDeleteEventArgs e)
        {
  int intCode = Convert.ToInt32(Countrylist.DataKeys[e.RowIndex].Value);//here it will get the row selected to delete


           string  strQuery = "DELETE FROM Country_master " +
                       "WHERE CountryId=" + intCode + "";

            if (con.State == ConnectionState.Open)
            {
                con.Close();
            }
            con.Open();
            OleDbCommand oledbcmd = new OleDbCommand(strQuery, con);

            oledbcmd.ExecuteNonQuery();
            Display();//Your Gridview Bind function
}


You can call Your Display() On PageLoad()..

Hope This Helps!!
 
Share this answer
 
v2
Try this
C#
private void showAllCustomer()
        {
            SqlConnection con = new SqlConnection(Connection.getConnectionSource());
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select * from Customer";
            cmd.Connection = con;
            da.SelectCommand = cmd;

//You need tocreate fresh datatable object here. I think your datatable object is public & mantaining values
DataTable dt = new DataTable(); 

            da.Fill(dt);
            bsource.DataSource = dt;
            datagdvCustomer.DataSource = bsource;
 
        }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900