Small is New Big in C#

Coding

Archive for August 2010

Make TextBox Number Only With Length Limit

leave a comment »

    //Detect Value
        private void txtID_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (Char.IsControl(e.KeyChar) || Char.IsDigit(e.KeyChar))
            { e.Handled = false; }
            else
            { e.Handled = true; }
        }

    //Check TextBox.Text Length
        private string CollectionIDCheck(string id,int length)
        {
            Regex check = new Regex("^[0-9]{" + length.ToString() +"}");
            int count;
            if (!check.IsMatch(id))
            {

                if (id.Length < length)
                {
                    count = length – id.Length;
                    string zero = "0";
                    for (int i = 1; i < count; i++)
                        zero = "0" + zero;
                    id = zero + id;
                }
                else if (id.Length > length)
                {
                    count = id.Length – length;
                    id = id.Substring(count,
length);
                }
            }
            return id;
        }

Written by nauhil

August 30, 2010 at 10:48 am

Posted in Tips of C#

Update DataSet using SqlDataAdapter

leave a comment »

        private DataSet ds;
        private SqlConnection sqlconn;
        private SqlCommand comm;
        private SqlDataAdapter sqlda;
        private SqlCommandBuilder commBuilder;

        public SqlConnection getcon()
        {
            string M_str_sqlcon = "Data Source=(local);Database=Northwind;user Id=sa;PWD=";
            SqlConnection mycon = new SqlConnection(M_str_sqlcon);
            return mycon;
        }

        private void btnExportSql_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < ds.Tables["Categories"].Rows.Count; i++)
            {
                ds.Tables["Categories"].Rows[i][1] = "Teeeeest";
            }
            sqlda.Update(ds, "Categories");
            dataGridView1.DataSource = ds.Tables["Categories"];
        }

        private void frmMain_Load(object sender, EventArgs e)
        {
            sqlconn = this.getcon();
            comm = sqlconn.CreateCommand();
            comm.CommandText = "SELECT CategoryID,CategoryName, Description FROM Categories";
            sqlda = new SqlDataAdapter(comm);
            commBuilder = new SqlCommandBuilder(sqlda);
            ds = new DataSet();
            sqlda.Fill(ds, "Categories");
            dataGridView1.DataSource = ds.Tables["Categories"];
        }

Written by nauhil

August 27, 2010 at 1:16 pm

Posted in Tips of C#