Pages

Saturday, 25 October 2014

Crystal Report in windows form application

Overview : Crystal reports are best used for printing document. Crystal reports provide a great printing experience. In this article I will tell you about how to add crystal report to your application and it's working.

Initially for this we need to take a DataSet from Add New Item in our application, and after adding the dataset we need to create a DataTable in our DataSet. I am creating a table with the name DataTable2. After adding table add some columns in your table as per your requirements. I am adding these columns as shown.
   

Now we need to add a crystalreport to our application from Add New Item in Reporting section as follows : 

After adding Crystal Report, a window named Database Expert appears, in this window expand Project Data, then expand ADO.NET Dataset then expand your dataset which you have created earlier and finally select your table and press OK.

Now you will see five sections in your crystal report as 
1. Section 1 (Report Header) : This section will be printed on the first page of the report.
2. Section 2 (Page Header) : This section will appear in all printed pages of your report.
3. Section 3 (Details) : This section will be repeated according to your data in DataTable.
4. Section 4 (Report Footer) : This section will be repeated once in a report.
5. Section 5(Page Footer) : This section will appear in footer of every page.

After placing data at appropriate place, your report is ready for print command, Now we will see the code to print the report.

The below shown code will be placed on the Click command of the button on which the report will be printed : 

First of all we will declare a DataTable with same name as in DataSet as follows : 

                            DataTable DataTable2 = new DataTable();
                            DataTable2.Columns.Add("date", typeof(string));
                            DataTable2.Columns.Add("agent", typeof(string));
                            DataTable2.Columns.Add("amount", typeof(string));
                            DataTable2.Columns.Add("receivedby", typeof(string));

now populate the DataTable with data as follows : 


                            DataRow dr = DataTable2.NewRow();
                            dr[0] = "column1";
                            dr[1] = "column2";
                            dr[2] = "column3";
                            dr[3] = "column4";

                            DataTable2.Rows.Add(dr);

After this the print command :

crystalreport1 crr1 = new crystalreport1();
crr1.Database.Tables["DataTable2"].SetDataSource((DataTable)DataTable2);
crystalReportViewer1.ReportSource = crr1;

This code will print the crystal report to your default printer.

NameSpace for this action : 

using System.IO;
using System.Drawing.Printing;


Happy Coding....

Friday, 24 October 2014

How to change SCHEMA name in SQL SERVER

  1. Description:
 A Database schema is a described structure in a language supported by DBMS.

Example:
USE databasename;
GO
ALTER SCHEMA new_schema TRANSFER old_schema.TableName;
GO

Sunday, 19 October 2014

User control in ASP.NET

Usercontrols can be understood as a portion of web page created separately as some other page but can be called at any location withing a web page. The advantage of creating usercontrol in an application is that you need not to write the same code again and again.


Adding usercontrol to website :  
  

   Add usercontrol by simply adding new item to your site as shown in image : 
 


Adding usercontrol to a page : 

  To add a usercontrol to a page you need to write this code on top : 

<%@ Register src="~/usercontrol/mainheader.ascx" TagName="UserInformation" TagPrefix="UserInformation" %>

after adding the above code on top of the page, now you just need to place the code on the place you need to add the usercontrol

<UserInformation:UserInformation id="UserInformation" runat="server"/> 


As Simple As That   


Sunday, 29 June 2014

Export data to EXCEL form ListView in Windows Application

Overview :  While working on windows form application sometimes we need to copy all data of listview to an EXCEL sheet. This is very important and needy task in most of the application because EXCEL provides as a great and easy data management platform for those who are not very much expert in handling large database, I am talking about the end user of the application.

Example: Suppose I have a listview in my form having columns as described below -

             listView1.Columns.Add("Item Code", 120);
            listView1.Columns.Add("Party Name", 100);
            listView1.Columns.Add("Brand", 80);
            listView1.Columns.Add("Model No", 100);
            listView1.Columns.Add("Item Name", 100);
            listView1.Columns.Add("Category", 80);
            listView1.Columns.Add("Size", 50);
            listView1.Columns.Add("Color", 70);
            listView1.Columns.Add("Quantity", 50);
            listView1.Columns.Add("Purchase Rate", 90);
            listView1.Columns.Add("Sale Rate", 90);
            listView1.Columns.Add("Disc(%)", 50);
            listView1.Columns.Add("Final Price", 70);
            listView1.Columns.Add("Branch", 100);

 I have a button placed somewhere on the form, so code for the button is as follows -

            copyAlltoClipboard(listView1);
            Microsoft.Office.Interop.Excel.Application xlexcel;
            Microsoft.Office.Interop.Excel.Workbook xlWorkBook;
            Microsoft.Office.Interop.Excel.Worksheet xlWorkSheet;
            object misValue = System.Reflection.Missing.Value;
            xlexcel = new Excel.Application();
            xlexcel.Visible = true;
            xlWorkBook = xlexcel.Workbooks.Add(misValue);
            xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1);
            Excel.Range CR = (Excel.Range)xlWorkSheet.Cells[1, 1];
            CR.Select();
            xlWorkSheet.PasteSpecial(CR, Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing, true);


function copyAlltoClipboard(ListView lv) used on buttins's click event as follows -

        public void copyAlltoClipboard(ListView lv)
        {
            StringBuilder buffer = new StringBuilder();

            for (int i = 0; i < lv.Columns.Count; i++)
            {
                buffer.Append(lv.Columns[i].Text);
                buffer.Append("\t");
            }

            buffer.Append("\n");

            for (int i = 0; i < lv.Items.Count; i++)
            {
                for (int j = 0; j < lv.Columns.Count; j++)
                {
                    buffer.Append(lv.Items[i].SubItems[j].Text);
                    buffer.Append("\t");
                }

                buffer.Append("\n");
            }

            Clipboard.SetText(buffer.ToString());
        }

NemaSpace used : 

using Excel = Microsoft.Office.Interop.Excel;
using fm = System.Windows.Forms;