Thursday, 9 February 2012

C# : Saving Excel File in C#


Excel.Application myExcelApp;
                        Excel.Workbook myExcelWorkbook;
                        Excel.Worksheet myExcelWorksheet;
                        object misValue = System.Reflection.Missing.Value;
                        myExcelApp = new Excel.ApplicationClass();

                        myExcelWorkbook = myExcelApp.Workbooks.Add(misValue);
                        myExcelWorksheet = (Excel.Worksheet)myExcelWorkbook.ActiveSheet;
                        myExcelApp.Visible = false;

                         myExcelWorksheet.SaveAs(filepath, Microsoft.Office.Interop.Excel.XlFileFormat.xlExcel8, misValue, misValue, misValue, misValue, misValue, misValue, misValue, misValue);
                        myExcelApp.Quit();




// this coding working fine in  c# 3.5 frame work


Friday, 23 December 2011

C# Text box only allow two decimal points and numbers using text box keypress event


 private void txtPrice_KeyPress(object sender, KeyPressEventArgs e)
        {


            if ((e.KeyChar < '0' || e.KeyChar > '9') && (e.KeyChar != '\b') && (e.KeyChar != '.'))
            {
                e.Handled = true;
            }
            else
            {
                e.Handled = false;
            }
            if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsNumber(e.KeyChar) || e.KeyChar == '.')
            {
                TextBox tb = sender as TextBox;
                int cursorPosLeft = tb.SelectionStart;
                int cursorPosRight = tb.SelectionStart + tb.SelectionLength;
                string result = tb.Text.Substring(0, cursorPosLeft) + e.KeyChar + tb.Text.Substring(cursorPosRight);
                string[] parts = result.Split('.');
                if (parts.Length > 1)
                {
                    if (parts[1].Length > 2 || parts.Length > 2)
                    {
                        e.Handled = true;
                    }
                }

C# OLEDB CONNECTION STRING FOR EXCEL ( USE EXCEL AS DATABASE)


 System.Data.OleDb.OleDbConnection MyConnection;
                System.Data.OleDb.OleDbCommand myCommand = new System.Data.OleDb.OleDbCommand();
                System.Data.OleDb.OleDbCommand create = new System.Data.OleDb.OleDbCommand();
                string sql = null, cretable = null; ;
                MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source='" + filePath + "';Extended Properties=\"Excel 8.0;HDR=Yes;\"");
                //MyConnection = new System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + filePath + "';Extended Properties=Excel 8.0;");
                //Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\MyExcel.xls;Extended Properties="Excel 8.0;HDR=Yes;IMEX=1";
                MyConnection.Open();
                myCommand.Connection = MyConnection;
                create.Connection = MyConnection;
                cretable = "create Table [Sheet1$] (InvoiceNo int,ProductId int,ProductName char(20),Quantity int,Price int,Amount int,TransactionNo Text)";
                create.CommandText = cretable;
                create.ExecuteNonQuery();

Wednesday, 23 November 2011

*** THIS JAVASCRIPT ONLY ALLOW TWO DECIMAL POINTS IN TEXT BOX ***



function numberOnly(txt, e) {
var arr = “0123456789.”;
var code;
var code1;
if (window.event)
code = e.keyCode;
else
code = e.which;
var cha = String.fromCharCode(code);
if (arr.indexOf(cha) == -1)
return false;
else if (cha == “.”)
if(txt.value.indexOf(“.”)>-1)
return false;
if(txt.value.indexOf(“.”)>-1)
{
code1=txt.value.indexOf(“.”);
if(txt.value.length == code1 + 3)
return false;
}