Showing posts with label website development. Show all posts
Showing posts with label website development. Show all posts

Wednesday, 26 August 2015

code to insert data and display data from database MS ACESS in c#.net


database connection oledb drivers

 public static String MyDataFile = System.Windows.Forms.Application.StartupPath + @"\hardb.mdb";

   
        public String constr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source= " + MyDataFile + ";Jet OLEDB:Database Password=highclonosoftec"; 
        


code for inserting in database on save button in c# 



 OleDbConnection con = new OleDbConnection(conclass.constr);
            try
            {
                if (btrue == true)
                {
                 

                    string query = "insert into stock_master(ID,stock_item,initialprice,price,purchase_price,company,stock_entry_date,stock_loaded,stock_remaining,stock_photo,stock_tax_applied,stock_tax_cal,serial_model,model_number,imie_number,is_del) values (" + txtstock_id.Text + ",'" + txtstock_name.Text + "',"+txtinitialpurchase.Text+"," + txtstock_price.Text + "," + txtpurchase_price.Text + ",'" + txtstock_company.Text + "','" + txtstock_date.Text + "'," + txtstock_loaded.Text + "," + txtstock_remaning.Text + ",'" + txtphoto.Text + "'," + cmbtax.Text + "," + txttax.Text + ",'" + txtserial.Text + "','" + txtmodel.Text + "','" + txtimienumber.Text + "','" + false_1 + "')";
                    con.Open();
                    OleDbCommand cmd = new OleDbCommand(query, con);

                    cmd.ExecuteNonQuery();

                   

                    MessageBox.Show("Record inserted Successfully");


                }
                if (btrue == false)
                {


                    con.Open();
                    string query = "UPDATE stock_master SET stock_item='" + txtstock_name.Text + "',initialprice="+txtinitialpurchase.Text+",price=" + txtstock_price.Text + ",purchase_price=" + txtpurchase_price.Text + ",company='" + txtstock_company.Text + "',stock_entry_date='" + txtstock_date.Text + "',stock_loaded=" + txtstock_loaded.Text + ",stock_remaining=" + txtstock_remaning.Text + ",stock_photo='" + txtphoto.Text + "',stock_tax_applied=" + cmbtax.Text + ",stock_tax_cal=" + txttax.Text + ",serial_model='" + txtserial.Text + "',model_number='" + txtmodel.Text + "',imie_number='" + txtimienumber.Text + "',is_del='" + false_1 + "' where ID=" + txtstock_id.Text;

                    OleDbCommand cmd = new OleDbCommand(query, con);

                    cmd.ExecuteNonQuery();

                 

                    MessageBox.Show("Record updated Successfully");

                }


            }
            catch (Exception ee)
            {

                MessageBox.Show(ee.Message);
            }
            finally
            {
                con.Close();
            }


code to display record from  database in datagrid c#

              OleDbConnection con = new OleDbConnection(conclass.constr);
            try
            {
                con.Open();
                string query = "select * from stock_master where is_del='" + false + "'";
                OleDbCommand cmd = new OleDbCommand(query, con);
                OleDbDataReader dr = cmd.ExecuteReader();
                DataTable dt = new DataTable();

                for (int i = 0; i < dr.FieldCount; i++)
                {
                    dt.Columns.Add(dr.GetName(i));
                }

                while (dr.Read())
                {
                    DataRow drow = dt.NewRow();
                    for (int i = 0; i < dr.FieldCount; i++)
                    {
                        drow[i] = dr[i];
                    }
                    dt.Rows.Add(drow);
                }

                dataGridView1.DataSource = dt;
            }
            catch (Exception ee)
            {

            }