public List <PersonObject> getAllData()
        {
            List <PersonObject> listPObject = new List <PersonObject>();
            SqlConnection       con         = CreateConnection();
            SqlDataReader       rd;

            using (con) {
                SqlCommand cmd = new SqlCommand("GetPersonData", con);
                cmd.CommandType = CommandType.StoredProcedure;

                rd = cmd.ExecuteReader();
                while (rd.Read())
                {
                    PersonObject pObject = new PersonObject();
                    pObject.Name    = rd["name"].ToString();
                    pObject.Nric    = rd["nric"].ToString();
                    pObject.DOB     = Convert.ToDateTime(rd["dob"]);
                    pObject.PhoneNo = Convert.ToInt32(rd["phoneNo"]);
                    pObject.Address = rd["address"].ToString();
                    pObject.Email   = rd["email"].ToString();

                    listPObject.Add(pObject);
                }


                rd.Close();
            }

            con.Close();

            return(listPObject);
        }
        public void insertData(PersonObject personObject)
        {
            SqlConnection con = CreateConnection();
            SqlCommand    cmd = con.CreateCommand();

            cmd.CommandText = "Execute InsertPerson @name,@nric,@address,@email,@dOB,@phoneNo";
            cmd.Parameters.Add("@name", SqlDbType.VarChar, 50).Value    = personObject.Name;
            cmd.Parameters.Add("@nric", SqlDbType.NChar, 10).Value      = personObject.Nric;
            cmd.Parameters.Add("@address", SqlDbType.VarChar, 50).Value = personObject.Address;
            cmd.Parameters.Add("@email", SqlDbType.VarChar, 50).Value   = personObject.Email;
            cmd.Parameters.Add("@dOB", SqlDbType.Date).Value            = personObject.DOB;
            cmd.Parameters.Add("@phoneNo", SqlDbType.Int).Value         = personObject.PhoneNo;
            cmd.ExecuteNonQuery();
            con.Close();
        }
Beispiel #3
0
//insert btn
        private void btnInsert_Click(object sender, EventArgs e)
        {
            /*
             * String allAddress = "";
             * foreach (Control childAddress in getAllControl(this, typeof(TextBox)))
             * {
             *  if (childAddress is TextBox && childAddress.Name.Contains("tbAddAddress"))
             *  {
             *      Console.WriteLine("+++++++++" + childAddress.Text);
             *      allAddress += ((TextBox)childAddress).Text + ",";
             *  }
             * }
             * MessageBox.Show("values : " + allAddress +",name = " + this.tbName.Text +",nric= "+ this.tbNRIC.Text);
             */


            String address = "";
            String email   = "";

            if (this.tbAltAddress.Text != null)
            {
                address = this.tbAddress.Text + "," + this.tbAltAddress.Text;
            }
            else
            {
                address = this.tbAddress.Text;
            }

            if (this.tbAltEmail.Text != null)
            {
                email = this.tbEmail.Text + "," + this.tbAltEmail.Text;
            }
            else
            {
                email = this.tbEmail.Text;
            }

            if (hasError == false)
            {
                databaseStoreProdMiniProject.dataBaseFunction DbFunction = new databaseStoreProdMiniProject.dataBaseFunction();
                databaseStoreProdMiniProject.PersonObject     pObject    = new databaseStoreProdMiniProject.PersonObject();
                pObject.Name    = this.tbName.Text;
                pObject.Nric    = this.tbNRIC.Text;
                pObject.PhoneNo = Int32.Parse(this.tbPhoneNum.Text);
                pObject.Email   = email;
                pObject.Address = address;
                pObject.DOB     = this.datePickerDOB.Value.Date;


                try
                {
                    DbFunction.insertData(pObject);

                    clearTbLB();
                    MessageBox.Show("Inserted to object");
                }
                catch (Exception ex)
                {
                    logClass.LogMsgToFile(ex.ToString());
                    //logClass.LogMsgToFile("Stack Trace" + ex.StackTrace.ToString());
                    MessageBox.Show("Please check Input value. NRIC must not be inserted before.");
                    logger.Error(ex, "qwerty123");
                }


                Util.objectList.Add(pObject);

                foreach (var i in Util.objectList)
                {
                    Console.WriteLine("value : " + i.ToString());
                }
            }
            else
            {
                MessageBox.Show("Please fill up the form correctly");
            }
        }