Email: Password:       Forgot Password 
    .netCodeSG
A Saarsha Group Online Community for dot net codes group like C#, Asp.NET, VB.NET, Sharepoint, JavaScript, JQuery, Ajax, SQL, WCF, WPF.
 
TECHNOLOGIES:
 

Print Receipt and Save Data

Article:
Viewed:  1468 
Posted On:  07/10/2014 06:53:48 
How we can print a receipt in C# ? How we can save our data into sql server ? 

In this article I am going to show how we can make print of our window form. Here in this example I am using a PatientDetail form. I am inserting data into SQL SERVER  Data Table and printing the slip.

Below is my Form1.cs

1.png

Image 1.

On Submit button click I am opening another form and inserting this form data into sql server.

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 PatientsAdmitReport
{
    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=MyPC\SQLSERVER2K8;INTEGRATED SECURITY=TRUE;DATABASE=TEST");
            da = new SqlDataAdapter("INSERT INTO Patient(PatientName,Age, Address,ContactNo, EmergencyContactNo, Sex) VALUES('" +
                                                    txtPatientName.Text +
','" + txtAge.Text + "','" + txtAddress.Text + "','" + txtContactNo.Text + "','" +                                 
                                               txtEmergencyContactNo.Text +
"','" + cmbSex.SelectedItem.ToString() + "')", con);
            ds = new DataSet();
            da.Fill(ds);
 
            Form2 frm2 = new Form2();
            this.Hide();
            frm2.lblPatientName.Text = txtPatientName.Text.ToString();
            frm2.lblAge.Text = txtAge.Text.ToString();
            frm2.lblAddress.Text = txtAddress.Text.ToString();
            frm2.lblContactNo.Text = txtContactNo.Text.ToString();
            frm2.lblEmergencyContactNo.Text = txtEmergencyContactNo.Text.ToString();
            frm2.lblSex.Text = cmbSex.SelectedItem.ToString();
            frm2.Show();
        }
    }
}
 

Below is My form2. Here you can design this from as you want your receipt. Here I am using TableLayOut Panel and some label. I also add PrintPreviewControl and PrintPreviewDialog on this form2.

2.png

Image 2.

Now I am showing my data in label7, label8, label9…… label12. As you can see I am using these label on form1. So I need to change Modifier property of these label like below.

3.png

Image 3.

 Now Right click on printPreviewDialog1 and select properties and set Document Properties to printDocument1 like below.

4.png 

Image 4.

Now Right Click on printDocument1 and select Properties -> In event set PrintPage event like below.

5.png

Image 5.

Now the code of form2 is.

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.Drawing.Printing;
 
namespace PatientsAdmitReport

{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }
 
        private void print_Click(object sender, EventArgs e)
        {
            PrintScreen();
            printPreviewDialog1.ShowDialog();
        }
       
        [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 PrintScreen()
        {
            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 printDocument1_PrintPage(object sender, PrintPageEventArgs e)
        {
            e.Graphics.DrawImage(memoryImage, 0, 0);
        }      
    }
}
 

Now Run the Application:

6.png

Image 6. 

Here fill the patients details and click on Submit.

7.png
 

Image 7.

This is the slip here click on Print button.

8.png

Image 8.

From here by clicking on  Print option you can print the slip. You can also increase the size of slip.

Now see in data base.

9.png

Image 9. 

Below is my Table design.

10.png

Image 10.

  Comment:
         HOME   |   Submit Article   |   Contact Us   |   About Us   |   Terms & Condition   |   Advertise With us