Thursday, September 29, 2011

textbox should accept only characters

1. write the code below at keypress event of textbox


private void txtFirstname_KeyPress(object sender, KeyPressEventArgs e)
        {


            if ((e.KeyChar >= 65 && e.KeyChar <= 90) || (e.KeyChar >= 97 && e.KeyChar <= 122) || (e.KeyChar == 32) || (e.KeyChar == 8))//keychar 8 is for backspace
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }


it should accept alphabets only.

textbox should accept numbers only

1. write the code below in your textbox keypress event


private void txtPincode_KeyPress(object sender, KeyPressEventArgs e)
  {
      if ((e.KeyChar >= 48 && e.KeyChar <= 57) || (e.KeyChar == 8))//keychar 8 is for backspace
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
  }

Print a particular area of form

1. add print document to your form
2. write the code below in your printDocument_PrintPage event, i am going to print the content of panel3


private void printDocument1_PrintPage(System.Object sender,
           System.Drawing.Printing.PrintPageEventArgs e)
        {
           // e.Graphics.DrawImage(memoryImage, 0, 0);
            float x = e.MarginBounds.Left;
            float y = e.MarginBounds.Top;
            Bitmap bmp = new Bitmap(this.panel3.Width, this.panel3.Height);
            this.panel3.DrawToBitmap(bmp, new Rectangle(0, 0, this.panel3.Width, this.panel3.Height));
            e.Graphics.DrawImage((Image)bmp, x, y);
        }


3. write the code at your print button event

private void btnPrint_Click(object sender, EventArgs e)
        {
          
            printDocument1.Print();

        }

Tuesday, September 27, 2011

Save image in Sql server


1. create table in database

CREATE TABLE [dbo].[tbl_Image_Data] (


        [ID] [int] NOT NULL ,

        [Name] [varchar] (50) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,

        [Picture] [image] NULL

) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]



2. add picturebox and name it pb and two buttons "browse" and "save"



3. First of all browse image from computer to pictureBox

private void btnBrowse_Click(object sender, EventArgs e)
        {
            Stream myStream = null;
           
                OpenFileDialog openFileDialog = new OpenFileDialog();
               // openFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
                openFileDialog.Filter = "Image Files(*.jpg; *.jpeg; *.bmp)|*.jpg; *.jpeg; *.bmp";
                if (openFileDialog.ShowDialog(this) == DialogResult.OK)
                {
                    try
                    {
                        if ((myStream = openFileDialog.OpenFile()) != null)
                        {
                            string FileName = openFileDialog.FileName;
                            txtImagepath.Text = FileName;
                            if (myStream.Length > 512000)
                            {
                                MessageBox.Show("File size limit exceed");
                            }
                            else
                            {
                                //pb is my pictureBox name load image in pb
                                pb.Load(FileName);
                             
                                //stretch image to fit in picturebox
                                pb.SizeMode = System.Windows.Forms.PictureBoxSizeMode.StretchImage;
                            }
                            //MessageBox.Show(myStream.Length.ToString());
                        }
                    }
                    catch (Exception ex)
                    {

                        MessageBox.Show("Error: Could not read file from disk. error: " + ex.Message);

                    }
                }
             

               

        }
2.save this to sql server

private long ImageFileLength = 0;
        private byte[] byte_Img;

private void btnSave_Click(object sender, EventArgs e)
 {
     //retrive the file size by using FileInfo class
     FileInfo fi = new FileInfo(txtImagepath.Text);

      this.ImageFileLength = fi.Length;

      //using FileStream object, fill the byte array.
      FileStream fst = new FileStream(txtImagepath.Text, FileMode.Open,                                 FileAccess.Read, FileShare.Read);

               byte_Img = new byte[Convert.ToInt32(this.ImageFileLength)];

               int iBytesRead = fst.Read(byte_Img, 0, Convert.ToInt32(this.ImageFileLength));

                        fst.Close();

                }

            try
            {
                this.sqlConnection1.Open();
                if (sqlCommand1.Parameters.Count == 0)
                {
                    this.sqlCommand1.CommandText = "INSERT INTO tbl_Image_Data(ID," +
                                   " Name,Picture) values(@ID,@Name,@Picture)";
                    this.sqlCommand1.Parameters.Add("@ID",
                                     System.Data.SqlDbType.Int, 4);
                    this.sqlCommand1.Parameters.Add("@Name",
                                     System.Data.SqlDbType.VarChar, 50);
                    this.sqlCommand1.Parameters.Add("@Picture",
                                     System.Data.SqlDbType.Image);
                }

                this.sqlCommand1.Parameters["@ID"].Value = this.ID.Text;
                this.sqlCommand1.Parameters["@Name"].Value = this.Name.Text;
                this.sqlCommand1.Parameters["@Picture"].Value = this.byte_Img;

                int result = this.sqlCommand1.ExecuteNonQuery();
                MessageBox.Show(Convert.ToString(result));
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                this.sqlConnection1.Close();
            }

  }




Set child form at the center of Mdi parent form


Place this code at page_load event 

this.Location = new Point(this.MdiParent.ClientSize.Width / 2 - this.Width / 2, this.MdiParent.ClientSize.Height / 2 - this.Height / 2 - 50);