Tuesday 10 July 2012

How to stored values and retrieve vaule for Checklist box in windows application


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 MySql.Data.MySqlClient;
using System.Configuration;

namespace MalaysiaQRA
{
    public partial class Master_APW4 : Form
    {
        MySqlCommand cmd;
        MySqlDataReader rd;
        MySqlConnection con;
        MySqlTransaction tn;
        DataTable dt = new DataTable();
        public Master_APW4()
        {
            string connection = ConfigurationManager.AppSettings["ConnectionString"].ToString();
            con = new MySqlConnection(connection);
            InitializeComponent();
        }

        private void Master_APW4_Load(object sender, EventArgs e)
        {
            DataSet ds = new DataSet();
            MySqlDataAdapter ada = new MySqlDataAdapter("select cur_cod,des from tblcurrency",con);
            ada.Fill(ds);
            for (int i=0; i < ds.Tables[0].Rows.Count; i++)
            {
                checkedListBox1.Items.Add(ds.Tables[0].Rows[i][0].ToString() +"-->"+ ds.Tables[0].Rows[i][1].ToString());
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                con.Open();
                cmd = new MySqlCommand("delete from master_apw",con);
                cmd.ExecuteNonQuery();
                for (int i = 0; i < checkedListBox1.Items.Count; i++)
                {
                    if (checkedListBox1.GetItemChecked(i) == true)
                    {   int u=checkedListBox1.Items[i].ToString().Length;
                    cmd = new MySqlCommand("insert into master_apw (cur_cod,description) values ('" + checkedListBox1.Items[i].ToString().Substring(0, 3) + "','" + checkedListBox1.Items[i].ToString().Substring(6,u-6) + "')", con);
                        cmd.ExecuteNonQuery();
                     
                    }
                }
           
                con.Close();
                MessageBox.Show("Updated Successfully!!!");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            finally
            {
                con.Close();
            }
        }

   
    }
}

No comments:

Post a Comment