Click here to Skip to main content
15,905,558 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi, This is my second post sorry if i'm missing any threads or any mistake.

While clicking on page setup i'm able to use the controls but the problem is , it is showing me some code in the setup and while clicking on print preview it is displaying me a blank page so even when i'm trying to take a print it is giving a blank page. Please help me who came cross this type of problem or whoever knows.

Here is my complete code:

C#
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;
using System.Diagnostics;
using System.Drawing.Printing;
using System.Data.SqlTypes;
using Microsoft.SqlServer.Server;
using System.Timers;

namespace ComplaintReport
{
    public partial class MRU240 : Form
    {

        private PrintPreviewDialog Pv;
        private PageSetupDialog Ps;
        private PrintDocument Pd;
        private PrintDialog Pr;

        private System.Windows.Forms.MainMenu mainMenu1;
        private System.Windows.Forms.MenuItem mnuFile;
        private System.Windows.Forms.MenuItem mnuSetup;
        private System.Windows.Forms.MenuItem mnuPreview;
        private System.Windows.Forms.MenuItem mnuPrint;
        private System.Windows.Forms.Button cmdQuit;

         public MRU240()
        {
            InitializeComponent();

            Pv = new PrintPreviewDialog();
            Ps = new PageSetupDialog();
            Pr = new PrintDialog();
            Pd = new PrintDocument();

            Pd.DocumentName = "My New Document";
            Pv.Document = Pd;
            Ps.Document = Pd;
            Pr.Document = Pd;
        }

       
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string cs = "Data Source=192.168.1.133;database=ComplaintReport;user id=sa;password=ssdrnd";
            SqlConnection conn = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand();
            conn.Open();
           
            SqlDataAdapter da = new SqlDataAdapter("select Mno from MRU240", conn);
            DataSet ds = new DataSet();
            cmd = new SqlCommand("select Mno from MRU240", conn);
            da.Fill(ds, "mru240");

            comboBox1.DataSource = ds.Tables["mru240"];
            comboBox1.DisplayMember = "Mno";
            conn.Close();
           
            
        }

      
        private void toolStripMenuback_Click(object sender, EventArgs e)
        {
            SSCVR obj6 = new SSCVR();
            obj6.Show();
            this.Hide();
        }
       

        private void MRU240_Load(object sender, EventArgs e)
        {
            comboBox1.DropDownHeight = comboBox1.ItemHeight * 10;
            comboBox1.MouseWheel += new MouseEventHandler(comboBox1_MouseWheel);
   
          

        }
        void comboBox1_MouseWheel(object sender, MouseEventArgs e)
        {
            ((HandledMouseEventArgs)e).Handled = true;
        }
        


        private void btnexit_Click(object sender, EventArgs e)
        {
            SSCVRGSE obj4 = new SSCVRGSE();
            obj4.Show();
            this.Hide();
        }



        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string cs = "Data Source=192.168.1.133;database=ComplaintReport;user id=sa;password=ssdrnd";
            SqlConnection conn = new SqlConnection(cs);
            if (txtsno.Text == "")
            {
                MessageBox.Show("Please enter serial number");
                txtsno.Focus();
            }
            if (txtsno.Text.Length >= 7)
            {
                MessageBox.Show("Serial number below 7 charachters ");
                txtsno.Focus();
            }


            try
            {
                string cfd = txt1.Text;
                string Uno = txt2.Text;
                string Mno = txtsno.Text;
                string cprc = txt3.Text;
                string sdc = txt4.Text;
                string contact = txt5.Text;
                string orderno = txt6.Text;
                string ino = txt7.Text;
                string consig = txt8.Text;
                string pft = txt9.Text;
                string remarks = txt11.Text;
                string obsv = txt12.Text;
                string disptach = txt13.Text;
                string repair = txt14.Text;
                string cause = txt15.Text;
                string userinfo = txt16.Text;
                string remedial = txt17.Text;
                string feedbck = txt18.Text;
                string conclu = txt19.Text;
                string notes = txt20.Text;
           
                SqlCommand cmd = new SqlCommand("insert into MRU240 values(@cfd,@Uno,@Mno,@cprc,@sdc,@contact,@orderno,@ino,@consig,@pft,@remarks,@obsv,@dispatch,@repair,@cause,@userinfo,@remedial,@feedbck,@conclu,@notes)", conn);
                cmd.Parameters.AddWithValue("@cfd", cfd);
                cmd.Parameters.AddWithValue("@Uno", Uno);
                cmd.Parameters.AddWithValue("@Mno", Mno);
                cmd.Parameters.AddWithValue("@cprc", cprc);
                cmd.Parameters.AddWithValue("@sdc", sdc);
                cmd.Parameters.AddWithValue("@contact", contact);
                cmd.Parameters.AddWithValue("@orderno", orderno);
                cmd.Parameters.AddWithValue("@ino", ino);
                cmd.Parameters.AddWithValue("@consig", consig);
                cmd.Parameters.AddWithValue("@pft", pft);
                cmd.Parameters.AddWithValue("@remarks", remarks);
                cmd.Parameters.AddWithValue("@obsv", obsv);
                cmd.Parameters.AddWithValue("@dispatch", disptach);
                cmd.Parameters.AddWithValue("@repair", repair);
                cmd.Parameters.AddWithValue("@cause", cause);
                cmd.Parameters.AddWithValue("@userinfo", userinfo);
                cmd.Parameters.AddWithValue("@remedial", remedial);
                cmd.Parameters.AddWithValue("@feedbck", feedbck);
                cmd.Parameters.AddWithValue("@conclu", conclu);
                cmd.Parameters.AddWithValue("@notes", notes);

                conn.Open();
                int i = cmd.ExecuteNonQuery();
                if (i == 1)
                {
                    MessageBox.Show("Record is saved ");
                    clearAll();
                }

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            conn.Close();

        }

        private void comboBox1_SelectedIndexChanged_1(object sender, EventArgs e)
        {
           

            string cs = "server=192.168.1.133;database=ComplaintReport;user id=sa;password=ssdrnd";
            SqlConnection conn = new SqlConnection(cs);
            SqlCommand cmd = new SqlCommand();
            try
            {
                conn.Open();
                int dbsno = int.Parse(comboBox1.Text);

                cmd = new SqlCommand("select * from MRU240 where Mno=@dbsno", conn);
                cmd.Parameters.AddWithValue("@dbsno", dbsno);
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    txt1.Text = dr["cfd"].ToString();
                    txt2.Text = dr["Uno"].ToString();
                    txtsno.Text = dr["Mno"].ToString();
                    txt3.Text = dr["cprc"].ToString();
                    txt4.Text = dr["sdc"].ToString();
                    txt5.Text = dr["contact"].ToString();
                    txt6.Text = dr["orderno"].ToString();
                    txt7.Text = dr["ino"].ToString();
                    txt8.Text = dr["consignee"].ToString();
                    txt9.Text = dr["pft"].ToString();
                    txt12.Text = dr["observation"].ToString();
                    txt13.Text = dr["dispatch"].ToString();
                    txt14.Text = dr["repair"].ToString();
                    txt15.Text = dr["cause"].ToString();
                    txt16.Text = dr["userinfo"].ToString();
                    txt17.Text = dr["remedial"].ToString();
                    txt18.Text = dr["feedback"].ToString();
                    txt19.Text = dr["conclusion"].ToString();
                    txt20.Text = dr["notes"].ToString();
                    txt11.Text = dr["remarks"].ToString();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            conn.Close();
        }

        private void btnupdate_Click_1(object sender, EventArgs e)
        {
            string cs = "Data Source=192.168.1.133;database=ComplaintReport;user id=sa;password=ssdrnd";
            SqlConnection conn = new SqlConnection(cs);

            string cfd = txt1.Text;
            string Uno = txt2.Text;
            string Mno = txtsno.Text;
            string cprc = txt3.Text;
            string sdc = txt4.Text;
            string contact = txt5.Text;
            string orderno = txt6.Text;
            string ino = txt7.Text;
            string consig = txt8.Text;
            string pft = txt9.Text;
            string remarks = txt11.Text;
            string obsv = txt12.Text;
            string disptach = txt13.Text;
            string repair = txt14.Text;
            string cause = txt15.Text;
            string userinfo = txt16.Text;
            string remedial = txt17.Text;
            string feedbck = txt18.Text;
            string conclu = txt19.Text;
            string notes = txt20.Text;
            int dbsno = int.Parse(comboBox1.Text);
            SqlCommand cmd = new SqlCommand("update MRU240 set cfd=@cfd,Uno=@Uno,cprc=@cprc,sdc=@sdc,contact=@contact,orderno=@orderno,ino=@ino,consignee=@consig,pft=@pft,remarks=@remarks,observation=@obsv,dispatch=@dispatch,repair=@repair,cause=@cause,userinfo=@userinfo,remedial=@remedial,feedback=@feedbck,conclusion=@conclu,notes=@notes where Mno=@dbsno", conn);
            cmd.Parameters.AddWithValue("@cfd", cfd);
            cmd.Parameters.AddWithValue("@Uno", Uno);
            cmd.Parameters.AddWithValue("@Mno", Mno);
            cmd.Parameters.AddWithValue("@cprc", cprc);
            cmd.Parameters.AddWithValue("@sdc", sdc);
            cmd.Parameters.AddWithValue("@contact", contact);
            cmd.Parameters.AddWithValue("@orderno", orderno);
            cmd.Parameters.AddWithValue("@ino", ino);
            cmd.Parameters.AddWithValue("@consig", consig);
            cmd.Parameters.AddWithValue("@pft", pft);
            cmd.Parameters.AddWithValue("@remarks", remarks);
            cmd.Parameters.AddWithValue("@obsv", obsv);
            cmd.Parameters.AddWithValue("@dispatch", disptach);
            cmd.Parameters.AddWithValue("@repair", repair);
            cmd.Parameters.AddWithValue("@cause", cause);
            cmd.Parameters.AddWithValue("@userinfo", userinfo);
            cmd.Parameters.AddWithValue("@remedial", remedial);
            cmd.Parameters.AddWithValue("@feedbck", feedbck);
            cmd.Parameters.AddWithValue("@conclu", conclu);
            cmd.Parameters.AddWithValue("@notes", notes);
            cmd.Parameters.AddWithValue("@dbsno", dbsno);
          
            conn.Open();
            int i = (int)cmd.ExecuteNonQuery();
            if (i >= 1)
            {
                MessageBox.Show("Record is updated ");
                clearAll();
                txt2.Text = "";
            }
            else
            {
                MessageBox.Show("Record is not updated");
            }
            conn.Close();
        }
       

        private void txtsno_Leave(object sender, EventArgs e)
        {
            if (txtsno.Text == "")
            {
                MessageBox.Show("Please enter serial number");
                txtsno.Focus();
            }
            if (txtsno.Text.Length >= 7)
            {
                MessageBox.Show("Enter below 7 charachers ");
                txtsno.Focus();
            }

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            DrawIt(e.Graphics);
        }

        private void DrawIt(Graphics G)
        {
            //PrintDocument document = new PrintDocument();
            //document.PrintPage += new PrintPageEventHandler(document_PrintPage);
            CaptureScreen();

        }
        private Bitmap _memoryImage;



        private void CaptureScreen()
        {
            // put into using construct because Graphics objects do not 
            //  get automatically disposed when leaving method scope
            using (var myGraphics = CreateGraphics())
            {
                var s = Size;
                _memoryImage = new Bitmap(1500, 1000, myGraphics);
                using (var memoryGraphics = Graphics.FromImage(_memoryImage))
                {
                    printDocument2.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 826, 1169);
                    memoryGraphics.CopyFromScreen(Location.X, Location.Y, 70, 30, s);
                }
            }
        }
        private void printDocument2_PrintPage(object sender, PrintPageEventArgs e)
        {
            // calculate width and height scalings taking page margins into account
            var wScale = e.MarginBounds.Width / (float)_memoryImage.Width;
            var hScale = e.MarginBounds.Height / (float)_memoryImage.Height;

            // choose the smaller of the two scales
            var scale = wScale < hScale ? wScale : hScale;

            // apply scaling to the image
            e.Graphics.ScaleTransform(scale, scale);

            // print to default printer's page
            e.Graphics.DrawImage(_memoryImage, 70, 30);
            printDocument2.DefaultPageSettings.PaperSize = new System.Drawing.Printing.PaperSize("PaperA4", 826, 1169);
        }


        private void pd_Print(object sender, PrintPageEventArgs e)
        {
            DrawIt(e.Graphics);
        }


        private void pageSetupToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Ps.ShowDialog();
            Pd.DefaultPageSettings = Ps.PageSettings;
            Pd.PrinterSettings = Ps.PrinterSettings;
        }

        private void printPreviewToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Pv.WindowState = FormWindowState.Maximized;
            Pv.ShowDialog();
        }

        private void printToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Pr.ShowDialog() == DialogResult.OK)
                Pd.Print();
        }
    }
}
Posted
Updated 26-Mar-14 21:55pm
v3
Comments
Sergey Alexandrovich Kryukov 27-Mar-14 1:01am    
This is real anti-programming.
—SA
Member 10700515 27-Mar-14 1:31am    
Yes. Do you have any solution for my problem?
Sergey Alexandrovich Kryukov 27-Mar-14 2:43am    
I really don't know. I don't think your code could be considered at all, at this moment. First, you need to write it accurately. At least remove any repetitions like *.Visible = true; *.ReadOnly = true. If you leave it as is, there is nothing to talk about...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900