Tuesday, January 27, 2015

Program to perform Updation and Deletion in Connection-Oriented Architecture

9:53 AM - By ajay desai 0


Explanation: -  The below given program explains about how update and delete records in a database through an application by using connection-oriented architecture. We are using the following class in the program: -

  • Command: This class of System.Data.SqlClient namespace is used to perform Insert, Select, Update and Delete operations on the data source by sending a request to a Data Source in the form of a SQL statement.  This class consists of three methods to execute the SQL statements, these methods are:-
1.      ExecuteReader() :-  return type of this method is DataReader object as it contains the selected row. 
2.    ExecuteScalar(): - return type of this method is ‘object’ as it returns a single value of any data type.
3.      ExecuteNonQuery(): - This method is used to perform insertion, updation and deletion. The return type of this method is ‘int’ as it tells about the number of rows affected after performing these operations.

Program: -


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

namespace InsertandUpdate
{
    public partial class Form1 : Form
    {
        SqlConnection con;
        SqlCommand cmd;
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'lIB_MGMTDataSet.books' table. You can move, or remove it, as needed.
            this.booksTableAdapter.Fill(this.lIB_MGMTDataSet.books);

        }

        private void bookids_Leave(object sender, EventArgs e)
        {
            showdata();
        }
        private void showdata()
        {
            bkname.ReadOnly = athname.ReadOnly = publication.ReadOnly = false;
            con = new SqlConnection("Data Source=IFMRJPAL-PC\\TESTDB; Initial Catalog=LIB_MGMT; Integrated Security=True;");
            con.Open();
            string q = "select bookname,authname,publication from books where bookid='" + bookids.Text + "'";
            cmd = new SqlCommand(q, con);
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                bkname.Text = dr[0].ToString();
                athname.Text = dr[1].ToString();
                publication.Text = dr[2].ToString();
            }
            con.Close();
            update.Enabled = true;
            delete.Enabled = true;
        }

        private void cancel_Click(object sender, EventArgs e)
        {
            cleandata();
        }
        private void cleandata()
        {
            bkname.Text = athname.Text = publication.Text = "";
            bkname.ReadOnly = athname.ReadOnly = publication.ReadOnly = true;
            update.Enabled = false;
            delete.Enabled = false;
        }

        private void bookids_Enter(object sender, EventArgs e)
        {
            cleandata();
        }

        private void update_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show(this, "Do You Want To Update this Book Details ?", "UPDATE BOOK DETAILS", MessageBoxButtons.YesNo);
            if (dr == DialogResult.Yes)
            {
                con = new SqlConnection("Data Source=IFMRJPAL-PC\\TESTDB; Initial Catalog=LIB_MGMT; Integrated Security=True;");
                con.Open();
                string q = "update books set bookname='" + bkname.Text + "', authname='" + athname.Text + "', publication='" + publication.Text + "' where bookid='" + bookids.Text + "'";
                cmd = new SqlCommand(q, con);
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("this book details are updated");
                showdata();
            }

        }

        private void delete_Click(object sender, EventArgs e)
        {
            DialogResult dr1 = MessageBox.Show(this, "Do You Want To delete this Book Details ?", "DELETE BOOK DETAILS", MessageBoxButtons.YesNo);
            if (dr1 == DialogResult.Yes)
            {
                con = new SqlConnection("Data Source=IFMRJPAL-PC\\TESTDB; Initial Catalog=LIB_MGMT; Integrated Security=True;");
                con.Open();
                string q = "delete from books where bookid='" + bookids.Text + "'";
                cmd = new SqlCommand(q, con);
                cmd.ExecuteNonQuery();
                con.Close();
                MessageBox.Show("this book details are deleted");
                cleandata();
                this.booksTableAdapter.Fill(this.lIB_MGMTDataSet.books);
            }
        }
    }
}






















Tags:
About the Author

I am Azeheruddin Khan having more than 6 year experience in c#, Asp.net and ms sql.My work comprise of medium and enterprise level projects using asp.net and other Microsoft .net technologies. Please feel free to contact me for any queries via posting comments on my blog,i will try to reply as early as possible. Follow me @fresher2programmer
View all posts by admin →

Get Updates

Subscribe to our e-mail newsletter to receive updates.

Share This Post

0 comments:

adsense

© 2014 Fresher2Programmer. WP Theme-junkie converted by Bloggertheme9
Powered by Blogger.
back to top