Friday, January 17, 2014

Scientific Calculator Code



Form1 Coding
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Cartoon
{
    public partial class Form1 : Form
    {
        double a, b, Ang;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            a = Convert.ToDouble(textBox1.Text);
            b = Convert.ToDouble(textBox2.Text);
            Class1 C1 = new Class1();
            textBox3.Text = Convert.ToString(C1.Add(a,b));
        }

        private void button2_Click(object sender, EventArgs e)
        {
            a = Convert.ToDouble(textBox1.Text);
            b = Convert.ToDouble(textBox2.Text);
            Class1 C1 = new Class1();
            textBox3.Text = Convert.ToString(C1.Subtract(a,b));
        }

        private void button3_Click(object sender, EventArgs e)
        {
            a = Convert.ToDouble(textBox1.Text);
            b = Convert.ToDouble(textBox2.Text);
            Class1 C1 = new Class1();
            textBox3.Text = Convert.ToString(C1.Multiply(a,b));
        }

        private void button4_Click(object sender, EventArgs e)
        {
            a = Convert.ToDouble(textBox1.Text);
            b = Convert.ToDouble(textBox2.Text);
            Class1 C1 = new Class1();
            textBox3.Text = Convert.ToString(C1.Divide(a, b));
        }

        private void button5_Click(object sender, EventArgs e)
        {
            Ang = Convert.ToDouble(textBox4.Text);
            Class1 C1 = new Class1();
            textBox5.Text = Convert.ToString(C1.Sin(Ang));
        }

        private void button6_Click(object sender, EventArgs e)
        {
            Ang = Convert.ToDouble(textBox4.Text);
            Class1 C2 = new Class1();
            textBox5.Text = Convert.ToString(C2.Cos(Ang));
        }

        private void button7_Click(object sender, EventArgs e)
        {
            Ang = Convert.ToDouble(textBox4.Text);
            Class1 C3 = new Class1();
            textBox5.Text = Convert.ToString(C3.Tan(Ang));
        }

        private void radioButton2_CheckedChanged(object sender, EventArgs e)
        {
            label2.Visible = true;
            label3.Visible = true;
            label4.Visible = true;
            label5.Visible = false;
            label6.Visible = false;
            textBox1.Visible = true;
            textBox2.Visible = true;
            textBox3.Visible = true;
            button1.Visible = true;
            button2.Visible = true;
            button3.Visible = true;
            button4.Visible = true;
            textBox4.Visible = false;           
            textBox5.Visible = false;
            button5.Visible= false;
            button6.Visible = false;
            button7.Visible = false;
        }

        private void radioButton1_CheckedChanged(object sender, EventArgs e)
        {
            label5.Visible = true;
            label6.Visible = true;
            label2.Visible = false;
            label3.Visible = false;
            label4.Visible = false;
            textBox4.Visible = true;
            textBox5.Visible = true;
            button5.Visible = true;
            button6.Visible = true;
            button7.Visible = true;
            textBox1.Visible = false;
            textBox2.Visible = false;
            textBox3.Visible = false;
            button1.Visible = false;
            button2.Visible = false;
            button3.Visible = false;
            button4.Visible = false;
        }
    }
}

Method class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Cartoon
{
    class Class1
    {
        double Answer,Ang_Value;
        public double Add(double x, double y)
        {
            Answer = x + y;
            return Answer;      
        }
        public double Subtract(double x, double y)
        {
            Answer = x - y;
            return Answer;
        }
        public double Multiply(double x, double y)
        {
            Answer = x * y;
            return Answer;
        }
        public double Divide(double x, double y)
        {
            Answer = x / y;
            return Answer;
        }
        public double Sin(double a)
        {
            if (a == 0)
            {
                Ang_Value = 0;
            }
            else
                if (a == 30)
                {
                    Ang_Value = 0.5;
                }
                else
                    if (a == 45)
                    {
                        Ang_Value = 0.7071;
                    }
                    else
                        if (a == 60)
                        {
                            Ang_Value = 0.8660;
                        }
                        else
                            if (a == 90)
                            {
                                Ang_Value = 1;
                            }
            return Ang_Value;

        }

        public double Cos(double a)
        {
            if (a == 0)
            {
                Ang_Value = 1;
            }
            else
                if (a == 30)
                {
                    Ang_Value = 0.8660;
                }
                else
                    if (a == 45)
                    {
                        Ang_Value = 0.7071;
                    }
                    else
                        if (a == 60)
                        {
                            Ang_Value = 0.5;
                        }
                        else
                            if (a == 90)
                            {
                                Ang_Value = 0;
                            }
            return Ang_Value;       
        }
        public double Tan(double a)
        {
            if (a == 0)
            {
                Ang_Value = 0;
            }
            else
                if (a == 30)
                {
                    Ang_Value = 0.5773;
                }
                else
                    if (a == 45)
                    {
                        Ang_Value = 1;
                    }
                    else
                        if (a == 60)
                        {
                            Ang_Value = 1.7320;
                        }
            return Ang_Value;
        }
    }
}




 

Thursday, January 16, 2014

Exception Handling in C# Form App



PROBLEM STATEMENT
Handle an exception found in a division method.

FORM 1:CODING:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Lab11
{
    public partial class Form1 : Form
    {
        int num1, num2, ans;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                num1 = Convert.ToInt32(textBox1.Text);
                num2 = Convert.ToInt32(textBox2.Text);
                ans = num1 / num2;
                textBox3.Text = Convert.ToString(ans);
            }
            catch (DivideByZeroException de)
            {
                MessageBox.Show("Enter any integer other than zero");
            }
            catch (FormatException fe)
            {
                MessageBox.Show(fe.Message);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                label4.Text = "this is from finally and it always run";
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

     
    }
}


OUTPUT:



Wednesday, January 15, 2014

Exception Handling




PROBLEM STATEMENT
Make an exception in a program & handle it.

CODING:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace exceptionhandling
{
    class arrayexception : Exception
    {
        public arrayexception() :
            base("please enter the command line arguement")
        {
        }
    }
    class throwarrayexception
    {
        public throwarrayexception(int a)
        {
            try { checksize(a); }
            catch (arrayexception ae)
            {
                Console.WriteLine(ae.Message);
            }
        }
        public void checksize(int a)
        {
            if (a == 0)
                throw new arrayexception();
            else
            {
                Console.WriteLine("the size of arguements passed to main method is {0}", a);
            }
        }
        class program
        {
            static void Main(string[] args)
            {
                new throwarrayexception(args.Length);
            }
        }
    }
}




OUTPUT:


Tuesday, January 7, 2014

SQL Query Class

SQL Query Class just add it to yourz code and call its methods through class object refrance and pass parameters of yourz SQL Database path and SQL query to implement on database. This Whole code is in C# .................................................................



using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.IO;
using System.Windows.Forms;

namespace IMS
{

    class SQLClass
    {

        public void Conect(string AddQuery, string Path)
        {
            SqlConnection con = new SqlConnection(Path);
            con.Open();
            SqlCommand com = new SqlCommand(AddQuery, con);
            var RowEfected = com.ExecuteNonQuery();
            con.Close();
            if (RowEfected > 0)
            {
                MessageBox.Show("Data Saved Successfully");
            }
            else
            {
                MessageBox.Show("Data Not Saved");
            }
        }

        public int DataReadrint(string AddQuery, string Path)
        {
            int Num=0;
            SqlConnection con = new SqlConnection(Path);
            con.Open();
            SqlCommand com = new SqlCommand(AddQuery, con);
            SqlDataReader sdr = com.ExecuteReader();
            while (sdr.Read())
            {
               Num = sdr.GetInt32(0);
            }
            con.Close();
            return Num;
        }

        public string DataReadrstring(string AddQuery, string Path)
        {
            string Word="a";
            SqlConnection con = new SqlConnection(Path);
            con.Open();
            SqlCommand com = new SqlCommand(AddQuery, con);
            SqlDataReader sdr = com.ExecuteReader();
            while (sdr.Read())
            {
                Word = sdr.GetString(0).ToString();
            }
            con.Close();
            return Word;
        }

    }
}

We Use this class on our C# app like that :::::::

SQLClass sq = new SQLClass();
string AQ = string.Format("Select I_Id From Items Where (I_Name='" + cmbx_R_IName.Text + "')");
            int IID = sq.DataReadrint(AQ, path);


Here first method is to simply do insertion in database and the second method is to get an integer value from data base and return it as an integer value . third one is the same method like second but just the change is, its for an string type data to get from data base and use is on yourz form app or on any console app....................