HOW TO CHECK USER DISCONNECTION IN VISUAL WEB GUI


In a Visual Web GUI application sometimes it is required to know, if user connected or not.
Disconnection can be occurred due to several reasons i.e.
1.     Enters another URL and navigates away.
2.     Closes the browser window.
3.     Refreshes the page. ( Sessions reset sometimes)
Following code snippet will show you the event that can be implemented to check such behaviours:
using System;
using Gizmox.WebGUI.Forms;

namespace Test
{
    public partial class Index : Form
    {
        public Index()
        {
            InitializeComponent();

            //This event is used for checking disconnection
Application.ThreadSuspend += new EventHandler(Application_ThreadSuspend);
        }

        void Application_ThreadSuspend(object sender, EventArgs e)
        {
            // TODO: Add Code to do the tasks after disconnection
        }
    }
}

HOW TO SEND A CRYSTAL REPORT DOCUMENT AS PDF DOCUMENT VIA EMAIL


This code snippet describes how to send Crystal Reports via Email Programmatically


using System;
using System.Net;
using System.Net.Mail;
using System.Windows.Forms;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;

namespace CSharpDeveloper.Core
{
    public class CrystalReportHelperClass
    {
        public void SendEmail(string emailId, string subject, string body, ReportDocument rpt, string fileName)
        {
            try
            {
                MailMessage mm = new MailMessage();
                mm.From = new MailAddress("xyz@example.com", "From Name");
                mm.To.Add(new MailAddress(emailId, "To Name"));
                mm.Subject = subject;
                mm.Body = body;
                mm.Attachments.Add(
                    new Attachment(rpt.ExportToStream(ExportFormatType.PortableDocFormat), fileName)
                    );

                SmtpClient sc = new SmtpClient("smtp.example.com");
                sc.Credentials = new NetworkCredential("xyz@example.com", "********");
                sc.Send(mm);

                MessageBox.Show("Email successfully sent to " + emailId);
            }
            catch (Exception e)
            {
                if (
                    MessageBox.Show("Unable to send email to "
+ emailId + " due to following error:\n\n"
                              + e.Message, "Email send error"
, MessageBoxButtons.RetryCancel, MessageBoxIcon.Error)
                    == DialogResult.Retry
                    )
                {
                    this.SendEmail(emailId, subject, body, rpt, fileName);
                }
            }
        }
    }
}

HOW TO SHOW A CRYSTAL REPORT WITHOUT USING ODBC DATA SOURCE

/*

 

 * This code demonstrates how to show a crystal report without using odbc data source.

 * It requires connection string to display report.

 * Crystal report should be made using OLE-DB Data provider.

 * Drag the crystal report viewer on form and you may set its dock property to fill.

 */

 

using System;

using System.Data.SqlClient;

using System.Windows.Forms;

using CrystalDecisions.CrystalReports.Engine;

using CrystalDecisions.Shared;

 

namespace CSharpDeveloper.Core

{

    public partial class FrmReportView : Form

    {

        public FrmReportView()

        {

            InitializeComponent();

            this.crystalReportViewer1.ReportSource = null;

        }

 

        public FrmReportView(

            ParameterFields rptParams, ReportDocument rpt, string connectionString)

            : this()

        {

            try

            {

                this.Text = rpt.FileName;

 

                if (rptParams != null)

                {

                    this.crystalReportViewer1.ParameterFieldInfo = rptParams;

                }

 

                //Load report from its location

                rpt.Load(rpt.FileName);

 

                //Set report on report viewer

                this.crystalReportViewer1.ReportSource = rpt;

 

                SqlConnectionStringBuilder sb = new SqlConnectionStringBuilder(connectionString);

                rpt.SetDatabaseLogon(

                    sb.UserID, sb.Password, sb.DataSource, sb.InitialCatalog, true);

 

                //Extract connection info from connection string           

                ConnectionInfo ci = new ConnectionInfo();

                ci.ServerName = sb.DataSource;

                ci.DatabaseName = sb.InitialCatalog;

                ci.UserID = sb.UserID;

                ci.Password = sb.Password;

 

                //Create TableLogOnInfo object and set connection info

                TableLogOnInfo tli = new TableLogOnInfo();

                tli.ConnectionInfo = ci;

 

                //Apply log on info to each table of report

                foreach (Table table in rpt.Database.Tables)

                {

                    table.ApplyLogOnInfo(tli);

                }

            }

            catch (Exception e)

            {

                MessageBox.Show(e.Message);

            }

        }

    }

}

HOW TO CALL A STORED PROCEDURE USING ENTITY SPACES ESUTILITY CLASS


Following code sample demonstrated how to call a stored procedure using entity spaces esUtility class, return output parameters from it and display those output parameters in a message box.
Simple stored procedure:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE spTest
  @p1 int = 0,
  @p2 int = 0 OUTPUT
AS
BEGIN
  SET NOCOUNT ON;

  SET @p2 = @p1 * 10000;
END
GO
Simple function in c#:
private void SPTest()
{
  try
  {
      esUtility es = new esUtility();

      //No need to use @ sign
      esParameter p1 = new esParameter("p1", 25, esParameterDirection.Input, DbType.Int32, 0);
      esParameter p2 = new esParameter("p2", esParameterDirection.Output, DbType.Int32, 0);

      esParameters parameters = new esParameters();
      parameters.Add(p1);
      parameters.Add(p2);

      es.ExecuteNonQuery(EntitySpaces.Interfaces.esQueryType.StoredProcedure, "spTest", parameters);

      MessageBox.Show(p2.Value.ToString());
  }
  catch (Exception e)
  {
      MessageBox.Show(e.Message);
  }
}