Saturday, 5 October 2013

Code for validation c#

Code for validation c#


private void txtP_Fname_KeyPress(object sender, KeyPressEventArgs e)
        {
            if ((e.KeyChar >= 'a' && e.KeyChar <= 'z') || (e.KeyChar >= 'A' && e.KeyChar <= 'Z') || (e.KeyChar == '\b'))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }

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();
        }

c# Simple SQL queries (insert

c# Simple SQL queries (insert


private void btnSave_Click(object sender, EventArgs e)
        {
 myad.InsertCommand = new SqlCommand("INSERT INTO EMP_PERS" +
                "(EMP_CODE,EMP_FNAME,EMP_MNAME,EMP_LNAME,PRE_ADDR,PRE_CITY,PRE_STATE,PER_ADDR,PER_CITY,PER_STATE,QUALIFICATION,D_O_B,AGE,MARITAL_STATUS,CONTACT_R,CONTACT_M,GENDER,PRE_EXPR,BLOOD_GRP,EMAIL_ID) VALUES" +
                "(@EMP_CODE,@EMP_FNAME,@EMP_MNAME,@EMP_LNAME,@PRE_ADDR,@PRE_CITY,@PRE_STATE,@PER_ADDR,@PER_CITY,@PER_STATE,@QUALIFICATION,@D_O_B,@AGE,@MARITAL_STATUS,@CONTACT_R,@CONTACT_M,@GENDER,@PRE_EXPR,@BLOOD_GRP,@EMAIL_ID)", mycon);

            SqlParameter myparm = null;

 myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@EMP_CODE", SqlDbType.NVarChar));
            myparm.SourceColumn = "EMP_CODE";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@EMP_FNAME", SqlDbType.NVarChar));
            myparm.SourceColumn = "EMP_FNAME";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@EMP_MNAME", SqlDbType.NVarChar));
            myparm.SourceColumn = "EMP_MNAME";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@EMP_LNAME", SqlDbType.NVarChar));
            myparm.SourceColumn = "EMP_LNAME";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@PRE_ADDR", SqlDbType.NVarChar));
            myparm.SourceColumn = "PRE_ADDR";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@PRE_CITY", SqlDbType.NVarChar));
            myparm.SourceColumn = "PRE_CITY";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@PRE_STATE", SqlDbType.NVarChar));
            myparm.SourceColumn = "PRE_STATE";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@PER_ADDR", SqlDbType.NVarChar));
            myparm.SourceColumn = "PER_ADDR";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@PER_CITY", SqlDbType.NVarChar));
            myparm.SourceColumn = "PER_CITY";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@PER_STATE", SqlDbType.NVarChar));
            myparm.SourceColumn = "PER_STATE";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@QUALIFICATION", SqlDbType.NVarChar));
            myparm.SourceColumn = "QUALIFICATION";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@D_O_B", SqlDbType.DateTime));
            myparm.SourceColumn = "D_O_B";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@AGE", SqlDbType.Int));
            myparm.SourceColumn = "AGE";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@MARITAL_STATUS", SqlDbType.NVarChar));
            myparm.SourceColumn = "MARITAL_STATUS";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@CONTACT_R", SqlDbType.Int));
            myparm.SourceColumn = "CONTACT_R";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@CONTACT_M", SqlDbType.Int));
            myparm.SourceColumn = "CONTACT_M";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@GENDER", SqlDbType.NVarChar));
            myparm.SourceColumn = "GENDER";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@PRE_EXPR", SqlDbType.Int));
            myparm.SourceColumn = "PRE_EXPR";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@BLOOD_GRP", SqlDbType.NVarChar));
            myparm.SourceColumn = "BLOOD_GRP";
            myparm.SourceVersion = DataRowVersion.Current;

            myparm = myad.InsertCommand.Parameters.Add(new SqlParameter("@EMAIL_ID", SqlDbType.NVarChar));
            myparm.SourceColumn = "EMAIL_ID";
            myparm.SourceVersion = DataRowVersion.Current;

            DataSet myDS = new DataSet();
            myad.SelectCommand = new SqlCommand("select * from EMP_PERS",mycon);
            myad.Fill(myDS, "EMP_PERS");

            DataRow newRow = myDS.Tables["EMP_PERS"].NewRow();

            newRow[0] = this.txtEID.Text;
            newRow[1] = this.txt_Fname.Text;
            newRow[2] = this.txt_Mname.Text;
            newRow[3] = this.txt_Lname.Text;
            newRow[4] = this.txtP_Add.Text;
            newRow[5] = this.txtP_City.Text;
            newRow[6] = this.txtP_State.Text;
            newRow[7] = this.txtP_PerAdd.Text;
            newRow[8] = this.txtP_PerCity.Text;
            newRow[9] = this.txtP_PerState.Text;
            newRow[10] = "";
            try
            {
                        newRow[11] = this.txtDOB.Text;
            }
            catch (SqlException sx2)
            {
                MessageBox.Show(sx2.Message, "SQL EXCEPTION OF DATE !");
            }
            catch (Exception ex1)
            {
                MessageBox.Show(ex1.Message, "OTHER EXCEPTION OF DATE !");
            }
            newRow[12] = int.Parse(this.lblP_Age.Text);//age;//this.txtp_age;
            newRow[13] = this.cmbMst.Text;
            newRow[14] = int.Parse(this.txtP_Phr.Text);
            newRow[15] = int.Parse(this.txtP_Phm.Text);
            newRow[16] = this.cmbGender.Text;
            newRow[17] = int.Parse(this.txtP_PExpr.Text);
            newRow[18] = this.cmbP_Bg.Text;
            newRow[19] = this.txtP_Mailid.Text + "@" + comboBox1.Text;
            try
            {
                myDS.Tables["EMP_PERS"].Rows.Add(newRow);
            }
            catch (SqlAlreadyFilledException sx)
            {
                MessageBox.Show(sx.Message, "SQL ERROR ALREADY FILLED!");
            }
            catch (SqlException sx1)
            {
                MessageBox.Show(sx1.Message, "SQL EXCEPTION ON INSERT !");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "INSERT ERROR ON INSERT !!");
            }
            finally
            {
                myad.Update(myDS, "EMP_PERS");
                MessageBox.Show("NEW DATA IS INSERTED !!", "INSERT COMMAND");
            }
            clearFields();
        }

        private void clearFields()
        {
            this.txtEID.Text = "";
        }

c# Code for delete data from database

c# Code for delete data from database


Print the report using the custom print controller in vb.net

Print the report using the custom print controller in vb.net


Public Function GetPapersizeID(ByValPrinterNameAsString,ByValPaperSizeNameAsString)AsInteger
Dim doctoprint As New System.Drawing.Printing.PrintDocument()
Dim PaperSizeID As Integer=0
Dim ppname As String=""
Dim s As String=""
doctoprint.PrinterSettings.PrinterName=PrinterName'(ex."EpsonSQ-1170ESC/P2")
For i As Integer=0 To doctoprint.PrinterSettings.PaperSizes.Count-1
Dim raw Kind As Integer
ppname=PaperSizeName
If doctoprint.PrinterSettings.PaperSizes(i).PaperName=ppnameThen
rawKind=CInt(doctoprint.PrinterSettings.PaperSizes(i).GetType().GetField("kind",
Reflection.BindingFlags.InstanceOrReflection.BindingFlags.NonPublic).GetValue(doctoprint.PrinterSettings.PaperSizes(i)))
PaperSizeID=rawKind
ExitFor
EndIf
Next
Return PaperSizeID
End Function

Dim repOptions As CrystalDecisions.CrystalReports.Engine.PrintOptions
With ReportDocument
repOptions=.PrintOptions
.Load(ReportPath,CrystalDecisions.Shared.OpenReportMethod.OpenReportByDefault)
.SetDataSource(ds.Tables(0))
With rep Options
.PaperOrientation= CrystalDecision.Shared.PaperOrientation.Portrait
.PaperSize= GetPapersizeID("EPSON LX-300+II", "CEPCRP")
.PrinterName= "EPSON LX-300+II"
End With
.Refresh()
.PrintToPrinter(NumberOFCopies,Collated,StartPage,EndPage)
End With

key generation code in vb.net

key generation code in vb.net


Public Class key_gen
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dd As Long = (Today.Date.Day)
Dim mm As Long = (Today.Date.Month)
Dim yy As Long = (Today.Date.Year)
Dim key1 As String
key1 = CStr(((yy - dd) * 4 - mm * dd * 4 + 50 + dd * 23) Mod 99999) + "-" + CStr(((dd Mod 6) * 8 * (yy - dd - mm - 1000)) Mod 99999) + "-" + CStr((yy * 7 - 1234 - dd * 12 + (mm * 20 * yy)) Mod 99999) + "-" + CStr(((mm * dd * 541) - yy - 1234 + (((yy - dd) * 3) - mm * 13)) Mod 99999)
TextBox1.Text = key1
End Sub
End Class

How To Remove and Add Right-Click Menu Items from Files and Folders

How To Remove and Add Right-Click Menu Items from Files and Folders


A lot of programs you install will add themselves to the right-click menu of your files and/or folders. And most times, you have no choice in the matter and, as a result, your right-click menu can get very long with added items you don't even use. The last person I was helping with this had a right context menu so long that the Rename option was no longer visible!
Fortunately, you can easily remove those unwanted menu items, if you know the registry values to edit. And it's not at all difficult once you know the keys responsible for the additions.

For Files, the secret lies in the "context menu handlers" under the shellex subkey for "All Files" which, in the registry, is nothing but an asterisk - like a dos wildcard, which means the values entered apply to all files. It is at the very top of the Root key, right here:

HKEY_CLASSES_ROOT\*\shellex\ContextMenuHandlers

Click the the + sign next to the ContextMenuHandlers key, to expand it.
Now you will see some of the programs that have added items to your right-click menu. Simply delete the program keys you don't want.
Yup! It's that simple. If deleting makes you uneasy, just export the key before deleting it. Or, instead of deleting the values, disable them. Simply double click the default value for the program on the right hand pane and rename the clsid value by placing a period or dash in front of it.
ie; - {b5eedee0-c06e-11cf-8c56-444553540000}
Then exit the registry, refresh, and right click a file to see if the item was removed from the menu.
Some programs - like WinZip or WinRar - will add several items to your right click menu but all of them will be removed by deleting or disabling their one context menu handler.

Note that the above key only applies to the right click menu of files.
To remove entries from the right click context menu of folders, you need to navigate to the Folder and Drive keys:

HKEY_CLASSES_ROOT\Folder\shellex\ContextMenuHandlers
HKEY_CLASSES_ROOT\Drive\shellex\ContextMenuHandlers


All you have to do is follow the same procedure as for Files - either disable or delete items you wish to remove.
Adding Items
Adding Items to the right click menu of Files and Folders is also fairly simple using the Registry. It just involves the creation of a few new keys for each item you wish to add. You edit the same keys used for removing items. Let's use Notepad as an example of an item you'd like to add to the right click menu of all your files or folders.

For folders, go to this key:
HKEY_CLASSES_ROOT\Folder
Click the + sign next to Folder and expand it so that the Shell key is visible. Right click the Shell key and choose New>Key and name the key Notepad or whatever else you'd prefer (whatever the key is named is what will appear in the right-click menu). Now right click the new key you made and create another key named Command. Then, in the right hand pane, double click "Default" and enter Notepad.exe as the value.
Exit the registry, refresh, and right click any folder. Notepad should now be on the context menu.


For files, go here again:

HKEY_CLASSES_ROOT\*
Expand the * key and see if a Shell key exists. If it does exist, follow the same procedure as for folders. If it does not exist, you'll have to create a new Shell first. Just right click the * key and choose New>Key and name it Shell. Then right click the Shell key and continue on the same way you did for adding items to the right click menu of folders.
Once done, Notepad should appear as an option in the right click menu of all your files