Monday, 18 June 2012

To Export the Crystal Report from Dataset through the C# Codes

// To create the DataSet by adding Add->new Item..

        private void btnReport_Click(object sender, EventArgs e)
        {
            ReportDocument cryRpt;
            ExportOptions CrExportOptions;
            DiskFileDestinationOptions CrDiskFileDestinationOptions;
            PdfRtfWordFormatOptions CrFormatTypeOptions;
                cryRpt = new ReportDocument();
                cryRpt.Load("C:\\LawrenceSoft\\Sample.rpt");
                DataSet DataNew = new DataSet();
                DataNew.ReadXml("F:\\LawrenceSoft\\VatDetails.xml");
                DataTable dtReportSource = (DataTable)DataNew.Tables[0];
                cryRpt.SetDataSource(dtReportSource);

                CrDiskFileDestinationOptions = new DiskFileDestinationOptions();
                CrFormatTypeOptions = new PdfRtfWordFormatOptions();
                SaveFileDialog sfdSavePDF = new SaveFileDialog();
                sfdSavePDF.OverwritePrompt = true;
                sfdSavePDF.SupportMultiDottedExtensions = true;
                sfdSavePDF.Filter = "PDF files (*.pdf)|*.pdf";
                sfdSavePDF.ShowDialog(this);
                CrDiskFileDestinationOptions.DiskFileName = sfdSavePDF.FileName;//@"c:\\test.pdf";
                if (string.IsNullOrEmpty(CrDiskFileDestinationOptions.DiskFileName) == false)
                {
                    CrExportOptions = cryRpt.ExportOptions;
                    {
                        CrExportOptions.ExportDestinationType = ExportDestinationType.DiskFile;
                        CrExportOptions.ExportFormatType = ExportFormatType.PortableDocFormat;
                        CrExportOptions.DestinationOptions = CrDiskFileDestinationOptions;
                        CrExportOptions.FormatOptions = CrFormatTypeOptions;
                    }
                    cryRpt.Export();
                }

        }
//To use the Report Viewer for showing the Crystal Report..

            DataSet DataNew = new DataSet();
            DataNew.ReadXml("F:\\LawrenceSoft\\VatDetails.xml");
            DataTable dtReportSource = (DataTable)DataNew.Tables[0];
            ReportDocument rdSample = new ReportDocument();

            rdSample.Load("C:\\LawrenceSoft\\Sample.rpt");
            rdSample.SetDataSource(dtReportSource);
            crystalReportViewer1.Visible = true;
            crystalReportViewer1.ReportSource = rdSample;
            crystalReportViewer1.Refresh();

Thursday, 14 June 2012

To Kill the Database transaction using the query


//Below query for getting system process id in the master database
select  kpid,* from sysprocesses where dbid=DB_ID('Training')


//Based on the process id we can kill the process of Database
kill kpid[processId] eg:- kill 53


Wednesday, 13 June 2012

Disable CUT,COPY,PASTE in your ASP.NET textbox

 Here I will explain how to disable copy, cut and paste functionality (Ctrl + c/Ctrl + v/ Ctrl+ x) in asp.net textbox using JavaScript.

            To achieve this we have two methods first method is directly set copy, cut and paste options return false in textbox to disable and second one we can use JavaScript functionality to disable copy, cut and paste options.

First Method

<asp:TextBox ID="TextBox1" runat="server" oncopy="return false" oncut="return false" onpaste="return false"></asp:TextBox>


Second Method

JavaScript Function:
         function DisableControlKey(e) {
                  // Message to display
                  var message = "Cntrl key/ Right Click Option disabled";
                  // Condition to check mouse right click / Ctrl key press
                  if (e.which == 17 || e.button == 2) {
                  alert(message);
                  return false;
                     }
                  }
            
In .aspx page
<asp:TextBox ID="txtUser" runat="server" onKeyDown="return DisableControlKey(event)"
onMouseDown="return DisableControlKey(event)"></asp:TextBox>

Tuesday, 5 June 2012

Expiring a page on clicking browser back button in Asp.net?

It is most common in some scenarios for us to expire a page as soon as the user clicks the back button in the web browser. This will be needed in scenarios like, when registering a new user or company in our web application.


For all who need this, I thought of sharing the code which will accomplish the same.


Response.Expires = -1;
Response.Cache.SetNoServerCaching();
Response.Cache.SetAllowResponseInBrowserHistory(false);
Response.CacheControl ="no-cache";
Response.Cache.SetNoStore();


Just paste the above code in the Page_Load event of your asp.net web page for which you want to expire when user clicks browser back button.

Wednesday, 30 May 2012

How to convert the table values to XML file and XSD file

SqlConnection Con = new SqlConnection("server=.;uid=.;Pwd=.;database=.");
SqlDataAdapter da = new SqlDataAdapter("Select top 10 * from SoftLawrence", Con);
DataSet DS = new DataSet();
da.Fill(DS);
DS.WriteXml("F:\\SoftLawrence\\Sample.XML");
DS.WriteXmlSchema("F:\\SoftLawrence\\Test.XSD");

//To convert the Xml file to Dataset
DataSet dsVat = new DataSet();
string appPath =  GetExecutingPath();
dsVat.ReadXml(appPath + "Sample.xml");
 return dsVat;

Wednesday, 16 May 2012

To give Bold in the particular word of the Asp label control text

<asp:Label ID="lblText" runat ="server" Text="To attach a file, type the path of the file in the field below or click on the &lt;b&gt;Browse&lt;/b&gt; button to locate it in your computer." />

Tuesday, 1 May 2012

Use of COALESCE in the SQL Server

DECLARE @RegistrationNo VARCHAR(1000);    

                    SELECT  @RegistrationNo = COALESCE( CASE WHEN CAST(@RegistrationNo AS VARCHAR(1000)) IS NOT NULL AND @RegistrationNo<>'' then CAST(@RegistrationNo AS VARCHAR(1000)) + ', ' end, '') +''+ CAST(RegistrationNo AS VARCHAR(100))   

                    FROM Table_Name

                    SELECT '[' + @RegistrationNo + ']'