Search (Article Or Program)

11 September 2013

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


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 dropTableLayoutPanel 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:
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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:-
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
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, intnWidth, 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:-

Click below for download whole application
           DOWNLOAD


ref: http://www.msdotnet.co.in/2012/06/how-can-take-print-receipt-and-save.html