Friday, October 7, 2011

SQL Server(2005) With Data Add, Delete, Update and GridView

Form1.cs

using System;
using System.Data;
using System.Windows.Forms;

namespace GridView
{
    public partial class GridView : Form
    {
        Form1DataHandler callForm1DataHandler = new Form1DataHandler();
        public GridView()
        {
            InitializeComponent();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            string id = txbID.Text;
            string name = txbName.Text;
            string contact = txbContact.Text;
            string department = txbDepartment.Text;
            callForm1DataHandler.AddData(id, name, contact, department);
            ClearText();
        }

        private void btnView_Click(object sender, EventArgs e)
        {
            DataSet dataset = new DataSet();
            dataGridView1.Refresh();
            dataset = callForm1DataHandler.BindData();
            dataGridView1.DataSource = dataset.Tables[0];
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            string id = txbID.Text;
            string name = txbName.Text;
            string contact = txbContact.Text;
            string department = txbDepartment.Text;
            callForm1DataHandler.UpDate(id, name, contact, department);
            ClearText();
        }
        private void ClearText()
        {
            txbContact.Clear();
            txbDepartment.Clear();
            txbID.Clear();
            txbName.Clear();
        }

        private void txbID_KeyUp(object sender, KeyEventArgs e)
        {
           
            if (e.KeyValue == 13)       // ENTER KET EVENT CAPTURING
            {
                string id = txbID.Text;
                DataSet fillText = new DataSet();
                fillText = callForm1DataHandler.CheckText(id);
                if (fillText != null)
                {
                    txbName.Text = fillText.Tables[0].Rows[0][1].ToString();
                    txbContact.Text = fillText.Tables[0].Rows[0][2].ToString();
                    txbDepartment.Text = fillText.Tables[0].Rows[0][3].ToString();
                }
            }
        }

        private void btnDelet_Click(object sender, EventArgs e)
        {
            string id = txbID.Text;
            callForm1DataHandler.Delet(id);
            ClearText();
        }
    }
}



Form1DataHandler.cs


using System.Data;

namespace GridView
{
    class Form1DataHandler : DataBaseHandler
    {
        public void AddData(string id,string name,string contact,string department)
        {
            string quary="INSERT INTO client VALUES('"+id+"','"+name+"','"+contact+"','"+department+"')";
            ExecuteQuary(quary);  
        }

        public DataSet BindData()
        {
            string quary1 = "SELECT * FROM client";
            DataSet bindDataSet = new DataSet();
            bindDataSet = GetData(quary1);
            return bindDataSet;
        }

        public void UpDate(string id, string name, string contact, string department)
        {
            string quary = "UPDATE client SET NAME='" + name + "',CONTACT='" + contact + "',DEPARTMENT='" + department + "' WHERE ID='"+id+"'";
            ExecuteQuary(quary);
        }

        public void Delet(string id)
        {
            string quary = "DELETE FROM vin WHERE ID='" + id + "'";
            ExecuteQuary(quary);
        }

        public DataSet CheckText(string id)
        {
            string quary2 = "SELECT * FROM client WHERE ID='"+id+"'";
            DataSet checkData = new DataSet();
            checkData = GetData(quary2);
            return checkData;
        }
    }
}



DataBaseHandler.cs
 

 using System;
using System.Data;
using System.Data.SqlClient;

namespace GridView
{
    class DataBaseHandler
    {
        SqlConnection connection = new SqlConnection("Data Source =VINDANA-PC; database=vin; User =sa;Password=vinx");
        SqlCommand command = null;
        SqlDataAdapter adapter = null;
        public void ExecuteQuary(string quary)
        {
            try
            {
                if (connection.State == ConnectionState.Closed)
                {
                    connection.Open();
                    command = new SqlCommand();
                }
                command.CommandText = quary;
                command.Connection = connection;
                command.ExecuteNonQuery();
                connection.Close();
            }
            catch (Exception)
            {
          
            }
        }
        public DataSet GetData(string quary)
        {
            if (connection.State == ConnectionState.Closed)
            {
                connection.Open();
                adapter = new SqlDataAdapter(quary, connection);
            }
            DataSet getDataSet = new DataSet();
            adapter.Fill(getDataSet);
            connection.Close();
            return getDataSet;
        }
    }
}

5 comments:

  1. *****connection class*****

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    namespace xBoundRFC.Common
    {
    class Connection
    {
    string strConn;
    public DataSet ds;
    public SqlConnection con;

    public Connection()
    {
    ds = new DataSet();
    strConn = ConfigurationSettings.AppSettings.Get("ConnString");
    con = new SqlConnection(strConn);
    }

    public void closeCon()
    {
    if (con.State == ConnectionState.Open)
    {
    con.Close();
    }
    }

    public DataSet GetDataSet(string SqlString)
    {
    SqlDataAdapter daLocl;
    DataSet dsLocl = new DataSet();
    if (con.State == ConnectionState.Closed)
    {
    con.Open();
    }
    SqlCommand sCom = new SqlCommand(SqlString, con);
    daLocl = new SqlDataAdapter(sCom);
    daLocl.Fill(dsLocl);
    daLocl.Dispose();
    con.Close();
    return dsLocl;
    }

    public bool ExecuteCommand(string cmd)
    {
    if (con.State == ConnectionState.Closed)
    { con.Open(); }
    SqlCommand comLPM = new SqlCommand(cmd, con);
    int noRec = comLPM.ExecuteNonQuery();
    con.Close();
    return true;
    }

    public int GetNoOfRecords(string strsql)
    {
    DataSet dsCount = this.GetDataSet(strsql);
    int noOfRecors = dsCount.Tables[0].Rows.Count;
    dsCount.Dispose();
    return noOfRecors;
    }
    }
    }

    ReplyDelete
  2. ********* Bind Report.rdlc*************

    DataSet dsLoctBarcode = con.GetDataSet(sqlLocation);
    ReportDataSource rdsLoctmBarcode = new ReportDataSource("dsLocationBarcode_LocationMaster", dsLoctBarcode.Tables[0]);
    rvLocationBarcode.LocalReport.ReportPath = Path.Combine("Reports", "rptLocationBarcode.rdlc");
    ReportParameter[] ParamDM = new ReportParameter[1];
    ParamDM[0] = new ReportParameter("Merchandiser", strParam);
    rvLocationBarcode.LocalReport.SetParameters(ParamDM);
    rvLocationBarcode.LocalReport.DataSources.Clear();
    rvLocationBarcode.LocalReport.DataSources.Add(rdsLoctmBarcode);
    rvLocationBarcode.LocalReport.Refresh();

    ReplyDelete
  3. **********Bind Report.rdl*****************






    hdnReportServerURL.Value = WebConfigurationManager.AppSettings["ReportServerUrl"];
    hdnReportFolder.Value = WebConfigurationManager.AppSettings["ReportFolder"];

    ReportParameter[] rptParameters = new ReportParameter[2];

    rptParameters[0] = new ReportParameter();
    rptParameters[0].Name = "From";
    rptParameters[0].Values.Add(rDpFrom.SelectedDate.Value.ToString("MM/dd/yyyy"));

    rptParameters[1] = new ReportParameter();
    rptParameters[1].Name = "To";
    rptParameters[1].Values.Add(rDpTo.SelectedDate.Value.ToString("MM/dd/yyyy"));

    repViewer.ProcessingMode = Microsoft.Reporting.WebForms.ProcessingMode.Remote;
    repViewer.ServerReport.ReportServerUrl = new Uri(hdnReportServerURL.Value);
    repViewer.ServerReport.ReportPath = hdnReportFolder.Value + "/ContractorWiseCost";
    repViewer.ServerReport.SetParameters(rptParameters);
    repViewer.ServerReport.Refresh();

    ReplyDelete
  4. configuration
    startup

    supportedRuntime version="v2.0.50727"
    startup
    appSettings

    add key="ConnString" value="data source=servername;initial catalog=dbName;persist security info=True;user id=sa_rs;password=pwd;"
    appSettings
    configuration

    ReplyDelete