neesha herath

neesha herath

  • NA
  • 16
  • 3.1k

C#.Net Message Box

Sep 8 2013 9:53 AM
I am here with attaching the insert record code in my own development application.this code is working. but message box are not working properly. If I inserting data with empty field then it displays "please enter relevant field" and then it jumps to the catch block. I want to correct this error. please help me..thank you very much for your kind consideration.

      private void ButtInsert_Click(object sender, EventArgs e)

        {

 

     if (TxtWorkOrderNo.Text == "")

            {

                MessageBox.Show("Please enter a Work Order Number");

            }

            else if (Txtcat.Text == "")

            {

                MessageBox.Show("Please enter CAT");

            }

            else if (Txtmain.Text == "")

            {

                MessageBox.Show("Please enter MAIN");

            }

            else if (TxtSub.Text == "")

            {

                MessageBox.Show("Please enter SUB");

            }

            else if (RTBSpec.Text == "")

            {

                MessageBox.Show("Please enter SPEC");

            }

            else if (TxtEwoSpec.Text == "")

            {

                MessageBox.Show("Please enter EWO_SPEC");

            }

            else if (TxtExtc.Text == "")

            {

                MessageBox.Show("Please enter EXTC");

            }

            else if (TxtContractorCode.Text == "")

            {

                MessageBox.Show("Please enter Contractor Code");

            }

            else if (TxtCreatedBy.Text == "")

            {

                MessageBox.Show("Please enter Created By ?");

            }

 

            else

            {

                //stores the values entered as variables

                string SED_EWO_NO = TxtWorkOrderNo.Text;

                string SED_CAT = Txtcat.Text;

                string SED_MAIN = Txtmain.Text;

                string SED_SUB = TxtSub.Text;

                string SED_SPEC = RTBSpec.Text;

                string SED_EWO_SPEC = TxtEwoSpec.Text;

                string SED_XTC = TxtExtc.Text;

                string SED_CONTRACTOR_CODE = TxtContractorCode.Text;

                string CREATED_BY = TxtCreatedBy.Text;

            }

           SqlCommand cmd = new SqlCommand();

            string sql; 

            try

            {

                sql = "INSERT INTO EWO_DETAILS (SED_EWO_NO,SED_CONTRACTOR_CODE,SED_EWO_SPEC,SED_CAT,SED_MAIN,SED_SPEC,SED_SUB,SED_XTC,SED_START_DATE,SED_END_DATE,CREATED_BY,CREATED_DATE)";

                sql += String.Format ("VALUES ( @SED_EWO_NO, @SED_CONTRACTOR_CODE, @SED_EWO_SPEC, @SED_CAT, @SED_MAIN, @SED_SPEC, @SED_SUB, @SED_XTC, @SED_START_DATE, @SED_END_DATE, @CREATED_BY, @CREATED_DATE)");

 

                cmd = new SqlCommand(sql, cs);

                cmd.CommandText = sql;

                cmd.CommandType = CommandType.Text;

 

                cmd.Parameters.AddWithValue("@SED_EWO_NO", TxtWorkOrderNo.Text);

                cmd.Parameters.AddWithValue("@SED_CONTRACTOR_CODE", TxtContractorCode.Text);

                cmd.Parameters.AddWithValue("@SED_EWO_SPEC", TxtEwoSpec.Text);

                cmd.Parameters.AddWithValue("@SED_CAT", TxtJcat.Text);

                cmd.Parameters.AddWithValue("@SED_MAIN", TxtJmain.Text);

                cmd.Parameters.AddWithValue("@SED_SPEC", RTBSpec.Text);

                cmd.Parameters.AddWithValue("@SED_SUB", TxtSub.Text);

                cmd.Parameters.AddWithValue("@SED_XTC", TxtExtc.Text);              

                cmd.Parameters.Add("SED_START_DATE", SqlDbType.VarChar).Value = StartDatePicker.Value.Date;

                cmd.Parameters.Add("SED_END_DATE", SqlDbType.VarChar).Value = EndDatePicker.Value.Date;

                cmd.Parameters.AddWithValue("@CREATED_BY", TxtCreatedBy.Text);

                cmd.Parameters.Add("CREATED_DATE", SqlDbType.VarChar).Value = CreatedDatePicker.Value.Date;

 

                cs.Open(); //opens connection

                cmd.ExecuteNonQuery(); //writes to the database

                MessageBox.Show("Details Added", "Successful");

            }

           catch (Exception )          

            {                

                MessageBox.Show("Duplicate Data", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

            }

            finally

            {

                cs.Close(); // Close connection

            }

        }

 


Answers (10)