Library data save application in c#

May 31, 2013

Library data save application in c#

Develop an application that allows the user to retrieve data from a database and perform updates. The application should provide following features:
1.      A ComboBox that populates a list of items retrieved from the database for search criteria
2.      A Search button that retrieves a list of records for the item selected in the ComboBox and populates a DataGridView with these records (Master portion of the Master-Detail). Show at least 4 data columns.
3.      A set of individual fields that are filled once a row is selected in the DataGridView (Detail portion of the Master-Detail).

4.      A button to perform update to the data changed in the Detail portion.
      
  Code

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 lab7
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        string query;
       
        private void Form1_Load(object sender, EventArgs e)
        {
            SqlCommand combo_pop= new SqlCommand();
            combo_pop.Connection = con; ;
            combo_pop.CommandType=CommandType.Text;
            query="select distinct author from library";
            combo_pop.CommandText= query;
          
            con.Open();
            SqlDataReader dr = combo_pop.ExecuteReader();
            while (dr.Read())
            {
                comboBox1.Items.Add((string)dr["Author"]);
            }
            dr.Close();
            con.Close();
           
  
        }

        private void search_Click(object sender, EventArgs e)
        {
            query = "select BookID,BookName,PublishDate,Price from library where Author=(@Author)";
            SqlParameter p1 = new SqlParameter("@Author", SqlDbType.NVarChar, 50, "Author");
            p1.Value = comboBox1.Text;
            SqlCommand grid_pop = new SqlCommand();
            grid_pop.Connection = con;
            grid_pop.CommandType = CommandType.Text;
            grid_pop.CommandText = query;
            grid_pop.Parameters.Add(p1);
            con.Open();
            SqlDataReader dr = grid_pop.ExecuteReader();
            DataTable table = new DataTable();
            table.Load(dr);
            dataGridView1.DataSource = table;
            con.Close();
        }

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            tb2.Width = tb1.Width;
            tb1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            tb2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            tb3.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            tb4.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            if (tb2.Width < (TextRenderer.MeasureText(tb2.Text, tb2.Font)).Width)
                tb2.Width =(TextRenderer.MeasureText(tb2.Text, tb2.Font)).Width;
        }

     

        private void comboBox1_TextChanged(object sender, EventArgs e)
        {

          reset();
           
        }
        public void reset()
        {
            tb1.Text = "";
            tb2.Text = "";
            tb3.Text = "";
            tb4.Text = "";
            tb2.Width = tb1.Width;
            dataGridView1.DataSource = null;
            dataGridView1.Rows.Clear();
        }

        private void Update_btn_Click(object sender, EventArgs e)
        {
            SqlCommand upd_command = new SqlCommand();
            query = "update Library set BookName=@BookName, PublishDate=@PublishDate, Price=@Price where BookID=@BookID";
            upd_command.CommandType = CommandType.Text;
            upd_command.Connection = con;
            upd_command.CommandText = query;
            SqlParameter p1 = new SqlParameter("@BookID",SqlDbType.Int,4,"BookID");
            SqlParameter p2 = new SqlParameter("@BookName",SqlDbType.NVarChar,100,"BookName");
            SqlParameter p3 = new SqlParameter("@PublishDate",SqlDbType.SmallDateTime,15,"");
            SqlParameter p4 = new SqlParameter("@Price",SqlDbType.Int,4,"Price");
            p1.Value = int.Parse(tb1.Text);
            p2.Value = tb2.Text;
            p3.Value = tb3.Text;
            p4.Value = int.Parse(tb4.Text);
            upd_command.Parameters.Add(p1);
            upd_command.Parameters.Add(p2);
            upd_command.Parameters.Add(p3);
            upd_command.Parameters.Add(p4);

            con.Open();
            int rows_affected = upd_command.ExecuteNonQuery();
            con.Close();
            reset();

        }
  }
       
    }

               Screen Shot






You Might Also Like

0 comments

Popular Posts

Like us on Facebook

Flickr Images