C# asp.net中的if-else条件
我的项目有问题 我想从excel中读取数据并插入数据库, 但在此之前 但是我遇到了一些情况,我在excel中输入了错误的日期时间格式,所以我的程序出错了, 所以我想在将数据处理到数据库之前验证一下 这是我的密码C# asp.net中的if-else条件,c#,asp.net,web,C#,Asp.net,Web,我的项目有问题 我想从excel中读取数据并插入数据库, 但在此之前 但是我遇到了一些情况,我在excel中输入了错误的日期时间格式,所以我的程序出错了, 所以我想在将数据处理到数据库之前验证一下 这是我的密码 if (FileUpload1.HasFile) { if (ddlTest.SelectedValue.Length != 0) { string filename = "Schedule"
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>");
}
if(FileUpload1.HasFile)
{
如果(ddlTest.SelectedValue.Length!=0)
{
string filename=“Schedule”+DateTime.Now.ToString(“dddd\u dd\u MMMM\u yyyy”)+“.xls”;
FileUpload1.SaveAs(临时文件+文件名);
字符串tempfile=临时文件+文件名;
DataTable=新的DataTable();
FileInfo existingFile=newfileinfo(tempfile);//+FileUpload1.FileName);
使用(ExcelPackage=新的ExcelPackage(现有文件))
{
Excel工作表=package.Workbook.Worksheets[1];
int colCount=工作表.Dimension.End.Column;
int rowCount=工作表.Dimension.End.Row;
表.Columns.Add(“Emp_NIK”,typeof(string));
表.Columns.Add(“ID_Shift”,typeof(string));
表.列.添加(“计划日期”,类型(日期时间));
对于(int i=1;i
例如,我的excel中有5行数据,1行无效,4行有效, 我希望当I行无效时,另4行插入数据库失败, 但当前情况是,当一行无效时,另一行仍然插入到数据库中
我如何解决这个问题?正如我上面的评论。请检查以下代码作为参考:
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>");
}
if(FileUpload1.HasFile)
{
如果(ddlTest.SelectedValue.Length!=0)
{
string filename=“Schedule”+DateTime.Now.ToString(“dddd\u dd\u MMMM\u yyyy”)+“.xls”;
FileUpload1.SaveAs(临时文件+文件名);
字符串tempfile=临时文件+文件名;
DataTable=新的DataTable();
FileInfo existingFile=newfileinfo(tempfile);//+FileUpload1.FileName);
使用(ExcelPackage=新的ExcelPackage(现有文件))
{
SqlTransaction transaction=objDBConnector.GetConn().BeginTransaction(“SampleTransaction”);
Excel工作表=package.Workbook.Worksheets[1];
int colCount=工作表.Dimension.End.Column;
int rowCount=工作表.Dimension.End.Row;
表.Columns.Add(“Emp_NIK”,typeof(string));
表.Columns.Add(“ID_Shift”,typeof(string));
表.列.添加(“计划日期”,类型(日期时间));
对于(int i=1;i