Sunday, July 1, 2012

Passing Data From One Form to another in C#, THE EASIER WAY.

This is how you pass data from one form to another in c#. There are lot of methods to do this and this one is the easiest of the lot.
USING CONSTRUCTOR

In Form1 in the function where you move to the other form pass the value you want to send to the other form while instantiating the Form Object.


  private void button1_Click(object sender, System.EventArgs e)
  {
    Form2 frm=new Form2(textBox1.Text);
    frm.Show();
  }

In Form2 add a paramaterized constructor to receive that data that was sent from Form1.

  
  public Form2(string strTextBox)
   {
   InitializeComponent(); 
   label1.Text=strTextBox;
   }

And that is it you have passed data from one form to another.


Tuesday, June 26, 2012

Database operations select, insert, update and delete in c# with SQL


There are basically four functions each one for a database operation. Parameters are used in insert, update and select. 

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 AddressBook
{
    public partial class Form1 : Form
    {


        public Form1()
        {
            InitializeComponent();


        }


        //INSERT
        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Thangamani\Documents\visual studio 2010\Projects\AddressBook\AddressBook\Database1.mdf;Integrated Security=True;User Instance=True";
                string na = textBox1.Text;
                int ag = int.Parse(textBox2.Text);
                string ci = textBox3.Text;
                using (SqlConnection connection = new SqlConnection(connectionString))
                {


                    using (SqlCommand insertCommand = connection.CreateCommand())
                    {
                        insertCommand.CommandText = "INSERT INTO address(name,age,city) VALUES (@na,@ag,@ci)";
                        insertCommand.Parameters.AddWithValue("@na", na);
                        insertCommand.Parameters.AddWithValue("@ag", ag);
                        insertCommand.Parameters.AddWithValue("@ci", ci);




                        insertCommand.Connection.Open();
                        insertCommand.ExecuteNonQuery();
                        insertCommand.Connection.Close();
                        MessageBox.Show("Data Successfully Inserted");


                    }
                    
                    //connection.Close();
                }


            }
            finally { }


        }


        //SELECT
        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                string s = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Thangamani\Documents\visual studio 2010\Projects\AddressBook\AddressBook\Database1.mdf;Integrated Security=True;User Instance=True";
                SqlConnection a = new SqlConnection(s);
                a.Open();
                SqlCommand comm = new SqlCommand("SELECT * FROM address");
                comm.Connection = a;
                SqlDataReader re = comm.ExecuteReader();
                while (re.Read())
                {
                    textBox1.Text = re["name"].ToString();
                    textBox2.Text = re["age"].ToString();
                    textBox3.Text = re["city"].ToString();


                    MessageBox.Show("Data Selected");
                }
            }
            finally
            {
            }
        }


        //DELETE
        private void button3_Click(object sender, EventArgs e)
        {
            try
            {
                string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Thangamani\Documents\visual studio 2010\Projects\AddressBook\AddressBook\Database1.mdf;Integrated Security=True;User Instance=True";
                string na = textBox1.Text;
                using (SqlConnection connection = new SqlConnection(connectionString))
                {


                    using (SqlCommand insertCommand = connection.CreateCommand())
                    {
                        insertCommand.CommandText = "DELETE FROM address WHERE name LIKE @na";
                        insertCommand.Parameters.AddWithValue("@na", na);






                        insertCommand.Connection.Open();
                        insertCommand.ExecuteNonQuery();
                        insertCommand.Connection.Close();
                        MessageBox.Show("Data successfully Deleted");


                    }




                }
            }
            finally { }
        }


        //UPDATE
        private void button4_Click(object sender, EventArgs e)
        {
            try
            {
                string connectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\Thangamani\Documents\visual studio 2010\Projects\AddressBook\AddressBook\Database1.mdf;Integrated Security=True;User Instance=True";
                string na = textBox1.Text;
                int ag = int.Parse(textBox2.Text);
                string ci = textBox3.Text;
                using (SqlConnection connection = new SqlConnection(connectionString))
                {


                    using (SqlCommand insertCommand = connection.CreateCommand())
                    {
                        insertCommand.CommandText = "UPDATE address SET age=@ag, city=@ci WHERE name LIKE @na";
                        insertCommand.Parameters.AddWithValue("@na", na);
                        insertCommand.Parameters.AddWithValue("@ag", ag);
                        insertCommand.Parameters.AddWithValue("@ci", ci);




                        insertCommand.Connection.Open();
                        insertCommand.ExecuteNonQuery();
                        insertCommand.Connection.Close();
                        MessageBox.Show("Data successfully Updated");


                    }




                }
            }
            finally { }
        }
    }
}