How can Take Print Receipt and Save the Data in Database in Windows Forms Application

By
Hi friends, here i am going to make an Application "how can take Print Receipt and save data in database in windows Forms application".This project is very helpful for your Real Time Project Development and you can achieve more knowledge from this projects. You can download whole Application from the bottom of this page.
There are some steps ,Please follow it 
Step1:- Open your visual studio-> File->Click New Project->Select Windows Forms Application  -> Select c# language from left window->write your application name->click OK.
SEE IT:->



Step2:-  Now open Form1.cs(Design) File->Drag and drop label ,Text Box and Button Control from Toolbox-> change the label name as shown below


Step3:- Add New Form form solution Explorer windows->go Form2.cs(Design) File->Drag and drop TableLayoutPanel From Toolbox-> go Property of TableLayoutPanel and change the RowCount =7.

SEE IT:->

Step4:- Drag and drop label control from Toolbox-> change the Label Name as below: see it:-


Step5:- Now go  Property o each  label (9,10,11,12,13,14,15) and change Each label Modifiers=Public
SEE IT:-

Step6:- Now drag and drop printDocument1 and printPreviewDialog1 control from the Toolbox on the Form 2-> go property of printPreviewDialog1 and select Document =  printDocument1.
see it:-




Step7:-  Create Studentdetail1 table in SQL Database for save the Form1 data on click on Submit Button.
see it:



Step8:- Now go Form 1->Double click on Submit Button -> Write the following codes which are given below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace print_recipt
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        SqlDataAdapter da;
        DataSet ds;
        SqlConnection con;
        private void button1_Click(object sender, EventArgs e)
        {
        con = new SqlConnection("data source=RAMASHANKER-PC;Integrated Security=Yes;Database=master");
 da = new SqlDataAdapter("insert into studentdetail1 values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "')", con);
            ds = new DataSet();
            da.Fill(ds);
            //MessageBox.Show("Fee has been successfully Submitted");

            Form2 f2 = new Form2();
            this.Hide();
            f2.label9.Text = textBox1.Text.ToString();
            f2.label10.Text = textBox2.Text.ToString();
            f2.label11.Text = textBox3.Text.ToString();
            f2.label12.Text = textBox4.Text.ToString();
            f2.label13.Text = textBox5.Text.ToString();
            f2.label14.Text = textBox6.Text.ToString();
            int n1 = int.Parse(textBox5.Text);
            int n2 = int.Parse(textBox6.Text);
            int total = n1 + n2;
            f2.label15.Text = total.ToString();
            f2.Show();
        }
    }
}

  • Write the above code on the Form1.cs File.
See it:-


Step9:- Now go Form2.cs(Design) File-> Double click on Preview button-> write the following code which are given below:-
using System;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Printing;

namespace print_recipt
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        [System.Runtime.InteropServices.DllImport("gdi32.dll")]
        public static extern long BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
        private Bitmap memoryImage;
        private void CaptureScreen()
        {
            Graphics mygraphics = this.CreateGraphics();
            Size s = this.Size;
            memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
            Graphics memoryGraphics = Graphics.FromImage(memoryImage);
            IntPtr dc1 = mygraphics.GetHdc();
            IntPtr dc2 = memoryGraphics.GetHdc();
            BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
            mygraphics.ReleaseHdc(dc1);
            memoryGraphics.ReleaseHdc(dc2);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            CaptureScreen();
            printPreviewDialog1.ShowDialog(); 
        }

        private void printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(memoryImage, 0, 0);
        }

    }
}
  • Write the above code on the Form2.cs File.
see it:-

Step10:- 
  • Now Run The Application(Press F5)-> Fill the data on the Form1.cs(Design) File.
see it:-

  • Click the Submit button-> then you will go to the Form 2.
see it:

  • Now click the Preview button -> Take printout on click on print button.
see it:-

Step11: Now you can see the studentdetail1 table data after the printout of the form.
see it:-
For more:  
  1. Add sql Database(.mdf) in Asp.Net ApplicationPrint the Grid Data in Windows FormsApplication
  2.  Create .dll File and Use in Asp.netApplication 
  3.  Create a Setup File
  4.  File Handling Real Application
  5. Ado.Net Application
  6.  Make Captcha image for Asp.NetApplication
  7. E-Post System Project
  8. How to use application state and global.asax file in asp.net
  9. How to use binding Navigator controls in asp.net
  10. Web Form controls in asp.net
  11. How to solve sql server database (.mdf) file problems
  12. How to make custom login and Registration page
  13. Call by value and call by reference in c#
  14. Data set and Data adopter in c#
To Get the Latest  Free Updates Subscribe
Click below for download whole application
           DOWNLOAD

36 comments:

  1. [System.Runtime.InteropServices.DllImport("gdi32.dll")] in this error occur

    ReplyDelete
  2. @Sunny Dhiman ,there are some missing .dll file in your visual studio.follow the below steps

    open your solution Explorer -->right click on project--->Add Reference--> .NET--> select all InteropServices one by one-->ok .then you can run this application without error.

    ReplyDelete
  3. I need to do a textile billing application.. how can i use the printing . here it takes the screen shot fully.. i need just the contents.. what to do?

    ReplyDelete
    Replies
    1. you can use session variable to transfer value from one page to another page and use java script code for printing.

      Delete
  4. Replies
    1. first use session variable for transfer the control value to another page and use java script or j query for printing this page.

      Delete
  5. Hii i have an application where there are lots of lables and textboxes here i want to print the text of lables and text inside the textbox and i did it but only printing the content which is fitting in one page in preview im unable to extend the content to print to next page ...so how can I do it Please help me

    ReplyDelete
    Replies
    1. Hi Muzammil tell me,which type of application you are using windows Forms or asp.net.you can send snapshot of your page on my Email-ramashanker@outlook.com .then i can help you.

      Delete
  6. thanks it s really nice. i have one doubt from datagridview. i save student details name,id,phone. and show in gridview . then i doubleclick the any row in gridview another form need to open and get value form datagrid selected value and display on name field name, id field id, and so on. guide me my mail id manojija007@gmail.com

    ReplyDelete
  7. Mine data is not shown in the print page..data is being inserted but not shown when i click on the preview button,its shows me blank page to print

    ReplyDelete
    Replies
    1. @Ravinder singh saini, follow step 5 carefully .it will work definetly......

      Delete
  8. I have followed it sir,i used try-catch now it gives invalid printer exception.

    ReplyDelete
  9. hey sir when i click on print button it doesn't show any preview...

    ReplyDelete
  10. hi suraj,go properties of each control on form2,change Modifier==public,see step 5 ,i have already mentioned it.

    ReplyDelete
  11. DEAR SIR THIS CODE ONLY USE IN WEB FORM , THAN HOW USE THIS CODE FOR WEB APPLICATION DATA ENTRY IN C#
    BECAUSE MY DATA ENTRY FORM IN WEB APPLICATION
    PLEASE GUIDE ME

    ReplyDelete
    Replies
    1. hi sandesh, you can use same concepts in web application also but you need to write same code as web application using c#.i have already posted many post please read it and implement in your projects.

      Delete
  12. Thank you so much for your post because this is very useful for me at the end time of my project...

    tc...gn

    ReplyDelete
  13. sir i need ado.net material will plzz forward to this mail id.
    raja.sekhar0011@gmail.com

    ReplyDelete
  14. I am going to write a code for print page and receipts in asp.net,c#. whenever we works on any website and software – we need to print some report and date using printer. So now we are going to learn write thing on write place.
    So for this execution we need to follow some step. You can also find here attached application here Print demo application
    Firstly we creates a new website and a newpage and create new app_code folder .
    Here we writes a code for print in app_code folder that is work as class file.
    Add a class file in app_code folder with te name of PrintHelper.cs.
    Code for PrintHelper.cs in App_code : -
    http://nirajtiwari.com/code-print-receiptpage-asp-net-c/

    ReplyDelete
  15. hi sir,
    thank u for sending me the material. And one more request will please guide how can i prepare the ado.net and get succeed in get the job please give some guide lines.....

    ReplyDelete
  16. hi sekhar ! only focus on your concepts.If you know concepts very well ,I hope ,you will get job definitely.There are some points which you have to follow.
    1.) English communication
    2.) Technical Knowledge(aptitude,reasoning)
    3.) Language knowledge(c or c++ or c# or java)
    Attention :-Now days English communication is more important......
    you can learn aptitude,reasoning and other technical things from below url.
    http://www.indiabix.com/
    if any query ask again.....

    ReplyDelete
  17. i sir may i knw where r u from and will please give me ur phne no to contact if u dnt mine i wan clarify my douts through call.

    ReplyDelete
  18. its also showing button on print

    ReplyDelete
  19. hiiiiiiii,Actually i have done integration part of camera in c# windows application through metricam. now i want code for capturing image and that images is save in Database.Please Help me. send me that code on my email id
    vaibhavb029@gmail.com

    ReplyDelete
    Replies
    1. Refer below link it may be helpful for you...
      http://www.codeproject.com/Articles/125478/Versatile-WebCam-C-library

      Delete
  20. Replies
    1. hi Krishnan ! it may be version problem,so you have to create your own sql database on your system .

      Delete
  21. hello sir i have tried ur code but i am getting print of half windows form nly plzzz help?

    ReplyDelete
    Replies
    1. hi alex ! Follow step 5 carefully.you can print only visible screen on your desktoponly in windows form application.

      Delete
  22. hello mr.ramashanker i put your code in my form which has textbox so its not working why??? ...

    ReplyDelete
  23. hello mr.ramashankar .. i want to print form which has textbox and picturebox with image but this code not working ... how to print colorful form with images form

    ReplyDelete
    Replies
    1. hi priyank ! follow step 5 ,6 carefully,set all control's modifier = public,.Then you can print whole form without any problem,For color print, you have to use color printer...

      Delete
  24. this doesn't work, errer "document does not contain any pages", i think the Form2 you post lost a little code. :|

    ReplyDelete
  25. bhaii apni chaii ki dukaan hai , ye sofware bnaya , but wo peela wala form kaise bnauu, koi btaega to usko free chaii pilaunga

    ReplyDelete
  26. in place of sql server db want to use ms excel, can you help to get the method

    ReplyDelete
  27. Hi,

    For me data is not shown in the print page..data is getting inserted in database also but not shown when i click on the preview button,its shows me blank page to print

    and I am getting these errors

    Could not copy "obj\x86\Debug\PrintReceipt1.exe" to "bin\Debug\PrintReceipt1.exe".Exceeded retry count of 10.Failed.

    Unable to copy file "obj\x86\Debug\PrintReceipt1.exe" to "bin\Debug\PrintReceipt1.exe".The process can not access the file "bin\Debug\PrintReceipt1.exe" because it is being used by another process.

    ReplyDelete

Powered by Blogger.