【问题标题】:if else condition in asp.netif else 条件在 asp.net
【发布时间】:2020-08-26 11:34:26
【问题描述】:

我的项目有问题,

我想从 excel 中读取我的数据并插入到数据库中, 但在此之前,

但我在某些情况下,我在我的 excel 中输入了错误的日期时间格式,所以它在我的程序中出错, 所以我想我想在将数据处理到数据库之前验证我的数据,

这是我的代码,

if (FileUpload1.HasFile)
        {
            if (ddlTest.SelectedValue.Length != 0)
            {
                string filename = "Schedule" + DateTime.Now.ToString("dddd_dd_MMMM_yyyy") + ".xls";
                FileUpload1.SaveAs(temp_file + filename);
                string tempfile = temp_file + filename;
                DataTable table = new DataTable();
                FileInfo existingFile = new FileInfo(tempfile); //+ FileUpload1.FileName);
                using (ExcelPackage package = new ExcelPackage(existingFile))
                {
                    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                    int colCount = worksheet.Dimension.End.Column;
                    int rowCount = worksheet.Dimension.End.Row;
                    table.Columns.Add("Emp_NIK", typeof(string));
                    table.Columns.Add("ID_Shift", typeof(string));
                    table.Columns.Add("Schedule_Date", typeof(DateTime));
                    for (int i = 1; i < rowCount; i++)
                    {
                       try
                        {

                            DateTime d;
                            bool validate = DateTime.TryParseExact(
                            worksheet.Cells[i + 1, 3].Value.ToString(),
                            "M/dd/yyyy h:mm:ss",
                            CultureInfo.InvariantCulture,
                            DateTimeStyles.None,
                            out d);
                            TableRow row = new TableRow();
                            TableCell cell1 = new TableCell();
                            if (validate == true)
                            {
                                string labelmonth = ddlTest.SelectedValue.ToString();
                                string shift = worksheet.Cells[i + 1, 2].Value.ToString();
                                string employee_id = worksheet.Cells[i + 1, 1].Value.ToString();
                                DateTime schedule_date = DateTime.Parse(worksheet.Cells[i + 1, 3].Value.ToString());
                                string user = Session["LogedUserID"].ToString();
                                bool validatemonth = ddlTest.SelectedValue.ToString() == DateTime.Parse(worksheet.Cells[i + 1, 3].Value.ToString()).ToString("MM");
                                if (validatemonth == false)
                                {
                                    cell1.Text = worksheet.Cells[i + 1, 1].Value.ToString() + "  " + worksheet.Cells[i + 1, 2].Value.ToString() + " " + worksheet.Cells[i + 1, 3].Value.ToString() + " Baris ke " + i + " Bulan Tidak Sesuai";
                                    row.Cells.Add(cell1);
                                    mytable.Rows.Add(row);
                                }
                                else
                                {
                                    schedule.InsertData(employee_id, shift, schedule_date, user);
                                }

                            }
                            else
                            {
                                cell1.Text = worksheet.Cells[i + 1, 1].Value.ToString() + "  " + worksheet.Cells[i + 1, 2].Value.ToString() + " " + worksheet.Cells[i + 1, 3].Value.ToString() + " Baris ke " + i + "Format Tanggal salah";
                                row.Cells.Add(cell1);
                                mytable.Rows.Add(row);
                            }
                        }
                        catch (Exception err)
                        {
                            Response.Write("<script>window.alert('File Excel Kosong')</script>");
                        }
                    }
                }
            }
            else
            {
                Response.Write("<script>window.alert('Pilih Bulan Terlebih dahulu')</script>");
            }
        }
        else
        {
            Response.Write("<script>window.alert('File Belum Di Upload')</script>");
        }

例如,我的 excel 中有 5 行数据,
1行无效,4行有效, 我希望当 i 行无效时,另外 4 行无法插入数据库, 但当前情况是当一行无效时,另一行仍插入数据库

我该如何解决这个问题?

【问题讨论】:

  • 你可以使用Transaction来做:Submit()如果成功,Rollback()如果出现任何问题。
  • 你能告诉我,如何在技术上实现它?
  • 我需要schedule.InsertData()的代码。您可以选择这样做: 1. 初始化一个 SqlTransaction 实例,例如:transaction first;然后2,将transaction传递给schedule.InsertData(); 3、如果所有行都有效,则做transaction.Commit();,否则,调用`transaction.Rollback()';
  • public void InsertData(string sEmpNIK, string sIDShift, DateTime dScheduleDate,string sUser) { conn = objDBConnector.GetConn(); cmd = objDBConnector.GetCommand(); SqlDataReader rdr = null; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "插入数据"; cmd.Parameters.AddWithValue("@Emp_NIK", sEmpNIK); cmd.Parameters.AddWithValue("@ID_Shift", sIDShift); cmd.Parameters.AddWithValue("@Schedule_Date", dScheduleDate); cmd.Parameters.AddWithValue("@User", sUser); }
  • 这是您需要的代码,stackoverflow 的注释字符有限,如果我的文字看起来不整洁,我很抱歉

标签: c# asp.net web


【解决方案1】:

就像我上面的cmets。请检查以下代码作为参考:

        if (FileUpload1.HasFile)
        {
            if (ddlTest.SelectedValue.Length != 0)
            {
                string filename = "Schedule" + DateTime.Now.ToString("dddd_dd_MMMM_yyyy") + ".xls";
                FileUpload1.SaveAs(temp_file + filename);
                string tempfile = temp_file + filename;
                DataTable table = new DataTable();
                FileInfo existingFile = new FileInfo(tempfile); //+ FileUpload1.FileName);
                using (ExcelPackage package = new ExcelPackage(existingFile))
                {
    SqlTransaction transaction = objDBConnector.GetConn().BeginTransaction("SampleTransaction");
                    ExcelWorksheet worksheet = package.Workbook.Worksheets[1];
                    int colCount = worksheet.Dimension.End.Column;
                    int rowCount = worksheet.Dimension.End.Row;
                    table.Columns.Add("Emp_NIK", typeof(string));
                    table.Columns.Add("ID_Shift", typeof(string));
                    table.Columns.Add("Schedule_Date", typeof(DateTime));
                    for (int i = 1; i < rowCount; i++)
                    {
                       try
                        {

                            DateTime d;
                            bool validate = DateTime.TryParseExact(
                            worksheet.Cells[i + 1, 3].Value.ToString(),
                            "M/dd/yyyy h:mm:ss",
                            CultureInfo.InvariantCulture,
                            DateTimeStyles.None,
                            out d);
                            TableRow row = new TableRow();
                            TableCell cell1 = new TableCell();
                            if (validate == true)
                            {
                                string labelmonth = ddlTest.SelectedValue.ToString();
                                string shift = worksheet.Cells[i + 1, 2].Value.ToString();
                                string employee_id = worksheet.Cells[i + 1, 1].Value.ToString();
                                DateTime schedule_date = DateTime.Parse(worksheet.Cells[i + 1, 3].Value.ToString());
                                string user = Session["LogedUserID"].ToString();
                                bool validatemonth = ddlTest.SelectedValue.ToString() == DateTime.Parse(worksheet.Cells[i + 1, 3].Value.ToString()).ToString("MM");
                                if (validatemonth == false)
                                {
                                    cell1.Text = worksheet.Cells[i + 1, 1].Value.ToString() + "  " + worksheet.Cells[i + 1, 2].Value.ToString() + " " + worksheet.Cells[i + 1, 3].Value.ToString() + " Baris ke " + i + " Bulan Tidak Sesuai";
                                    row.Cells.Add(cell1);
                                    mytable.Rows.Add(row);
                                }
                                else
                                {
                                    schedule.InsertData(transaction, employee_id, shift, schedule_date, user);
                                }

                            }
                            else
                            {
                                cell1.Text = worksheet.Cells[i + 1, 1].Value.ToString() + "  " + worksheet.Cells[i + 1, 2].Value.ToString() + " " + worksheet.Cells[i + 1, 3].Value.ToString() + " Baris ke " + i + "Format Tanggal salah";
                                row.Cells.Add(cell1);
                                mytable.Rows.Add(row);
                            }

if(mytable.Rows != null && mytable.Rows.Count > 0)
{
    transaction.Rollback();
}
else
{
    transaction.Commit();
}
                        }
                        catch (Exception err)
                        {
transaction.Rollback();
                            Response.Write("<script>window.alert('File Excel Kosong')</script>");
                        }
                    }
                }
            }
            else
            {
                Response.Write("<script>window.alert('Pilih Bulan Terlebih dahulu')</script>");
            }
        }
        else
        {
            Response.Write("<script>window.alert('File Belum Di Upload')</script>");
        }

【讨论】:

  • schedule.insertdata() 中的代码怎么样,我需要做任何更改吗?
  • 暂时没有变化。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2019-07-01
  • 1970-01-01
  • 2011-11-06
  • 2020-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多