Showing posts with label c# sharp. Show all posts
Showing posts with label c# sharp. Show all posts

Saturday, 23 July 2011

checking data on sql through win form weather entry is added or failed or no double entry


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;
using System.Data.SqlClient;

namespace WindowsFormsApplication8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        int flag = 0;
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection conn = new SqlConnection("server=WEBNET;database=scott;user=sa;password=aptech");
                conn.Open();
                string select = "select * from reg";
                SqlCommand comm = new SqlCommand(select, conn);
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    if (dr["rollno"].ToString() == textBox1.Text)
                    {
                        MessageBox.Show("Record already exists");
                        flag = 1;
                    }
                }
                if (textBox1.Text.Equals("") || textBox2.Text.Equals("") || textBox3.Text.Equals("") || textBox4.Text.Equals(""))
                {
                    MessageBox.Show("values cannot be empty");
                }
                else if(flag==0)
                {
                    string insert;
                    insert = "insert into reg values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')";
                    SqlCommand comm1 = new SqlCommand(insert, conn);
                    flag = 0;
                    dr.Close();
                    comm1.ExecuteNonQuery();
               
               
               
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Error Occured");
            }
        }
       
    }
}

C# Sharp WinForm , SqlServer Adding Data Through Textbox, Radio button,Massage box


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;
using System.Data.SqlClient;

namespace WindowsFormsApplication6
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string gender;
           
            if (radioButton1.Checked)
            {
                gender = radioButton1.Text.ToString();
            }
            else
            {
                gender = radioButton2.Text.ToString();
            }
            SqlConnection cn = new SqlConnection("server=LAB2-86;database=studentsdb;user=sa;password=aptech");
       
            string insert;
            insert = "insert into db values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + gender + "')";
            SqlCommand sc = new SqlCommand(insert, cn);
            cn.Open();
            if (MessageBox.Show("Do want to insert record? ", "Winform", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning) == DialogResult.OK)
            {
                sc.ExecuteNonQuery();
            }
            string select = "select * from db";
            SqlDataAdapter da = new SqlDataAdapter(select, cn);
            DataTable dt = new DataTable();
            da.Fill(dt);
            dataGridView1.DataSource = dt;
            cn.Close();
        }
    }
}

data grid view export data to exel


private void Excel_Click(object sender, EventArgs e)
>         {
>             if (dataGridView1.Rows.Count != 0)
>             {
>                 SaveFileDialog sfd = new SaveFileDialog();
>                 sfd.Filter = "Microsoft Excel 97/2000/XP(*.xls)|
> *.xls";
>                 sfd.FileName = "Report.xls";
>                 if (sfd.ShowDialog() == DialogResult.OK)
>                 {
>                     StreamWriter wr = new StreamWriter(sfd.FileName);
>
>                     int cols = dataGridView1.Columns.Count;
>                     for (int i = 0; i < cols; i++)
>                     {
>
> wr.Write(dataGridView1.Columns[i].Name.ToUpper() + "\t");
>                     }
>
>                     wr.WriteLine();
>
>                     for (int i = 0; i < (dataGridView1.Rows.Count -
> 1); i++)
>                     {
>                         for (int j = 0; j < cols; j++)
>                         {
>
> wr.Write(dataGridView1.Rows[i].Cells[j].Value + "\t");
>                         }
>                         wr.WriteLine();
>                     }
>                     wr.Close();
>                     label9.Text = "Your file has been successfully
> saved !";
>                 }
>             }
>             else
>             {
>                 MessageBox.Show("Error");
>             }
>         }

Another C# Sharp WinForm With Massage Box Creating Connection To SqlServer adding,delete,update Data Through Textbox


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;
using System.Data.SqlClient;
namespace WindowsFormsApplication4
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("server=WEBNET;database=myfirstdb;user=sa;password=aptech");
            string insert;
            insert = "insert into employee values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "' )";
            SqlCommand cm = new SqlCommand(insert,cn);
            cn.Open();
            cm.ExecuteNonQuery();
            MessageBox.Show("Record added succesfully");
            cn.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("server=WEBNET;database=myfirstdb;user=sa;password=aptech");
            string delete;
            delete = "delete from employee where empno='" + textBox1.Text + "' ";
            SqlCommand cm = new SqlCommand(delete, cn);
            cn.Open();
            cm.ExecuteNonQuery();
            cn.Close();


 private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection cn = new SqlConnection("server=LAB2-80;database=myfirstdb;user=sa;password=aptech");
            string update;
            update = "update emp set empno='"+textBox1.Text+"',name='"+textBox2.Text+"',location='"+textBox3.Text+"' where empno='"+textBox1.Text+"' ";
            SqlCommand cm = new SqlCommand(update, cn);
            cn.Open();
            cm.ExecuteNonQuery();
            MessageBox.Show("Record updated succesfully");
            cn.Close();
        }



        }
    }
}

C# Sharp WinForm Ceating Connection To SqlServer adding,delete,update Data Through Textbox


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;
using System.Data.SqlClient;

namespace mydatabase
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlConnection sc = new SqlConnection("Server=LAB2-86;Database=ApStudents;user=sa;password=aptech");
            string insert = "insert into Students values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
            SqlCommand cm = new SqlCommand(insert, sc);
            sc.Open();
            //cm.ExecuteNonQuery();
            try
            {
                cm.ExecuteNonQuery();
                MessageBox.Show("Data Insert Successfully");
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            sc.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection sc = new SqlConnection("Server=LAB2-86;Database=ApStudents;user=sa;password=aptech");
            string delete = "delete from Students where std_ID=('" + textBox1.Text + "')";
            SqlCommand cm = new SqlCommand(delete, sc);
            sc.Open();
            cm.ExecuteNonQuery();
            MessageBox.Show("Data Delete Successfully");
            sc.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection sc = new SqlConnection("Server=LAB2-86;Database=ApStudents;user=sa;password=aptech");
            string update = "update Students set std_ID='" + textBox1.Text + "',S_Name='" + textBox2.Text + "',S_Address='" + textBox3.Text + "' where std_ID='" + textBox1.Text + "' ";
            SqlCommand cm = new SqlCommand(update, sc);
            sc.Open();
            cm.ExecuteNonQuery();
            MessageBox.Show("Data Update Successfully");
            sc.Close();
        }
    }
}

some logic building question code

Question1

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

namespace ConsoleApplication3
{
    class Program
    {
        static void Main(string[] args)
        {
            int age;
            int a, b, c;
            Console.WriteLine("Enter your Current Age");
            age= Convert.ToInt32(Console.ReadLine());
            if (age < 18)
            {
                Console.WriteLine("You are a Child");
            }
            else if (18 <=age)
            {
                Console.WriteLine("You are Adult");
            }
            else
            {
                 Console.WriteLine("You are a Child");
            }
            Console.ReadKey();
        }
    }
}



Question2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
          
            for (a = 1; a <=100 ;a++ )
            {
                if (a % 2 !=0 && a % 3 != 0 && a % 5 != 0)
                {
                 
                    Console.WriteLine("{0}",a);
                }

            }
            Console.ReadKey();
        }
    }
}



Question3
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            for (a = 0; a <= 255; a++)
            {
                Console.WriteLine("{0} --->{1}", a,Convert.ToChar(a));


            }
            Console.ReadKey();
        } 
    }
}



Question4
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            int b;
           int c;
            Console.WriteLine("what your want to do *,+,-,/  select one");
            c = Convert.ToChar(Console.ReadLine());
            Console.WriteLine("Enter First Number");
            a = Convert.ToInt32(Console.ReadLine());
    

            Console.WriteLine("Enter Second Number");
            b = Convert.ToInt32(Console.ReadLine());
            switch (c)
            {
                case '*':
                    c = a * b;
                    Console.WriteLine("The Mul of a and b {0}", c);
                    break;
                case '+':
                    c = a + b;
                    Console.WriteLine("The sum of a & b {0}", c);
                    break;
                case '-':
                    c = a - b;
                    Console.WriteLine("The difference of a & b {0}", c);
                    break;
                case '/':
                    c = a / b;
                    Console.WriteLine("The quotient of a & b {0}", c);
                    break;
            }
            Console.ReadKey();

        }
    }
}


Question5
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int a;
            int b;
            
            Console.WriteLine("Enter Number for Table");
            a = Convert.ToInt32(Console.ReadLine());
            for (b = 1; b <= 10; b++)
            {
               
                Console.WriteLine("{0}X {1}={2}",a, b,a*b);
            }
            Console.ReadKey();
        }
    }
}


radio button with sqldata


 private void button1_Click(object sender, EventArgs e)
        {
            string gender;
            if (radioButton1.Checked)
            {
                gender = radioButton1.Text.ToString();
            }
            else
            {
                gender = radioButton2.Text.ToString();
            }
            SqlConnection cn = new SqlConnection("server=WEBNET;database=scott;user=sa;password=aptech");
            string insert;
            insert = "insert into student values('" + textBox1.Text + "','" + textBox2.Text + "','" + gender + "')";
            SqlCommand comm = new SqlCommand(insert, cn);
            cn.Open();
            if (MessageBox.Show("Do want to insert record? ","Winform",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning)==DialogResult.OK)
            {
                comm.ExecuteNonQuery();
            }
                cn.Close();
        }

salary calculator


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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            int mon,tues,wed,thurs,fri;
            int twhour, salrary;
            int ovtime;

            Console.WriteLine("working hour of monday");
            mon = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("working hour of tuesday");
            tues = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("working hour of wedday");
            wed = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("working hour of thursday");
            thurs = Convert.ToInt32(Console.ReadLine());
            Console.WriteLine("working hour of friday");
            fri = Convert.ToInt32(Console.ReadLine());

            twhour = mon + tues + wed + thurs + fri;
            Console.WriteLine("the total working hour is {0}",twhour);

            salrary = twhour * 500;
            Console.WriteLine("the total salary is {0} $", salrary);

            if(twhour>40)
            {
                ovtime = twhour - 40;
                Console.WriteLine("over time hour are{0} ",ovtime);
            }
            Console.ReadKey();
        }
    }
}