Translate

Home > June 2012

June 2012

ASP.NET C# Popup MessageBox

Tuesday, June 26, 2012 Category : , 1

 public static void ShowAlertMessage(string error)
        {
            Page page = HttpContext.Current.Handler as Page;
            if (page != null)
            {
                error = error.Replace("'", "\'");

                ScriptManager.RegisterStartupScript(page, page.GetType(), "err_msg", "alert('" + error + "');", true);
            }
        }

ASP.NET Regular Expressions List

Category : 0

Expression List :
Minimum 3 Character Validation :    ^[a-zA-Z0-9]{3,}

Only 6 Numbers allowed :    [0-9]{6,}

Only Numbers Allowed :    ^\d+$

Date Validator : ^(0[1-9]|[12][0-9]|3[01])[- /.](0[1-9]|1[012])[- /.](19|20)\d\d$  (dd/MM/yyyy)        (dd/mm/yyyy)

Date Validator :  ^([0-9]{1,2})[./-]+([0-9]{1,2})[./-]+([0-9]{2}|[0-9]{4})$       (mm/dd/yyyy)

Mail Validator  :  

"^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$"

C# Problem With Inserting more than 3000 data in Database

Tuesday, June 19, 2012 Category : 0

// some time a problem arises when user perfrom some loop operation which perfrm more than
//3000 thousand calculation and some time CLR stop the process. in this case you need to use thread

private Thread demoThread = null;

 private void btnBrowse_Click(object sender, EventArgs e)
 {
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                                   this.demoThread = new Thread(new ThreadStart(InsertData));
                                   this.demoThread.Start();
                 // OR
                //ThreadPool.QueueUserWorkItem(new WaitCallback(InsertData), oChalanObject); 
            }
}

// note here InserData is a function which perfrom a insert operation. you can use thread in this two
//way second option is if u want to pass a parameter in Function .


 private void InsertData()
 {
        // write your code here
// note you can not access any winform control directyly if want to access a TextBox or Combobox or //something else then you need a delegate to perform this operation. for example
SetControlPropertyValue(lblProgress, "Text", value);
ClearGroupBoxContent(groupBox1);
}



// Generic Control Value Seter function
delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
        private void SetControlPropertyValue(Control oControl, string propName, object propValue)
        {
            try
            {
                if (oControl.InvokeRequired)
                {
                    SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
                    oControl.Invoke(d, new object[] { oControl, propName, propValue });
                }
                else
                {
                    Type t = oControl.GetType();
                    PropertyInfo[] props = t.GetProperties();
                    foreach (PropertyInfo p in props)
                    {
                        if (p.Name.ToUpper() == propName.ToUpper())
                        {
                            p.SetValue(oControl, propValue, null);
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
        }

// only a specific Control Property set
delegate void GroupBoxPerformStep(GroupBox group1);
        private void ClearGroupBoxContent(GroupBox group1)
        {
            if (group1.InvokeRequired)
            {
                GroupBoxPerformStep del = ClearGroupBoxContent;
                group1.Invoke(del, new object[] { group1 });
                return;
            }
            Result oResult = new Result();
            oResult.ClearAll(group1);
            //myProgressBar.PerformStep();

        }

C# Thread Handleing in Winform

Category : 0

private Thread demoThread = null;

 private void btnBrowse_Click(object sender, EventArgs e)
 {
    if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                                   this.demoThread = new Thread(new ThreadStart(InsertData));
                                   this.demoThread.Start();
                 // OR
                //ThreadPool.QueueUserWorkItem(new WaitCallback(InsertData), oChalanObject); 
            }
}

// note here InserData is a function which perfrom a insert operation. you can use thread in this two
//way second option is if u want to pass a parameter in Function .


 private void InsertData()
 {
        // write your code here
// note you can not access any winform control directyly if want to access a TextBox or Combobox or //something else then you need a delegate to perform this operation. for example
SetControlPropertyValue(lblProgress, "Text", value);
ClearGroupBoxContent(groupBox1);
}



// Generic Control Value Seter function
delegate void SetControlValueCallback(Control oControl, string propName, object propValue);
        private void SetControlPropertyValue(Control oControl, string propName, object propValue)
        {
            try
            {
                if (oControl.InvokeRequired)
                {
                    SetControlValueCallback d = new SetControlValueCallback(SetControlPropertyValue);
                    oControl.Invoke(d, new object[] { oControl, propName, propValue });
                }
                else
                {
                    Type t = oControl.GetType();
                    PropertyInfo[] props = t.GetProperties();
                    foreach (PropertyInfo p in props)
                    {
                        if (p.Name.ToUpper() == propName.ToUpper())
                        {
                            p.SetValue(oControl, propValue, null);
                        }
                    }
                }
            }
            catch (Exception)
            {

            }
        }

// only a specific Control Property set
delegate void GroupBoxPerformStep(GroupBox group1);
        private void ClearGroupBoxContent(GroupBox group1)
        {
            if (group1.InvokeRequired)
            {
                GroupBoxPerformStep del = ClearGroupBoxContent;
                group1.Invoke(del, new object[] { group1 });
                return;
            }
            Result oResult = new Result();
            oResult.ClearAll(group1);
            //myProgressBar.PerformStep();

        }

C# Read Excel (xls,xlsx) File

Friday, June 1, 2012 Category : 1

  public DataTable SelectAll(string FilePath, string Extension, string isHDR)
        {

            string conStr = "";

            switch (Extension)
            {

                case ".xls": //Excel 97-03

                    conStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}; Extended Properties='Excel 8.0;HDR={1}'";

                    break;

                case ".xlsx": //Excel 07

                    conStr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 8.0;HDR={1}'";

                    break;

            }

            conStr = String.Format(conStr, FilePath, isHDR);

            OleDbConnection connExcel = new OleDbConnection(conStr);

            OleDbCommand cmdExcel = new OleDbCommand();

            OleDbDataAdapter oda = new OleDbDataAdapter();

            DataTable dt = new DataTable();

            cmdExcel.Connection = connExcel;



            //Get the name of First Sheet

            connExcel.Open();

            DataTable dtExcelSchema;

            dtExcelSchema = connExcel.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);

            string SheetName = dtExcelSchema.Rows[0]["TABLE_NAME"].ToString();

            connExcel.Close();



            //Read Data from First Sheet

            connExcel.Open();

            cmdExcel.CommandText = "SELECT * From [" + SheetName + "]";

            oda.SelectCommand = cmdExcel;

            oda.Fill(dt);

            connExcel.Close();



            return dt;



        }


Powered by Blogger.