Question

I have the current sequence of events, the user opens a departmental page (aspx with c# code behind) and is presented with a plethora of buttons:

  • Query buttons that pass query results into a single gridview (this works fine)
  • Send email buttons, the same number as query buttons (this also works fine)
  • Export to excel button that exports the gridview content, after pressing the query button, to excel (this also works fine)

But now I’m trying to make the app’s navigation more professional i.e with error messages, as I’ve mentioned above, the user must first press the query button and then the “export” or “email” button, otherwise the excel file exported or attached is empty, so if the first button that the user presses is not the query button, the other 2 buttons should give out error messages telling the user to press the query button first, i would also like to display a message telling the user that the email has been sent, do I accomplish this with CATCH and TRY? Or maybe hide the other buttons and only display them after the user presses the query button? i'm only asking this because my button already have catch and try methods, therefore i don't know how to proceed

I would appreciate some sample code ‘cause I’m a c# novice

here's my gridview code

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true"></asp:GridView>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>

excel button

protected void Buttonexcel_Click(object sender, EventArgs e)
{

    try
    {
        Response.Clear();
        Response.Buffer = true;
        Response.ContentType = "application/vnd.ms-excel";
        Response.Charset = "";
        Response.AddHeader("content-disposition", "attachment;filename=dados.xls");
        StringWriter sWriter = new StringWriter();
        HtmlTextWriter hWriter = new HtmlTextWriter(sWriter);
        GridView1.RenderControl(hWriter);
        Response.Output.Write(sWriter.ToString());
        Response.Flush();
        Response.End();
    }
    catch (Exception ex)
    {
        Label1.Text = ex.ToString();
    }

}

email button

protected void Buttonmail_Click(object sender, EventArgs e)
{
    fn_AttachGrid();
}

public void fn_AttachGrid()
{

    StringWriter sWriter = new StringWriter();
    HtmlTextWriter hWriter = new HtmlTextWriter(sWriter);
    GridView1.RenderControl(hWriter);
    MailMessage mail = new MailMessage();
    mail.IsBodyHtml = true;
    mail.To.Add(new MailAddress("receiver@domain.pt"));
    mail.Subject = "Sales Report";
    System.Text.Encoding Enc = System.Text.Encoding.ASCII;
    byte[] mBArray = Enc.GetBytes(sWriter.ToString());
    System.IO.MemoryStream mAtt = new System.IO.MemoryStream(mBArray, false);
    mail.Attachments.Add(new Attachment(mAtt, "dad.xls"));
    mail.Body = "Hi PFA";
    SmtpClient smtp = new SmtpClient();
    mail.From = new MailAddress("sender@gmail.com", "name");
    smtp.Host = "smtp.gmail.com";
    smtp.UseDefaultCredentials = true;
    System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
    NetworkCred.UserName = "sender@gmail.com";
    NetworkCred.Password = "password";
    smtp.Credentials = NetworkCred;
    smtp.EnableSsl = true;
    smtp.Port = 587;
    smtp.Send(mail);
}
Was it helpful?

Solution 2

You can try disabling the Export and Email buttons, and enabling them in the click event of the Query button. If you don't want to disable them, you can set a hidden field which will say whether the Query button has been clicked. In the click event of the Query button, set the hidden field value. On ClientClick events of the Export and Email buttons, write some JScript in which you can read the hidden field value. If the value corresponds to the Query button not been clicked thus far, show an alert message to the user. If not, proceed as usual.

For Email, try the following piece of code:

protected void Buttonmail_Click(object sender, EventArgs e)
{
    try
    {
       fn_AttachGrid();
       Label1.Text="Email sent successfully";
    }
    catch(Exception ex)
    {
       Label1.Text = ex.ToString();
    }
}

On Email button click, the fn_AttachGrid() will be invoked, and if it sends out the mail successfully, will cause the success message to be shown to the user on the following line. If there's an exception, it will be caught in the catch block, and the exception message is shown to the user. As a side note, it's a good idea to show customised error messages to the user instead of the default exception message and log the exception somewhere.

OTHER TIPS

dipra, i tried to disable the excel/email button on the query button event but i got nowhere, could you provide some sample code?

here's my query button

protected void ButtonQ1_Click(object sender, EventArgs e)
{
    DataTable dt = new DataTable();
    SqlConnection connection = new SqlConnection(GetConnectionString());
    connection.Open();
    SqlCommand sqlCmd = new SqlCommand("SQL QUERY", connection);
    SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
    sqlDa.Fill(dt);
    if (dt.Rows.Count > 0)
    {
        GridView1.DataSource = dt;
        GridView1.DataBind();
    }
    connection.Close();
}

EDIT : I figured out how to hide the buttons, on page_load event i set the buttons as invisible and on each query button onclick event i make them visible.

however i still can't get the email button to display a message telling the user that e-mail has been sent

EDIT 2: got it, i just added to send email code button

ScriptManager.RegisterClientScriptBlock(this, typeof(Page), "anything", "alert('Enviado com sucesso.');", true);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top