Click here to Skip to main content
15,902,492 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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)
    {
        //Validations for From Date,To Date and File Uploader 
        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();
            //send filepath,sheet number,selected rows to class file
            DataTable dt = obj.Read(fullpath, 1);
            bool Ismatch = false;
            //string getmonth = txtfromdate.Text.Trim();
            //string getyear = txtToDate.Text.Trim();
            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];
                //newly added for the existing code to obtain the month and year
                //from date
                //string[] splitmonth = getmonth.Split('/');
                //string newmonth = splitmonth[0].ToString();
                //txtfromdate.Text = newmonth.ToString();
                ////To date
                //string[] splityear = getyear.Split('/');
                //string newyear = splityear[2].ToString();
                //txtToDate.Text = newyear.ToString();
                if (month == ddlmonth.SelectedValue && year == ddlyear.SelectedValue)
                {
                    Ismatch = true;
                    //break;
                }
                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;
            }
            //checking the input month and year records exists or not in DB
            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";
                    //mapping the columns 
                    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";
                    //mapping the columns 
                    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";
                //mapping the columns 
                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";
                //txtfromdate.Text = "";
                //txtToDate.Text = "";
                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
Posted
Updated 23-Feb-17 20:45pm
Comments
Karthik_Mahalingam 24-Feb-17 2:35am    
what is the issue?
post only the relevant code..
kav@94 24-Feb-17 2:40am    
the issue is till i upload the sheet successfully into database i am able to display the label messages to the user select month ,select year,not a valid document,select file all these messages i am able to display when user clicks the button but once the sheet is uploaded i am unable to see the error messages on button click but the button click event is firing
kav@94 24-Feb-17 2:48am    
my problem is i should again display the error messages after uploading the sheet successfully againg if user select invalid month and year but once the data is inserted no error message is displayed on button click
Karthik_Mahalingam 24-Feb-17 2:51am    
ya, what is point of displaying any error message once the data loaded successfully ?
did you try the below solution.

1 solution

Clear the label text on button click
protected void btnUpload_Click(object sender, EventArgs e)
   {
   lblerrorMessage.Text = "";
       .
       .
       .
 
Share this answer
 

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