Below is my button click event the problem is till the sheet is uploaded successfully i am getting the label messages as error messages on button click but once it is uploaded successfully when i click the button the label messages are not getting displayed
protected void btnUpload_Click(object sender, EventArgs e)
{
if (ddlmonth.SelectedValue == "0")
{
lblerrorMessage.Text = "Please Select The Month";
}
else if (ddlyear.SelectedValue == "0")
{
lblerrorMessage.Text = "Please Select The Year";
}
else if (FileUpload1.HasFile == false)
{
lblerrorMessage.Text = "Please Select A File";
}
else
{
string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/Files/" + fileName));
string fullpath = Path.GetFullPath(Server.MapPath("~/Files/" + fileName));
ReadExcelSheet obj = new ReadExcelSheet();
DataTable dt = obj.Read(fullpath, 1);
bool Ismatch = false;
for (int i = 0; i < dt.Rows.Count; i++)
{
string date = dt.Rows[i]["Month"].ToString();
string[] date1 = date.Split('/');
string month = date1[0];
string year = date1[2];
if (month == ddlmonth.SelectedValue && year == ddlyear.SelectedValue)
{
Ismatch = true;
}
else
{
Ismatch = false;
break;
}
}
if (Ismatch == true)
{
lblerrorMessage.Visible = false;
lblerrorMessage.Text = "Valid Document";
}
else
{
lblerrorMessage.Text = "Not a Valid Document";
ddlmonth.ClearSelection();
ddlyear.ClearSelection();
label1.Text = "";
return;
}
SqlCommand cmd = new SqlCommand("select Uploaded from TestMCount ", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable DBdt = new DataTable();
da.Fill(DBdt);
if (DBdt.Rows.Count > 0)
{
int month1;
bool validMonth = int.TryParse(ddlmonth.SelectedValue, out month1);
int year1;
bool validYear = int.TryParse(ddlyear.SelectedValue, out year1);
var filteredRows = from row in DBdt.AsEnumerable()
let date = row.Field<System.DateTime>("Uploaded")
where date.Month == month1 && date.Year == year1
select row;
DataRow[] dr = filteredRows.ToArray();
DataTable selectedrows = filteredRows.CopyToDataTable();
if (selectedrows.Rows.Count > 0)
{
for (int i = 0; i <= selectedrows.Rows.Count - 1; i++)
{
string date2 = selectedrows.Rows[i]["Uploaded"].ToString();
con.Open();
SqlCommand cmd1 = new SqlCommand("delete from TestMCount where Uploaded='" + date2 + "'", con);
SqlDataAdapter da1 = new SqlDataAdapter(cmd1);
cmd1.ExecuteNonQuery();
con.Close();
}
SqlBulkCopy objbulk = new SqlBulkCopy(con);
objbulk.DestinationTableName = "TestMCount";
objbulk.ColumnMappings.Add("ID", "MID");
objbulk.ColumnMappings.Add("Month", "Uploaded");
objbulk.ColumnMappings.Add("Client Name", "ClientName");
objbulk.ColumnMappings.Add("Charges", "Charge");
objbulk.ColumnMappings.Add("Payment", "Payment");
objbulk.ColumnMappings.Add("Adjustment", "Adjustment");
objbulk.ColumnMappings.Add("W/O", "WO");
con.Open();
objbulk.WriteToServer(dt);
con.Close();
label1.Text = "Uploaded Successfully";
}
else
{
SqlBulkCopy objbulk = new SqlBulkCopy(con);
objbulk.DestinationTableName = "TestMCount";
objbulk.ColumnMappings.Add("ID", "MID");
objbulk.ColumnMappings.Add("Month", "Uploaded");
objbulk.ColumnMappings.Add("Client Name", "ClientName");
objbulk.ColumnMappings.Add("Charges", "Charge");
objbulk.ColumnMappings.Add("Payment", "Payment");
objbulk.ColumnMappings.Add("Adjustment", "Adjustment");
objbulk.ColumnMappings.Add("W/O", "WO");
con.Open();
objbulk.WriteToServer(dt);
con.Close();
label1.Text = "Uploaded Successfully";
}
}
else
{
SqlBulkCopy objbulk = new SqlBulkCopy(con);
objbulk.DestinationTableName = "TestMCount";
objbulk.ColumnMappings.Add("Month", "Uploaded");
objbulk.ColumnMappings.Add("ID", "MID");
objbulk.ColumnMappings.Add("Client Name", "ClientName");
objbulk.ColumnMappings.Add("Charges", "Charge");
objbulk.ColumnMappings.Add("Payment", "Payment");
objbulk.ColumnMappings.Add("Adjustment", "Adjustment");
objbulk.ColumnMappings.Add("W/O", "WO");
con.Open();
objbulk.WriteToServer(dt);
con.Close();
label1.Text = "Uploaded Successfully";
ddlmonth.ClearSelection();
ddlyear.ClearSelection();
}
}
What I have tried:
i could not find any solution i i ma calling my button event in page load it is not working properly