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;
}

Wednesday, 22 June 2011


C Program for fibanacci series using recursion.

#include<stdio.h>
#include<conio.h>
int fib(int);
void main()
{
int a,b,i,c;
clrscr();
printf("Enter the N value");
scanf("%d",&a);
for(i=0;i<a;i++)
{
printf("\n%d",fib(i));
}
getch();
}
int fib(int x)
{
if(x==0)
return(0);
if(x<=1)
{
return(1);
}
else
{
return(fib(x-1)+(fib(x-2)));
}
}
C PROGRAM FOR PASCAL TRIANGLE.

#include<stdio.h>
#include<conio.h>
void main()
{
int i=1,j,num,x=1;
clrscr();
printf("Please Enter the Number :");
scanf("%d",&num);
printf("\n\n");
printf("OUTPUT:\n");
printf("------");
printf("\n\n");
printf("The Pascal triangle is\n");
printf("**********************\n");
for(i=1;i<=num;i++)
{
for(j=1;j<=i;j++)
{
if(j==1)
{
x=1;
printf("%d",x);
}
else
{
x=(x*(i-j+1)/(j-1));
printf("%d",x);
}
}
printf("\n");
}
getch();
}
C Program for Prime Number Generation.

#include<stdio.h>
#include<conio.h>
void main()
{
int n,j,a=0,i;
clrscr();
printf(" Please Enter a number :");
scanf("%d",&n);
printf("\n\n");
printf("OUTPUT:\n");
printf("------");
printf("\n\n");
printf("The Prime Number series is\n");
printf("**************************");
for(j=1;j<=n;j++)
{
for(i=2;i<j;i++)
{
if(j%i==0)
{
a=1;
goto x;
}
}
if((a==0)&&(j!=1))
{
printf("\n%d",j);
}
x:
a=0;
}
getch();
}
C Program For REPLACE OF STRING. 

#include<stdio.h>
#include<conio.h>
void main()
{
char a[100],b[100],c[100],d[100];
int i,j,k,l1,l2,l3,m,count=-1;
clrscr();
printf("\t\t\t REPLACE OF STRING ");
printf("\n\t\t\t ********************");
printf("\n\n\tINPUT");
printf("\n\t*******");
printf("\n\nEnter the first String:");
scanf("%[a-z ]s",a);
printf("\nEnter the substring:");
scanf("%s",b);
printf("\nEnter the replace string:");
scanf("%s",c);
printf("\n\n\tOUTPUT");
printf("\n\t*******");
for(i=0;a[i]!='\0';i++)
l1=i;
for(i=0;b[i]!='\0';i++)
l2=i;
for(i=0;c[i]!='\0';i++)
l3=i;
for(i=0;a[i]!='\0';i++)
d[i]=a[i];
d[i]='\0';
if(l1>=l2)
{
do
{
for(i=0,j=0;a[i]!='\0' && b[j]!='\0';i++ )
{
if(a[i]==b[j])
{
count++;
j++;
}
else
{
count=-1;
if(j>0)
i--;
j=0;
}
}
m=i;
if(count==l2)
{
i=i-j;
for(k=0;c[k]!='\0';i++,k++)
a[i]=c[k];
if(l2!=l3)
{
for( ;d[m]!='\0';m++,i++)
{
a[i]=d[m];
}
}
//a[i]='\0';
//printf("\nReplaced string:%s",a);
}
else
break;
//printf("\nThe substring is not there");
}while(a[i]!='\0');
printf("\nReplaced string:%s",a);
}
getch();
}
C Program for String Length.

#include<stdio.h>
#include<conio.h>
void main()
{
int i,c=0;
char a[50];
clrscr();
printf("OUTPUT\n");
printf("------");
printf("\n\n");
printf("Please Enter a String:\n");
printf("*********************");
scanf("%s",a);
for(i=0;a[i]!='\0';i++)
{
c=c+1;
}
printf("\n\n");
printf("Length Of The Given String is=%d\n",c);
printf("*****************************");
getch();
}
C Program For REPLACE OF STRING.

#include<stdio.h>
#include<conio.h>
void main()
{
char a[100],b[100],c[100],d[100];
int i,j,k,l1,l2,l3,m,count=-1;
clrscr();
printf("\t\t\t REPLACE OF STRING ");
printf("\n\t\t\t ********************");
printf("\n\n\tINPUT");
printf("\n\t*******");
printf("\n\nEnter the first String:");
scanf("%[a-z ]s",a);
printf("\nEnter the substring:");
scanf("%s",b);
printf("\nEnter the replace string:");
scanf("%s",c);
printf("\n\n\tOUTPUT");
printf("\n\t*******");
for(i=0;a[i]!='\0';i++)
l1=i;
for(i=0;b[i]!='\0';i++)
l2=i;
for(i=0;c[i]!='\0';i++)
l3=i;
for(i=0;a[i]!='\0';i++)
d[i]=a[i];
d[i]='\0';
if(l1>=l2)
{
do
{
for(i=0,j=0;a[i]!='\0' && b[j]!='\0';i++ )
{
if(a[i]==b[j])
{
count++;
j++;
}
else
{
count=-1;
if(j>0)
i--;
j=0;
}
}
m=i;
if(count==l2)
{
i=i-j;
for(k=0;c[k]!='\0';i++,k++)
a[i]=c[k];
if(l2!=l3)
{
for( ;d[m]!='\0';m++,i++)
{
a[i]=d[m];
}
}
//a[i]='\0';
//printf("\nReplaced string:%s",a);
}
else
break;
//printf("\nThe substring is not there");
}while(a[i]!='\0');
printf("\nReplaced string:%s",a);
}
getch();
}