Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

May 11, 2012

C# AND LINQ TO SQL CONNECTION









LINQ TO SQL SAMPLE-C#


 private void Form1_Load(object sender, EventArgs e)
        {

            refResh();

        }
        //sql connection
        private void refResh()
        {
            DataClasses1DataContext dc = new DataClasses1DataContext();

            var src = from c in dc.AÜs
                      select new {c.Id ,c.Soyad,  c.Ad , c.Job};
            dataGridView1.DataSource = src;
       
        }
        //Update
        private void button1_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext dc = new DataClasses1DataContext();

            var src = (from c in dc.AÜs
                      where c.Id==int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString())
                      select c).SingleOrDefault();
          
           
            if ((textBox3.Text != "")&&(textBox2.Text!=""))
            {
                src.Soyad =textBox2.Text;
                src.Ad = textBox3.Text;
               
            }
            if ((textBox2.Text!= "") && (textBox3.Text == ""))
            {
                src.Ad = dataGridView1.CurrentRow.Cells[2].Value.ToString();
                src.Soyad =textBox2.Text;
            }
           if ((textBox3.Text!= "") && (textBox2.Text == ""))
            {
                src.Soyad = dataGridView1.CurrentRow.Cells[1].Value.ToString();
                src.Ad=textBox3.Text;
            }
           src.Job =comboBox1.Text;
          
            dc.SubmitChanges();
            refResh();
            MessageBox.Show("Updated");

        }
        //delete
        private void button2_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext dc = new DataClasses1DataContext();

            var src = (from c in dc.AÜs
                       where c.Id==int.Parse(dataGridView1.CurrentRow.Cells[0].Value.ToString())

                       select c).SingleOrDefault();

            if (src != null)
            {
                dc.AÜs.DeleteOnSubmit(src);


            }

            dc.SubmitChanges();
            refResh();
            MessageBox.Show("Deleted");
            textBox1.Text = "";
            textBox6.Text = "";


        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
        //insert
        private void button3_Click(object sender, EventArgs e)
        {
            DataClasses1DataContext dc = new DataClasses1DataContext();

            AÜ cust = new AÜ();
            cust.Ad = textBox5.Text;
            cust.Soyad = textBox4.Text;
            cust.Job = comboBox2.Text;
            dc.AÜs.InsertOnSubmit(cust);
            dc.SubmitChanges();
            refResh();
            MessageBox.Show("Inserted");
            textBox4.Text = "";
            textBox5.Text = "";
            comboBox2.Text = "";
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            textBox3.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            textBox1.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            textBox6.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            comboBox1.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
        }

April 27, 2012

C# CheckBox


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 WindowsFormsApplication11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
         
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int d = 0;
            int y = 0;
            if ((checkBox1.Checked) && (checkBox3.Checked) && (checkBox2.Checked == false) &&                     (checkBox4.Checked == false))
            {
                d = d + 1;
            }

            else
            {
                y = y + 1;
            }

            if ((checkBox5.Checked == false) && (checkBox6.Checked == false) &&
                                                                    (checkBox7.Checked == false) && (checkBox8.Checked))
            {
                d = d + 1;
            }

            else
            {
                y = y + 1;
            }

            checkBox1.BackColor = Color.PaleGreen;
            checkBox3.BackColor = Color.PaleGreen;
            checkBox8.BackColor = Color.PaleGreen;

            label5.Text = d.ToString();
            label6.Text = y.ToString() ;


        }

    }
}

C# RadioButton and Calculator



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 WindowsFormsApplication10
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int tx1,tx2,rs=0;

            tx1=int.Parse(textBox1.Text);
            tx2=int.Parse(textBox2.Text);



            if (radioButton1.Checked)
                rs = tx1 + tx2;
            else if (radioButton2.Checked)
                rs = tx1 - tx2;
            else if (radioButton3.Checked)
                rs = tx1 * tx2;
            else if (radioButton4.Checked)
                rs = tx1 / tx2;

            textBox3.Text = rs.ToString();
          }

     
    }
}

C# ComboBox and ListBox


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 WindowsFormsApplication9
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
         
        }

        private void button1_Click(object sender, EventArgs e)
        {

         
            if (comboBox1.SelectedIndex == -1)
                MessageBox.Show("Seçenek bulunuz");
            else
                listBox1.Items.Add(comboBox1.SelectedItem);
           }

        private void button2_Click(object sender, EventArgs e)
        {

            listBox1.Items.Remove(listBox1.SelectedItem);

        }

     
    }
}