Hi all,

Am new to C# and i have a form which registers users,But i want to create methods that which check if the TextBox conatins Letters (ie for coloumns that shouldnt take letters) and check for special characters and number for coloumns that should only have text.
I have created a Method called IsAllNumbers, i need someone to help me on how i will call the method

below is the code that i have written so far

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.Data.Sql;
using System.IO;

namespace UserLogin
{
    public partial class FrmRegisterEnrolee : Form
    {

        # region Constructor
        public FrmRegisterEnrolee()
        {
            InitializeComponent();
        }

        # endregion Constructor 

        # region Variable

        string empID;
        string lname;
        string fname;
        string DOB;
        string gender;
        string tel;
        string address;
        string empr;
        string emprAdd;
        string HMO;
        string HCP;
      //  int del_flg;
       // Image pix;

        # endregion Variable

        private void txtBoxEmpID_TextChanged(object sender, EventArgs e)
        {
            string empID = txtBoxEmpID.Text;

        }

        private void txtBoxLname_TextChanged(object sender, EventArgs e)
        {
            string lname = txtBoxLname.Text;
        }

        private void txtBoxFname_TextChanged(object sender, EventArgs e)
        {
            string fname = txtBoxFname.Text;
        }

        private void dateTimePicker_ValueChanged(object sender, EventArgs e)
        {
            DateTime DOB = dateTimePicker.Value;
        }

        private void txtBoxTel_TextChanged(object sender, EventArgs e)
        {
            string tel = txtBoxTel.Text;
        }

        private void txtBoxAdd_TextChanged(object sender, EventArgs e)
        {
            string address = txtBoxAdd.Text;
        }

        private void txtBoxEmpr_TextChanged(object sender, EventArgs e)
        {
            string empr = txtBoxEmpr.Text;
        }

        private void txtBoxEmprAdd_TextChanged(object sender, EventArgs e)
        {
            string emprAdd = txtBoxEmprAdd.Text;
        }

        private void txtBoxHMO_TextChanged(object sender, EventArgs e)
        {
            string HMO = txtBoxHMO.Text; 
        }

        private void txtBoxHCP_TextChanged(object sender, EventArgs e)
        {
            string HCP = txtBoxHCP.Text;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            if (txtBoxEmpID.Text == "")
            {
                MessageBox.Show("Please Enter Employer ID");
            }

            else if (txtBoxLname.Text == "")
            {
                MessageBox.Show("Please Enter Last Name");
            }
            else
            {
                bool isValid = GetValues();


                //int del_flg = 0;

                try
                {   
                    SqlConnection sqlConn = new SqlConnection(@"Data Source=AYO-PC\SQLEXPRESS;Initial Catalog=NHIS;Integrated Security=True");

                    Bitmap image = new Bitmap(picBoxEmp.Image);
                    ImageConverter converter = new ImageConverter();

                    object temp = converter.ConvertTo(picBoxEmp.Image, typeof(byte[]));
                    byte[] imageBytes = (byte[])temp;

                    string query = "INSERT into Emp (empID, lname, fname, gender, DOB, tel, address, empr, emprAddress, HMO, HCP,pix, del_flg) VALUES (@empID, @lname, @fname, @gender, @DOB, @tel, @address,@empr,@emprAddress, @HMO, @HCP, @pix, 0 )";

                    SqlCommand cmd = new SqlCommand(query, sqlConn);
                    cmd.Parameters.AddWithValue("@empID", txtBoxEmpID.Text);
                    cmd.Parameters.AddWithValue("@lname", txtBoxLname.Text);
                    cmd.Parameters.AddWithValue("@fname", txtBoxFname.Text);
                    cmd.Parameters.AddWithValue("@gender", gender);
                    cmd.Parameters.AddWithValue("@DOB", SqlDbType.DateTime).Value = dateTimePicker.Value;
                    cmd.Parameters.AddWithValue("@tel", txtBoxTel.Text);
                    cmd.Parameters.AddWithValue("@address", txtBoxAdd.Text);
                    cmd.Parameters.AddWithValue("@empr", txtBoxEmpr.Text);
                    cmd.Parameters.AddWithValue("@emprAddress", txtBoxEmprAdd.Text);
                    cmd.Parameters.AddWithValue("@HMO", txtBoxHMO.Text);
                    //cmd.Parameters.AddWithValue(del_flg, 0);
                    cmd.Parameters.AddWithValue("@HCP", txtBoxHCP.Text);
                   cmd.Parameters.AddWithValue("@pix", imageBytes);           

                    cmd.Connection.Open();
                    cmd.ExecuteNonQuery();
                    cmd.Connection.Close();

                    MessageBox.Show(" Principal Registrated Successfully");
                    FrmToDO f1 = new FrmToDO();
                    this.Close();
                    f1.Show();
                  }

                 catch (Exception ex)              
                 {
                     MessageBox.Show(ex.Message);
                    // MessageBox.Show("Registration failed, Please check and Try again");
                 }              
           }
        }
        private bool GetValues()
        {
            bool isValid = true;

            if (isValid == false)
            {
                  isValid = IsAllDigits(txtBoxEmpID.Text);
                  MessageBox.Show("Please Enter Valid Digits for Employee Id");                  
            }

            else
              empID = txtBoxEmpID.Text;
                lname = txtBoxLname.Text;
              fname = txtBoxFname.Text;
              gender = null;
              if (radMale.Checked) gender = radMale.Text;
              else gender = radFem.Text;

              DOB = dateTimePicker.Value.ToString("DD-Mon-YYYY");
            //DateTime.tryparse
              tel = txtBoxTel.Text;
             address = txtBoxEmprAdd.Text;
            empr = txtBoxEmpr.Text;
            emprAdd = txtBoxEmprAdd.Text;
            HMO = txtBoxHMO.Text;
            HCP = txtBoxHCP.Text;
            if (isValid == true)
            {
                Image pix = picBoxEmp.Image;
            }
            else
            {
                MessageBox.Show("Please Upload a picture to Complete Registration");
            }
            return isValid;
        }

        private bool IsAllDigits(string s)
        {
            foreach (char c in s)
            {
                if (!Char.IsDigit(c))
                    return false;
            }
            return true;
            }
        # region Event
        private void btnUploadPix_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog open = new OpenFileDialog();
                open.Filter = "Image Files(*.jpg; *.jpeg; *.gif; *.bmp)|*.jpg;";
                if (open.ShowDialog() == DialogResult.OK)
                {
                    Image image = new Bitmap(open.FileName);
                    picBoxEmp.Image = image.GetThumbnailImage(100, 120, null, new IntPtr());
                    open.RestoreDirectory = true;
               }
            }
            catch
            {
                MessageBox.Show("Cannot upload Image");
            }
        }

        # endregion Event

    }
}

Recommended Answers

All 4 Replies

Not related to the question but:

        private bool GetValues()
        {
            bool isValid = true;
            if (isValid == false)
            {
                  isValid = IsAllDigits(txtBoxEmpID.Text);
                  MessageBox.Show("Please Enter Valid Digits for Employee Id");                  
            }

You will never hit the line isValid = IsAllDigits(txtBoxEmpID.Text); because your setting the variable to true in instanciation and then immediately checking its false. It will never be false as nothing has the chance to change it.

Thanks MikeyIsMe

what do i Do? remove bool isValid =true ;

Well it depends what your trying to do with the logic. I would declare it as false, so that it always runs on the first attempt.

But then you would need to put the code below that into a new if statement checking the variable again else it would never reach that logic due to the first part of the if statement being run.

Thanks once again, but can you help me rewrite the statement so that it checks to see that the EMPID is all number and if it isnt, it displays the Empid must be in Number.
I would also want it to enforce that Picture must be uploaded before it can be saved.
thanks a bunch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.