Click here to Skip to main content
15,919,479 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, I have a button import to upload csv file data into sql server.How can I check the error if there are one of the row cannot commit? It will throw the exception. But how to know which row data have the error? Then I want roll back the transaction.

I am using this code to upload data into sql server.

C#
private UCA.Common.DataControl.DBUtility dbu = UCA.Common.DataControl.DBOperatorFactory.GetDBOperator(System.Configuration.ConfigurationManager.AppSettings["DBType"].ToString());



C#
string[] cols3 = { "Creator", "Creation_Time", "Date" };
string[] vals3 = { this.UserID, DateTime.Now.ToString, DateTime.Now.ToString}
dbu.insertRow("Asset_Flow_History", cols3, vals3); 
Posted

1 solution

i think you can submit all rows in single transaction if something goes wrong you can rollback the transaction if not you can commit your answer

SqlTransaction transaction = sqlConnection.BeginTransaction(IsolationLevel.ReadCommitted);//choose your isolationLevel

then add this transaction to your SqlCommand

hope this help you
 
Share this answer
 
Comments
hueikar 26-Dec-13 4:24am    
But how can I check which row data is not successfully commit? So the user can go edit that particular row data.
Rana Zarour 26-Dec-13 4:42am    
you will insert row by row as i can see
here dbu.insertRow("Asset_Flow_History", cols3, vals3);

you can add try catch block here and catch the exception then rollback the transaction and inform user

also you can validate your data before insert it to database maybe this will be another solution

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