Saturday, 5 October 2013

c# Code for retrive data from database

c# Code for retrive data from database


private void txtEID_Leave(object sender, EventArgs e)
        {
            DialogResult resp;
          
            string mailID, str = null, domain = null;
            int j = 0;
            if (txtEID.Text == "")
            {
 MessageBox.Show("Some of the required fields are left blank please fill them first!\nCODE ", "CHEMINOVA");
              txtEID.Focus();
            }
            else
            {
                SqlCommand mycmd = new SqlCommand();
 string cmd = "select * from EMP_PERS WHERE EMP_CODE='" + txtEID.Text+ "'
                mycmd.CommandText = cmd;
                DataSet ds1 = new DataSet();
                mycon = new SqlConnection(dh.Properties.Settings.Default.CheminovaConnectionString.ToString());
                mycmd.Connection = mycon;
                try
                {
                    myad.SelectCommand = mycmd;
                    mycon.Open();
                    myad.Fill(ds1, "EMP_PERS");
                    mycon.Close();

                    if (ds1.Tables["EMP_PERS"].Rows.Count <= 0)
                    {

                        getempdetails();
                      
                    }
                    else
                    {
                        for (int i = 0; i < ds1.Tables["EMP_PERS"].Rows.Count; i++)
                        {

                            if (txtEID.Text == ds1.Tables["EMP_PERS"].Rows[i]["EMP_CODE"].ToString
 {
                                this.txtEID.Text = ds1.Tables["EMP_PERS"].Rows[i]["EMP_CODE"].ToString();
                                //
                                this.txt_Fname.Text = ds1.Tables["EMP_PERS"].Rows[i]["EMP_FNAME"].ToString();
                                this.txt_Mname.Text = ds1.Tables["EMP_PERS"].Rows[i]["EMP_MNAME"].ToString();
                                this.txt_Lname.Text = ds1.Tables["EMP_PERS"].Rows[i]["EMP_LNAME"].ToString();
                                this.txtDOB.Text = ds1.Tables["EMP_PERS"].Rows[i]["D_O_B"].ToString();
                                this.txtP_Add.Text = ds1.Tables["EMP_PERS"].Rows[i]["PRE_ADDR"].ToString();
                                this.txtP_City.Text = ds1.Tables["EMP_PERS"].Rows[i]["PRE_CITY"].ToString();
                                this.txtP_State.Text = ds1.Tables["EMP_PERS"].Rows[i]["PRE_STATE"].ToString();
                                this.txtP_PerAdd.Text = ds1.Tables["EMP_PERS"].Rows[i]["PER_ADDR"].ToString();
                                this.txtP_PerCity.Text = ds1.Tables["EMP_PERS"].Rows[i]["PER_CITY"].ToString();
                                this.txtP_PerState.Text = ds1.Tables["EMP_PERS"].Rows[i]["PER_STATE"].ToString();
                                this.lblP_Age.Text = ds1.Tables["EMP_PERS"].Rows[i]["AGE"].ToString();

                                this.cmbMst.Text = ds1.Tables["EMP_PERS"].Rows[i]["MARITAL_STATUS"].ToString();
                                this.txtP_Phr.Text = ds1.Tables["EMP_PERS"].Rows[i]["CONTACT_R"].ToString();
                                this.txtP_Phm.Text = ds1.Tables["EMP_PERS"].Rows[i]["CONTACT_M"].ToString();
                                this.cmbGender.Text = ds1.Tables["EMP_PERS"].Rows[i]["GENDER"].ToString();
                                this.txtP_PExpr.Text = ds1.Tables["EMP_PERS"].Rows[i]["PRE_EXPR"].ToString();
                                this.cmbP_Bg.Text = ds1.Tables["EMP_PERS"].Rows[i]["BLOOD_GRP"].ToString();
                                mailID = ds1.Tables["EMP_PERS"].Rows[i]["EMAIL_ID"].ToString();
                                while (mailID[j] != '@')
                                {
                                    str += mailID[j];
                                    j++;
                                }
                                j++;
                                while (j < mailID.Length)
                                {
                                    domain += mailID[j];
                                    j++;
                                }
                                txtP_Mailid.Text = str;
                                comboBox1.Text = domain;
                            }
                        }
                        resp=MessageBox.Show("The Data already exists.......... Do you want to DELETE or UPDATE the data?", "DATA EXISTS....", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                        if (resp == DialogResult.No)
                        {
                            clearFields();
                        }
                    }
                }
                catch (SqlException sx)
                {
                    MessageBox.Show(sx.Message, "SQL EXCEPTION !");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "OTHER EXCEPTION !");
                }
            }
           }

        private void getempdetails()
        {
            string sel = "select * from RECRUIT_PERSONAL";
            //mycon.ConnectionString = source;
            mycon = new SqlConnection(dh.Properties.Settings.Default.CheminovaConnectionString.ToString());
            SqlCommand select = new SqlCommand(sel, mycon);
            myad.SelectCommand = select;
            DataSet ds1 = new DataSet();
          
try
            {
                mycon.Open();
                myad.Fill(ds1, "RECRUIT_PERSONAL");
                mycon.Close();
            }
            catch (SqlException sx)
            {
                MessageBox.Show(sx.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

            int cnt = ds1.Tables["RECRUIT_PERSONAL"].Rows.Count;
            if (cnt > 0 && this.txtEID.Text != "")
            {

                for (int i = 0; i < cnt; i++)
                {
                    if (this.txtEID.Text == ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["EMP_CODE"].ToString())
                    {
                        this.txtEID.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["EMP_CODE"].ToString();
                        //
                        this.txt_Fname.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["EMP_FNAME"].ToString();
                        this.txt_Mname.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["EMP_MNAME"].ToString();
                        this.txt_Lname.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["EMP_LNAME"].ToString();
                        this.txtDOB.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["D_O_B"].ToString();
                        this.txtP_Add.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["ADD_PRES"].ToString();
                        this.txtP_City.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["CITY_PRES"].ToString();
                        this.txtP_State.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["STATE_PRES"].ToString();
                        this.txtP_PerAdd.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["ADD_PERM"].ToString();
                        this.txtP_PerCity.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["CITY_PERM"].ToString();
                        this.txtP_PerState.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["STATE_PERM"].ToString();
                        //this.txtP_Qual.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["EQ_QUALIF"].ToString();
                        //this.txtP_DO.Text = ds.Tables["EMP_PERS"].Rows[i]["D_0_B"].ToString();
                        this.lblP_Age.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["AGE"].ToString();

                        this.cmbMst.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["MARITAL_STATUS"].ToString();
                        this.txtP_Phr.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["PH_PRES"].ToString();
                        this.txtP_Phm.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["PH_PERM"].ToString();
                        this.cmbGender.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["GENDER"].ToString();
                        this.txtP_PExpr.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["EX_PERIOD"].ToString();
                        this.cmbP_Bg.Text = ds1.Tables["RECRUIT_PERSONAL"].Rows[i]["BLOOD_GRP"].ToString();

                        MessageBox.Show("THE CODE ALREADY EXISTS........YOU CAN EITHER DELETE OR UPDATE");
                    }
                }
            }
            mycon.Close();
        }

No comments:

Post a Comment