Monday, December 7, 2009

Printing from ListView to RAW Printer

private void PrintBill()
{
//Printing
//Locus.PMIS.Db.PMIS objPMIS = new Locus.PMIS.Db.PMIS();
string s = "";
//s += "\n"+Spaces(29," ")+objPMIS.GetServerDate().ToShortDateString()+"\n";
s += "Bill Number: "+txtBillNo.Text + "\n";
if (txtPatientName.Text.Trim().Length > 0)
s += "Issued To: " + txtPatientName.Text+"\n";
s += Spaces(39, "=");
s += "\n";
s += "Particulars" + Spaces(7, " ") + "|Qty" + "|Rate" + Spaces(3, " ") + "|Amount";
s += "\n";
s += Spaces(39, "-") + "\n";

//End of printing Master
int len = 0;
for (int i = 0; i < lvBillDetail.Items.Count; i++)
{
//For printing Details

len = lvBillDetail.Items[i].SubItems[2].Text.Length;
if (len < 18)
{
s += lvBillDetail.Items[i].SubItems[2].Text; //ItemName
s += Spaces(18 - len, " ");
}
else
{
s += lvBillDetail.Items[i].SubItems[2].Text.Substring(0, 16) + "-";
s += "\n";
s += lvBillDetail.Items[i].SubItems[2].Text.Substring(16, len-16);
s += Spaces((18 - lvBillDetail.Items[i].SubItems[2].Text.Substring(16, len-16).Length), " ");
}
s += "|";

len = lvBillDetail.Items[i].SubItems[4].Text.Trim().Length;
s += lvBillDetail.Items[i].SubItems[4].Text; //Quantity
s += Spaces(3 - len, " ");
s += "|";

len = lvBillDetail.Items[i].SubItems[3].Text.Trim().Length;
s += lvBillDetail.Items[i].SubItems[3].Text; //Rate
s += Spaces(7 - len, " ");
s += "|";

s += lvBillDetail.Items[i].SubItems[5].Text + "\n"; //Amount
}
s += Spaces(39, "-") + "\n";
s += Spaces(22," ")+"SubTotal|" + txtSubtotal.Text + "\n";
s += Spaces(22," ")+"Discount|" + txtDiscount.Text+"\n";
s += Spaces(19," ")+"Grand Total|" + lblGrandTotal.Text+"\n";
s += "\n"+"Cashier: " + ((mdiPharmacyModule)this.MdiParent).objEmployee.EmpName.ToString();
s += "\n" + Spaces(15, "=") + "Thank you" + Spaces(15, "=");
s += "\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n";
s += "\n" + Spaces(13, " ") + "My Print from ListView";// +Spaces(48, "");

objPrinter.SendStringToPrinter(printDocument1.PrinterSettings.PrinterName, s);

objPMIS.Close();
objPMIS.Dispose();
}
}

Printing RAW data in .Net

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Runtime.InteropServices;

public class clsDipendrazPrintingEngine
{
// Structure and API declarions:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public class DOCINFOA
{
[MarshalAs(UnmanagedType.LPStr)]
public string pDocName;
[MarshalAs(UnmanagedType.LPStr)]
public string pOutputFile;
[MarshalAs(UnmanagedType.LPStr)]
public string pDataType;
}
[DllImport("winspool.Drv", EntryPoint = "OpenPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool OpenPrinter([MarshalAs(UnmanagedType.LPStr)] string szPrinter, out IntPtr hPrinter, IntPtr pd);

[DllImport("winspool.Drv", EntryPoint = "ClosePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool ClosePrinter(IntPtr hPrinter);

[DllImport("winspool.Drv", EntryPoint = "StartDocPrinterA", SetLastError = true, CharSet = CharSet.Ansi, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartDocPrinter(IntPtr hPrinter, Int32 level, [In, MarshalAs(UnmanagedType.LPStruct)] DOCINFOA di);

[DllImport("winspool.Drv", EntryPoint = "EndDocPrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndDocPrinter(IntPtr hPrinter);

[DllImport("winspool.Drv", EntryPoint = "StartPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool StartPagePrinter(IntPtr hPrinter);

[DllImport("winspool.Drv", EntryPoint = "EndPagePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool EndPagePrinter(IntPtr hPrinter);

[DllImport("winspool.Drv", EntryPoint = "WritePrinter", SetLastError = true, ExactSpelling = true, CallingConvention = CallingConvention.StdCall)]
public static extern bool WritePrinter(IntPtr hPrinter, IntPtr pBytes, Int32 dwCount, out Int32 dwWritten);

// SendBytesToPrinter()
// When the function is given a printer name and an unmanaged array
// of bytes, the function sends those bytes to the print queue.
// Returns true on success, false on failure.
public bool SendBytesToPrinter(string szPrinterName, IntPtr pBytes, Int32 dwCount)
{
Int32 dwError = 0, dwWritten = 0;
IntPtr hPrinter = new IntPtr(0);
DOCINFOA di = new DOCINFOA();
bool bSuccess = false; // Assume failure unless you specifically succeed.

di.pDocName = "Dipendraz Printing Code";
di.pDataType = "RAW";

// Open the printer.
if (OpenPrinter(szPrinterName.Normalize(), out hPrinter, IntPtr.Zero))
{
// Start a document.
if (StartDocPrinter(hPrinter, 1, di))
{
// Start a page.
if (StartPagePrinter(hPrinter))
{
// Write your bytes.
bSuccess = WritePrinter(hPrinter, pBytes, dwCount, out dwWritten);
EndPagePrinter(hPrinter);
}
EndDocPrinter(hPrinter);
}
ClosePrinter(hPrinter);
}
// If you did not succeed, GetLastError may give more information
// about why not.
if (bSuccess == false)
{
dwError = Marshal.GetLastWin32Error();
}
return bSuccess;
}

public bool SendFileToPrinter(string szPrinterName, string szFileName)
{
// Open the file.
FileStream fs = new FileStream(szFileName, FileMode.Open);
// Create a BinaryReader on the file.
BinaryReader br = new BinaryReader(fs);
// Dim an array of bytes big enough to hold the file's contents.
Byte[] bytes = new Byte[fs.Length];
bool bSuccess = false;
// Your unmanaged pointer.
IntPtr pUnmanagedBytes = new IntPtr(0);
int nLength;

nLength = Convert.ToInt32(fs.Length);
// Read the contents of the file into the array.
bytes = br.ReadBytes(nLength);
// Allocate some unmanaged memory for those bytes.
pUnmanagedBytes = Marshal.AllocCoTaskMem(nLength);
// Copy the managed byte array into the unmanaged array.
Marshal.Copy(bytes, 0, pUnmanagedBytes, nLength);
// Send the unmanaged bytes to the printer.
bSuccess = SendBytesToPrinter(szPrinterName, pUnmanagedBytes, nLength);
// Free the unmanaged memory that you allocated earlier.
Marshal.FreeCoTaskMem(pUnmanagedBytes);
return bSuccess;
}

public bool SendStringToPrinter(string szPrinterName, string szString)
{
IntPtr pBytes;
Int32 dwCount;
// How many characters are in the string?
dwCount = szString.Length;
// Assume that the printer is expecting ANSI text, and then convert
// the string to ANSI text.
pBytes = Marshal.StringToCoTaskMemAnsi(szString);
// Send the converted ANSI string to the printer.
SendBytesToPrinter(szPrinterName, pBytes, dwCount);
Marshal.FreeCoTaskMem(pBytes);
return true;
}
}

Thursday, November 26, 2009

SQL Helper

using System;
using System.Data;
using System.Data.SqlClient;

///
/// Summary description for SqlHelper.
///

public class SQLHelper
{
SqlConnection sqlConn;
SqlCommand sqlCmd;
//SqlDataReader sqlDr;
SqlDataAdapter sqlDa;


public SQLHelper(string strConn)
{
//
// TODO: Add constructor logic here
//
sqlConn = new SqlConnection(strConn);
sqlConn.Open();
sqlCmd= new SqlCommand();
sqlCmd.Connection = sqlConn;
sqlDa= new SqlDataAdapter(sqlCmd);

}
public SQLHelper(string strConn, string SqlText)
{
if (strConn == "")
{
strConn = "server=dipendraz;database=MIS;user id=sa;password=sa123";
}
sqlConn = new SqlConnection(strConn);
sqlConn.Open();
sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlConn;
sqlDa = new SqlDataAdapter(sqlCmd);
sqlCmd.CommandText = SqlText;
}
public SQLHelper(int Number)
{
if (Number == 0)
{
string strConn = "server=dipendraz;database=MIS;user id=sa;password=sa123";
sqlConn = new SqlConnection(strConn);
sqlConn.Open();
sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlConn;
sqlDa = new SqlDataAdapter(sqlCmd);
}
else
{
string strConn = "server=dipendraz;database=MIS;user id=sa;password=sa123";
sqlConn = new SqlConnection(strConn);
sqlConn.Open();
sqlCmd = new SqlCommand();
sqlCmd.Connection = sqlConn;
sqlDa = new SqlDataAdapter(sqlCmd);
}
}

public SQLHelper(SqlConnection sqlCon)
{
//
// TODO: Add constructor logic here
//
sqlConn = sqlCon;
if(sqlConn.State != ConnectionState.Open)
sqlConn.Open();
sqlCmd= new SqlCommand();
sqlCmd.Connection = sqlCon;
sqlDa= new SqlDataAdapter(sqlCmd);
}

public DataTable getDataTable(bool IsProcedure)
{
DataTable dt = new DataTable();
if(IsProcedure)
sqlCmd.CommandType= CommandType.StoredProcedure;
else
sqlCmd.CommandType = CommandType.Text;

sqlDa.Fill(dt);
return dt;
}
public DataTable getDataTable()
{
DataTable dt = new DataTable();
sqlCmd.CommandType = CommandType.Text;

sqlDa.Fill(dt);
return dt;
}


public DataTable FillDataSet(DataSet ds,string table,bool IsProcedure)
{
DataTable dt = new DataTable();
if (IsProcedure)
{
sqlCmd.CommandType = CommandType.StoredProcedure;
sqlDa.Fill(ds, table);

}
else
{
sqlCmd.CommandType = CommandType.Text;
sqlDa.Fill(ds, table);
}
return dt;
}

public SqlDataReader getSqlDataReader(bool IsProcedure)
{
if(IsProcedure)
sqlCmd.CommandType= CommandType.StoredProcedure;
else
sqlCmd.CommandType = CommandType.Text;

return sqlCmd.ExecuteReader();
}

public string SqlText
{
set{sqlCmd.CommandText =value;}
get{return sqlCmd.CommandText;}
}

public void ExecuteSql(bool IsProcedure)
{

sqlCmd.Connection = sqlConn;
if(IsProcedure)
{
sqlCmd.CommandType = CommandType.StoredProcedure;
}
else
{
sqlCmd.CommandType = CommandType.Text ;
}
sqlCmd.ExecuteNonQuery();
}

public object ExecuteScalar(bool IsProcedure)
{

sqlCmd.Connection = sqlConn;
if(IsProcedure)
{
sqlCmd.CommandType = CommandType.StoredProcedure;
}
else
{
sqlCmd.CommandType = CommandType.Text ;
}
return sqlCmd.ExecuteScalar();
}

public object GetIdentityValue(string tablName)
{
sqlCmd.CommandText = "SELECT IDENT_CURRENT('" + tablName + "')";
return sqlCmd.ExecuteScalar();
}

public SqlParameter AddParameter(string strParam,object value)
{
return sqlCmd.Parameters.AddWithValue("@"+strParam,value);
}
public void SetValueForParameter(string strParam,object value)
{
sqlCmd.Parameters["@"+strParam].Value= value;
}
public void ClearAllParameters()
{
sqlCmd.Parameters.Clear();
}
public void Close()
{
sqlCmd.Dispose();
sqlDa.Dispose();
//sqlConn.Close();
}

public void Dispose()
{
sqlCmd.Dispose();
sqlDa.Dispose();
sqlConn.Close();
sqlConn.Dispose();

}

}