Wednesday, October 20, 2010

Check Printer Status in C#

Assembly : System.Management;

     public bool CheckPrinterStatus()
        {

            ManagementScope scope = new ManagementScope(@"\root\cimv2");
            scope.Connect();

            // Select Printers from WMI Object Collections

            ManagementObjectSearcher PrinterCollection= new
            ManagementObjectSearcher("SELECT * FROM Win32_Printer");

            string printerName = "";

            foreach (ManagementObject printer in PrinterCollection.Get())
            {
                printerName = printer["Name"].ToString().ToLower();
                if (printerName.Equals(@"hp laserjet 1018"))
                {
                    MessageBox.Show("Printer = " + printer["Name"]);
                    if (printer["WorkOffline"].ToString()=="True")
                    {
                        // Printer is offline

                        MessageBox.Show("Printer is not Connected.");
                        return false;
                    }
                    else
                    {
                        // Printer is Online
                       MessageBox.Show("Printer is Connected.");
                        return true;
                    }
                }
            }
        }

Monday, October 18, 2010

Copy all Data From one Table to another with same Structure in sql

copy all Data From one Table to another  with same Structure

insert into [MyDB].[dbo].[Emp] select * from [Info].[dbo].[A]

it will copy all data in EMP table from A Table

Saturday, October 9, 2010

Export DataGridView Data in Excel C#

object misValue = System.Reflection.Missing.Value;


Excel.Application xlapp;

Excel.Workbook xlworkbook;

Excel.Worksheet xlsheets;

xlapp = new Excel.ApplicationClass();

xlworkbook = xlapp.Workbooks.Add("");

xlsheets = (Excel.Worksheet)xlworkbook.Sheets.get_Item(1);

int i = 0; int j = 0;

int x = 2;

for (i = 0; i <= dataGridView1.Rows.Count - 1; i++)

{

for (j = 0; j <= dataGridView1.Columns.Count-1; j++)

{

DataGridViewCell dgvcell = dataGridView1[j, i];

if (i == 0)

{

string ss = "B"+(j+1);

xlsheets.Cells[i + 1, j + 1] = dataGridView1.Columns[dgvcell.ColumnIndex].Name;

//xlsheets.get_Range("A1", "C1").Interior.ColorIndex = 37;

xlsheets.get_Range("A1", "C1").Interior.Color= System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Blue);

xlsheets.get_Range("A1", "C1").Interior.Pattern = XlPattern.xlPatternSolid;

xlsheets.get_Range("A1", "C1").Font.Bold = true;

xlsheets.get_Range("A1", "C1").HorizontalAlignment = Excel.XlVAlign.xlVAlignCenter;

//xlsheets.get_Range("A1", "C1").VerticalAlignment = Excel.XlVAlign.xlVAlignCenter;



}



xlsheets.Cells[i+2, j+1] = dgvcell.Value;





}

string rng1 = "A" + (i + 2);

string rng2 = "C" + (i + 2);



xlsheets.get_Range(rng1, rng2).Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Pink);

xlsheets.get_Range(rng1, rng2).Interior.Pattern = XlPattern.xlPatternSolid;



}

xlworkbook.SaveAs("d:\\MM.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, misValue, misValue, misValue, misValue, misValue);
 
 
 
private void releaseObject(object obj)


{

try

{

System.Runtime.InteropServices.Marshal.ReleaseComObject(obj);

obj = null;

}

catch (Exception ex)

{

obj = null;

MessageBox.Show("Exception Occured while releasing object " + ex.ToString());

}

finally

{

GC.Collect();

}

}
 

Monday, October 4, 2010

Set Text of Button in DataGridView

 DataGridViewButtonColumn colbtnApproved = new DataGridViewButtonColumn();

colbtnApproved .HeaderText = "Approve";


colbtnApproved .Text = "Approve";

colbtnApproved .UseColumnTextForButtonValue = true;
 
now text is visible on button

Wednesday, September 29, 2010

Select Case Sensitive String from Table in Sql Server

I have a Table in which record is following



Name               Age

nagendra           23

NAGENDRA   24



SELECT  Name,Age FROM Customers

WHERE  Name COLLATE Latin1_General_CS_AS = 'nagendra'



Output:

nagendra

Friday, July 23, 2010

Find all Primary key in Table/Database

SELECT t.table_schema AS PrimarySchemaName ,
t.TABLE_NAME AS PrimaryKeyTable,
tc.CONSTRAINT_NAME AS PrimaryKey,
COALESCE(tc2.constraint_schema,'N/A') AS ForeignSchemaName,
COALESCE(rc1.CONSTRAINT_NAME,'N/A') AS ForeignKey ,
COALESCE(tc2.TABLE_NAME,'N/A') AS ForeignKeyTable
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
INNER JOIN INFORMATION_SCHEMA.TABLES t ON tc.TABLE_NAME = t.TABLE_NAME
LEFT JOIN INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS rc1 ON tc.CONSTRAINT_NAME =rc1.UNIQUE_CONSTRAINT_NAME
LEFT JOIN INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc2 ON tc2.CONSTRAINT_NAME =rc1.CONSTRAINT_NAME
WHERE TC.CONSTRAINT_TYPE ='PRIMARY KEY'
ORDER BY tc.TABLE_NAME,tc.CONSTRAINT_NAME,rc1.CONSTRAINT_NAME

It will Return all the Primary Key of every Table with their table name

Input only Number in TextBox in C#

IsDigit


IsHighSurrogate

IsLetter

IsLetterOrDigit

IsLower

IsLowSurrogate

IsNumber

IsPunctuation

IsSeparator

IsSurrogate

IsSurrogatePair

IsSymbol

IsUpper

IsWhiteSpace



private void textBox1_KeyPress(object sender, KeyPressEventArgs e)

{

if (Char.IsDigit(e.KeyChar) )

{ MessageBox.Show("Bad Input");

e.Handled = true; ;

}}

// It will Input Only Digit in TextBox
            if ((e.KeyChar < '0') || (e.KeyChar > '9'))
            {
                e.Handled = true;
            }
          
            // It will Input Only Alphabet and Digit in TextBox nothing else
            if (!Char.IsLetterOrDigit(e.KeyChar))
            {
                MessageBox.Show("Bad Input");
                e.Handled = true; ;
            }

Mat Table Angular

Mat Table in Angular Mat table is used to display data . its a material designed styled data-table . For using Material Table MatTableMo...