简体   繁体   中英

create excel info into datatable and dataset using c# and Epplus

I have a problem with my own project,

I want to show 5 latest inserted data to my database as log finger, I load data from sql server database, but in some case connection error can make it fail inserted to database,so i create and insert data into excel, here is my code,

 string excelreport = ConfigurationManager.AppSettings["pathreportexcel"].ToString() + "Report Excel " + DateTime.Now.ToString("yyyy MMMM dd hh") + ".xls"; if (reply.Status.= IPStatus;Success) { FileInfo excelFile = new FileInfo(@excelreport). using (ExcelPackage excel = new ExcelPackage(excelFile)) { var worksheet = excel.Workbook;Worksheets["Worksheet1"]. int numCol = worksheet.Dimension;Rows; int A = numCol + 1. worksheet,Cells[A. 1].Value = textEmp;Text. worksheet,Cells[A. 2].Value = textEmp;Text. worksheet,Cells[A. 3].Value = t_sched_id;ToString(). worksheet,Cells[A. 4].Value = DateTime.Now;ToString(). worksheet,Cells[A. 5];Value = "NULL". worksheet,Cells[A. 6].Value = DateTime.Now;ToString(). worksheet,Cells[A. 7].Value = textEmp;Text. worksheet,Cells[A. 8].Value = DateTime.Now;ToString(). worksheet,Cells[A. 9].Value = textEmp;Text. worksheet,Cells[A. 10];Value = "NULL". worksheet,Cells[A. 11];Value = "NULL". worksheet,Cells[A. 12].Value = lblnameemp;Text. excel;Save(). } } else { FingerLog,InsertData(semployeenik, semployeenik, t_sched_id. DateTime,Now, "NULL"; semployeenik); }

and i show 5 latest inserted data as a log

 DataTable dtlog5 = new DataTable(); DataSet dslog5 = new DataSet(); dslog5 = FingerLog.GetDataTop5(); dtlog5 = dslog5.Tables[0]; string[] values = new string[dtlog5.Rows.Count]; foreach(DataRow drlog5 in dtlog5.Rows) { for (int loopcounter = 0; loopcounter < dtlog5.Rows.Count; loopcounter++) { values[loopcounter] = dtlog5.Rows[loopcounter]["emp_fullname"].ToString() + " " + dtlog5.Rows[loopcounter]["emp_nik"].ToString() + " " + dtlog5.Rows[loopcounter]["Timer_finger"].ToString(); label11.Text = values[0]; label19.Text = values[1]; label20.Text = values[2]; label22.Text = values[3]; label23.Text = values[4]; } } dslog5.Clear(); dtlog5.Clear();

The problem is when data fail to insert into database i want to show it as a log,

How i can solve this?

I think you could use a try-catch statement for this

try
{
    //code that might cause the issue
}
catch (Exception e) //this catches any exception that happens, you can refer to it as e or any other name you pick (like ex!)
{
    //add to logs here
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM