Click here to Skip to main content
15,885,890 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The data is available in excel but when I tryy to delete a row with this code
''''''''''''''''''''''''''''''''''''''
C#
conn = new OleDbConnection();
                       conn.ConnectionString = gm.ConnectionString;
                       conn.Open();
                       cmd = new OleDbCommand("select * from [ExecutiveMember]", conn);
                       da = new OleDbDataAdapter(cmd);
                       dt.Clear();
                       da.Fill(dt);
                       DataRow dtRowDelete = dt.Rows[index];
                       dtRowDelete.Delete();
                       dt.AcceptChanges();
                       //DataTable dtable = dt;
                       da.Update(dt);

                       dgvExecutiveMember.DataSource = dt;



                       //cmd.Connection = conn;
                       //cmd.CommandText = "DELETE from [ExecutiveMember] where MobileNumber='" + MobileNumber + "'";
                       cmd.ExecuteNonQuery();
                       conn.Close();
                       Clear();
                       MobileNumber = "";

                       // index = -1;
                       MessageBox.Show("Data Deleted!...");
                        //conn = new OleDbConnection();
                        //conn.ConnectionString = gm.ConnectionString;
                        //conn.Open();
                        //string delete = "delete from ExecutiveMember where MobileNumber = '+91''" + txtMobile2.Text + "'";
                        //cmd = new OleDbCommand(delete, conn);
                        //cmd.ExecuteNonQuery();
                        //conn.Close();
                        //MessageBox.Show("Data Deleted!...");

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

row is delete but in excel sheet data is present.
Posted
Updated 24-Aug-11 3:25am
v2
Comments
Wayne Gaylard 24-Aug-11 9:26am    
Added code blocks. I am afraid your question is not really clear. Could you try and rephrase it a little. If you have problems then I suggest you try and get someone from General Indian Forum to translate for you.
Rohit Sharma706 26-Aug-11 8:16am    
ok.sir.
i want to know how to delete row in Excel sheet in c#.
so i do above coding by this [da.Update(dt);].i got delete value in datatable but it is not modify in Excel Sheet.as usual data in present.

1 solution

It looks like primary source (in your case Excel) is not getting the commit command to the transaction. But the changes are reflected at InMemory temporary storage. Possible cause could be some unhandled exception during the execution.

As a permanent solution/best practice mode, we can use the below style of coding with TransactionScope for any data source connection/execution.

C#
try
{
    conn = new OleDbConnection();
    conn.ConnectionString = gm.ConnectionString;
    conn.Open();
    transaction = conn.BeginTransaction(IsolationLevel.ReadCommitted);

    cmd = new OleDbCommand("select * from [ExecutiveMember]", conn);
    .................
    .................

    cmd.ExecuteNonQuery();
    transaction.Commit();
    conn.Close();
}
catch (Exception ex)
{
    Console.WriteLine(ex.Message);
    transaction.Rollback();
}
 
Share this answer
 
Comments
Rohit Sharma706 25-Aug-11 9:07am    
sir,I try that but it not working after fill data table it throw exception.

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