September 24, 2015

How to make a TextBox accept only alphabetic characters?

 private void txtname_TextChanged(object sender, EventArgs e)
        {
            if (!System.Text.RegularExpressions.Regex.IsMatch(txtname.Text, "^[a-zA-Z]"))
            {
                MessageBox.Show("This textbox accepts only alphabetical characters");
                txtname.Text = "";
            }
        }

September 22, 2015

How to make a TextBox accept only Numeric values ?

private void txtEmpCode_TextChanged_1(object sender, EventArgs e)
        {
            if (System.Text.RegularExpressions.Regex.IsMatch(txtEmpCode.Text, "[^0-9]"))
            {
                MessageBox.Show("Please enter only numbers.");
                txtEmpCode.Text.Remove(txtEmpCode.Text.Length - 1);
                txtEmpCode.Text = "";
            }
        }

September 15, 2015

How to upload image to database in C#.net Windows Application

To Upload

    SqlConnection cn= new SqlConnection(connectionString);
    OpenFileDialog open = new OpenFileDialog();
    open.Filter = "Image Files(*.jpeg;*.bmp;*.png;*.jpg)|*.jpeg;*.bmp;*.png;*.jpg";
    if (open.ShowDialog() == DialogResult.OK)
    {
        textBoximage.Text = open.FileName;
    }

    cn.Open();
    string image = textBoximage.Text;
    Bitmap bmp = new Bitmap(image);
    FileStream fs = new FileStream(image, FileMode.Open, FileAccess.Read);
    byte[] bimage = new byte[fs.Length];
    fs.Read(bimage, 0, Convert.ToInt32(fs.Length));
    fs.Close();
    SqlCommand cmd = new SqlCommand("insert into TableName(imgdata) values(@imgdata)",cn);
    cmd.Parameters.AddWithValue("@imgdata",SqlDbType.Image).Value=bimage;
    cmd.ExecuteNonQuery();
    cn.Close();
}


To Retrieve

cn.Open();
 SqlDataAdapter da = new SqlDataAdapter(new SqlCommand("Select Picture From TableName where FieldName=condition ", cn));
 DataSet ds = new DataSet();
 da.Fill(ds);
 byte[] myImage = new byte[0];
 myImage = (byte[])ds.Tables[0].Rows[0]["Picture"];
 MemoryStream stream = new MemoryStream(myImage);
 pictureBox1.Image = Image.FromStream(stream);
 cn.Close();

September 8, 2015

Customize MessageBox in C#

  MessageBox.Show("your message", "window title", MessageBoxButtons.OK,MessageBoxIcon.Asterisk);

September 2, 2015

Open a form using short cut in C sharp

 if (e.Alt && e.KeyCode == Keys.c)
            {
                ChangePassword  f3 = new ChangePassword();
                f3.ShowDialog();

            }


and
set the KeyPreview=True