private void Btnupdate_Click(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                int        j  = 0;
                SqlCommand ad = new SqlCommand("UPDATE CProductMaster SET ID='" + CTxtid.Text + "',ProName='" + CTxtname.Text + "',PhyState='" + CTxtstate.Text + "',ComName='" + CTxtcom.Text + "',Grade='" + CTxtgrade.Text + "'," +
                                               "Unit='" + CTxtUnit.Text + "',UnitName='" + CTxtUnitType.Text + "',Image=@Image,Origin='" + CTxtorigin.Text + "',Purchase='" + CTxtpurchase.Text + "',PackageT='" + CTxtPackT.Text + "',MRP='" + CTxtMRP.Text + "'," +
                                               "PackageS='" + CTxtPackS.Text + "',TUnit='" + CTxtTotal.Text + "',TUnitName='" + CtxtUnitT.Text + "',Usage='" + CTxtUsage.Text + "',Vat='" + CTxtVat.Text + "',Discount='" + CTxtdis.Text + "',Quantity='" + CTxtQP.Text + "',RName='" + CTxtweight.Text + "',RackNo='" + CTxtrack.Text + "' where ID=" + CTxtid.Text + "", con);

                MemoryStream ms = new MemoryStream();
                CTxtpic.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] pic = ms.ToArray();
                ad.Parameters.AddWithValue("@Image", pic);
                j = ad.ExecuteNonQuery();
                if (j > 0)
                {
                    MessageBox.Show("Record Update Successfully!");
                }

                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                CTxtid.Text       = "";
                CTxtname.Text     = "";
                CTxtstate.Text    = null;
                CTxtcom.Text      = null;
                CTxtgrade.Text    = "";
                CTxtUnit.Text     = "";
                CTxtUnitType.Text = null;
                CTxtorigin.Text   = "";
                CTxtpurchase.Text = "";
                CTxtPackT.Text    = "";
                CTxtMRP.Text      = "";
                CTxtPackS.Text    = "";
                CTxtTotal.Text    = "";
                CtxtUnitT.Text    = null;
                CTxtUsage.Text    = "";
                CTxtVat.Text      = "";
                CTxtdis.Text      = "";
                CTxtQP.Text       = "";
                CTxtweight.Text   = "";
                CTxtrack.Text     = null;
                Txtsrate.Text     = "";
                CTxtpic.Image     = null;
                CdataGridView.RefreshEdit();
                try
                {
                    int a;
                    con.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd             = con.CreateCommand();
                    cmd.CommandType = CommandType.Text;
                    cmd.CommandText = @"select MAX(ID) from CProductMaster";
                    SqlDataReader dr;
                    dr = cmd.ExecuteReader();
                    if (dr.Read())
                    {
                        string val = dr[0].ToString();
                        if (val == "")
                        {
                            CTxtid.Text = "1";
                        }
                        else
                        {
                            a           = Convert.ToInt32(dr[0].ToString());
                            a           = a + 1;
                            CTxtid.Text = a.ToString();
                        }
                    }
                    con.Close();
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
        private void Btnsave_Click(object sender, EventArgs e)
        {
            //SELECT TOP 1000[ProCode] ,[ProName] ,[PhyState] ,[ComName] ,[Grade] ,[Unit] ,[UnitName] ,[Image] ,[Origin] ,[Purchase] ,[PackageT] ,[MRP]
            //,[SaleRate] ,[PackageS] ,[TUnit] ,[TUnitName] ,[Usage] ,[Tsale] ,[Tpurchase] ,[Vat] ,[Discount] ,[Quantity] ,[RName] ,[RackNo]
            //FROM[dbo].[CProductMaster]
            try
            {
                con.Open();
                int        i   = 0;
                SqlCommand cmd = new SqlCommand();
                cmd             = con.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = @"INSERT INTO CProductMaster VALUES('" + CTxtid.Text + "','" + CTxtname.Text + "','" + CTxtstate.Text + "','" + CTxtcom.Text + "'," +
                                  "'" + CTxtgrade.Text + "','" + CTxtUnit.Text + "','" + CTxtUnitType.Text + "',@Image,'" + CTxtorigin.Text + "','" + CTxtpurchase.Text + "','" + CTxtPackT.Text + "'," +
                                  "'" + CTxtMRP.Text + "','" + Txtsrate.Text + "','" + CTxtPackS.Text + "','" + CTxtTotal.Text + "','" + CtxtUnitT.Text + "','" + CTxtUsage.Text + "','" + TxtTs.Text + "'," +
                                  "'" + TxtPr.Text + "','" + CTxtVat.Text + "','" + CTxtdis.Text + "','" + CTxtQP.Text + "','" + CTxtweight.Text + "','" + CTxtrack.Text + "')";
                MemoryStream mm = new MemoryStream();
                CTxtpic.Image.Save(mm, System.Drawing.Imaging.ImageFormat.Jpeg);
                byte[] pic = mm.ToArray();
                cmd.Parameters.AddWithValue("@Image", pic);
                i = cmd.ExecuteNonQuery();
                if (i > 0)
                {
                    MessageBox.Show("Chemical " + CTxtname.Text, "Successfully added in Data.", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }

                SqlDataAdapter ad = new SqlDataAdapter("SELECT * FROM [CProductMaster]", con);
                DataSet        ds = new DataSet();
                ad.Fill(ds);
                DataTable dt = ds.Tables[0];
                CdataGridView.DataSource = dt;
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                CTxtid.Text       = "";
                CTxtname.Text     = "";
                CTxtstate.Text    = null;
                CTxtcom.Text      = null;
                CTxtgrade.Text    = "";
                CTxtUnit.Text     = "";
                CTxtUnitType.Text = null;
                CTxtorigin.Text   = "";
                CTxtpurchase.Text = "";
                CTxtPackT.Text    = "";
                CTxtMRP.Text      = "";
                CTxtPackS.Text    = "";
                CTxtTotal.Text    = "";
                CtxtUnitT.Text    = null;
                CTxtUsage.Text    = "";
                CTxtVat.Text      = "";
                CTxtdis.Text      = "";
                CTxtQP.Text       = "";
                CTxtweight.Text   = "";
                CTxtrack.Text     = null;
                Txtsrate.Text     = "";
                CTxtpic.Image     = null;
                CdataGridView.Refresh();
            }
            try
            {
                int a;
                con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd             = con.CreateCommand();
                cmd.CommandType = CommandType.Text;
                cmd.CommandText = @"select MAX(ID) from CProductMaster";
                SqlDataReader dr;
                dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    string val = dr[0].ToString();
                    if (val == "")
                    {
                        CTxtid.Text = "1";
                    }
                    else
                    {
                        a           = Convert.ToInt32(dr[0].ToString());
                        a           = a + 1;
                        CTxtid.Text = a.ToString();
                    }
                }
                con.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }