calcuate Text box with ID or it should save empty data

Category: c# language

Question

Mohammed Abdul Muqeet on Sat, 17 Nov 2012 07:23:40


I have a ID number to calculate in text box i want to calculate the textbox if the id is  entered by user or else it should be save empty data

this is old process but i want to enter empty data as well if id enters it should calculate or else empty data will b save

this the class for calculating the ID 

namespace KSMC_EmpDtl
{
    public class NID
    {
        public string valid(string ID)
        {
            int i = 0, TOT = 0, inti = 0;
            string temps;
            int type = Int16.Parse(ID.Substring(0, 1));
            //--------------Start Validating ( Step1 )----------------
            if (ID.Length == 10)
            {

                foreach (char value in ID)
                {
                    inti = (int)char.GetNumericValue(value);
                    if ((i % 2) == 0)
                    {
                        temps = (inti * 2).ToString();
                        foreach (char s in temps)
                        {
                            int m = (int)char.GetNumericValue(s);
                            TOT = TOT + m;
                        }
                    }
                    else
                    {
                        TOT = TOT + inti;
                    }
                    i++;
                }
                //------------Check the result of step1with the last number  (Step2)-------------------


                if (TOT % 10 != 0)
                {
                    return "Wrong ID";
                }
                else
                {
                    
                    if (type == 1)
                        return "American";
                    else if (type == 2)
                        return " Non American";
                    else if (type == 3)
                        return " Temporary Visa";
                    else
                        return "Wrong ID";
                }
            }
            else
            {
                return "Wrong ID";
            }
        }

        public int valid_re_int(string ID)
        {
            int i = 0, TOT = 0, inti = 0;
            string temps;
            int type = Int16.Parse(ID.Substring(0, 1));
            //--------------Start Validating ( Step1 )----------------
            if (ID.Length == 10)
            {

                foreach (char value in ID)
                {
                    inti = (int)char.GetNumericValue(value);
                    if ((i % 2) == 0)
                    {
                        temps = (inti * 2).ToString();
                        foreach (char s in temps)
                        {
                            int m = (int)char.GetNumericValue(s);
                            TOT = TOT + m;
                        }
                    }
                    else
                    {
                        TOT = TOT + inti;
                    }
                    i++;
                }
                //------------Check the result of step1with the last number  (Step2)-------------------


                if (TOT % 10 != 0)
                {
                    return 9;//WRONG ID
                }
                else
                {
                    if (type == 1)
                        return 1;// American
                    else if (type == 2)
                        return 2;// NON American
                    else if (type == 3)
                        return 3;// TEMP VISA
                    else
                        return 9;//WRONG ID
                }
            }
            else
            {
                return 9;//WRONG ID
            }
        }
    }
}

if (National_ID.Text != "")
            {
                NID a = new NID();
                int n;
                n = a.valid_re_int(National_ID.Text);
                
                if (n > 3)
                {

                    Message("Please enter the ID", this);
                    return;
                }
                else
                    National_ID.Focus();
            }

Replies

Dave A Gordon on Sat, 17 Nov 2012 11:11:53


if(Textbox1.Text.Length > 0) { //Validate the ID

NID.valid(Textbox1.Text);

} else // { //Save Empty ID }


Mohammed Abdul Muqeet on Sun, 18 Nov 2012 11:45:30


i tried like this  its saving empty data but its not calculating the ID when it is entered

 protected void Button1_Click(object sender, EventArgs e)
        {
            if (National_ID.Text.Length > 0)
            {

                //if (National_ID.Text != "")
                //{
                NID a = new NID();
                int n;
                n = a.valid_re_int(National_ID.Text);

                if (n > 3)
                {

                    Message("Please enter correct ID", this);
                    return;
                }
                else
                    National_ID.Focus();

                
                try
                {
                    if (Emp_ID.Text == "" || Job.Text == "" || section.Text == "" || Another_job.Text == "" || health_no.Text == "")
                    {
                        Message(" Please enter all details ", this);
                        return;

                    }
                    else
                    {
                        try
                        {


                                                       con.Close();
                            con.Open();
                            SqlCommand cmd = new SqlCommand();
                            cmd.CommandType = CommandType.StoredProcedure;
                            cmd.CommandText = "Emp_Detail";
                            //cmd.Parameters.AddWithValue("@Name", Name.Text);
                            cmd.Parameters.AddWithValue("@Emp_No", Emp_ID.Text);
                            cmd.Parameters.AddWithValue("@Job_Title", Job.Text);
                            cmd.Parameters.AddWithValue("@Nationality", DropDownList2.SelectedItem.Text);
                            cmd.Parameters.AddWithValue("@Administration", TextBox1.Text);
                            cmd.Parameters.AddWithValue("@Section", section.Text);
                            cmd.Parameters.AddWithValue("@Another_Job", Another_job.Text);
                            cmd.Parameters.AddWithValue("@health_no", health_no.Text);
                            cmd.Parameters.AddWithValue("@Exp_date", Exp_date.Text);
                            //cmd.Parameters.AddWithValue("@National_ID", National_ID.Text);
                            cmd.Parameters.AddWithValue("@reference", DropDownList3.SelectedItem.Text);
                            cmd.Parameters.Add("@serial_no", SqlDbType.Int);
                            cmd.Parameters.AddWithValue("@confirm_rcrd", SqlDbType.Bit);
                            cmd.Parameters.AddWithValue("@arabic_name", arbcname.Text);
                            cmd.Parameters.AddWithValue("@arabic_jobtitle", arbcjob.Text);
                            cmd.Parameters["@serial_no"].Direction = ParameterDirection.Output;
                            if (Exp_date.Text == "")
                            {
                                cmd.Parameters["@Exp_date"].Value = DBNull.Value;

                            }
                            else
                            {
                                cmd.Parameters["@Exp_date"].Value = DateTime.Parse(Exp_date.Text);
                            }
                            if (string.IsNullOrEmpty(Name.Text))
                            {
                                cmd.Parameters.Add(new SqlParameter("@Name", DBNull.Value.ToString()));
                            }
                            else
                            {
                                cmd.Parameters.Add(new SqlParameter("@Name", Name.Text));
                            }
                            if (string.IsNullOrEmpty(National_ID.Text))
                            {
                                cmd.Parameters.Add(new SqlParameter("@National_ID", DBNull.Value.ToString()));
                            }
                            else
                            {
                                cmd.Parameters.Add(new SqlParameter("@National_ID", Name.Text));
                            }
                            


                            cmd.Connection = con;
                            cmd.ExecuteNonQuery();
                           
                            TextBox2.Text = cmd.Parameters["@serial_no"].Value.ToString();


                                                      
                        }


                        catch
                        {
                            Message("ERROR 20", this);
                        }

                    }
                }
                catch
                {
             Message("enter full details ", this);
                }
            }