Thursday, September 27, 2012

Creating and renaming Logs when the size exceeds 400 MB


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;
namespace readtxt
{
    public class CreateLog
    {
        string fileName = "";
        public string CreateLogFile()
        {
            return getsizeofFile();

        }

        public string getsizeofFile()
        {
            fileName = @"C:\Program Files\Virtual Payment Solutions\TSS 1.5\Retail.log";
            FileInfo f = new FileInfo(fileName);
            long s1 = f.Length;
            string size = GetFileSize(s1);
            createNewLogFile(size, fileName);
            return size;
        }

        public void createNewLogFile(string size, string fileLoc)
        {
            FileStream fs = null;
            if (size.Contains("MB"))
            {
                Regex re = new Regex(@"\d+");
                Match m = re.Match(size);
                if (Convert.ToInt64(m.Value) >= 400)
                {
                    string fileLocCopy = @"C:\Program Files\Virtual Payment Solutions\TSS 1.5\Retail_" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".log";
                    if (File.Exists(fileLoc))
                    {
                        File.Move(fileLoc, fileLocCopy);
                        if (File.Exists(fileLoc))
                        {
                            File.Delete(fileLoc);
                            using (fs = File.Create(fileLoc))
                            {

                            }
                        }
                        else if (!File.Exists(fileLoc))
                        {
                            using (fs = File.Create(fileLoc))
                            {

                            }
                        }
                    }
                    else if (!File.Exists(fileLoc))
                    {
                        using (fs = File.Create(fileLoc))
                        {

                        }
                    }
                }

            }

        }

        public static string GetFileSize(long Bytes)
        {
            if (Bytes >= 1073741824)
            {
                Decimal size = Decimal.Divide(Bytes, 1073741824);
                return String.Format("{0:##.##} GB", size);
            }
            else if (Bytes >= 1048576)
            {
                Decimal size = Decimal.Divide(Bytes, 1048576);
                return String.Format("{0:##.##} MB", size);
            }
            else if (Bytes >= 1024)
            {
                Decimal size = Decimal.Divide(Bytes, 1024);
                return String.Format("{0:##.##} KB", size);
            }
            else if (Bytes > 0 & Bytes < 1024)
            {
                Decimal size = Bytes;
                return String.Format("{0:##.##} Bytes", size);
            }
            else
            {
                return "0 Bytes";
            }
        }
    }

}

Tuesday, September 18, 2012

USING single quote in SQL insert statement

INSERT INTO [CLITransaction](CLITransactionTypeID,ReceivedDateTime,ContentDetail04) VALUES(7,'10-10-2012','Greg''s'+' Company') 

Wednesday, September 12, 2012

Enable/Disable Proxy in IE through C#

// ProxyEnable == 0 means the proxy is on
// ProxyEnable == 1 means the proxy is off




string key = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings";
                string serverName = ConfigurationManager.AppSettings["proxyAddress"];
                int port = Convert.ToInt32(ConfigurationManager.AppSettings["proxyPort"]);
                string proxyy = serverName + ":" + port;

                RegistryKey RegKey = Registry.CurrentUser.OpenSubKey(key, true);
                RegKey.SetValue("ProxyServer", proxyy);
                RegKey.SetValue("ProxyEnable", 1);

                proxy = new WebProxy(proxyy, true);;
                proxy.Credentials = CredentialCache.DefaultCredentials;

                proxy.BypassProxyOnLocal = true;

Tuesday, September 11, 2012

Passing Subqueries to Stored procedure


ALTER PROC [dbo].[sp_GetPinnedSalesDetail]
    -- Add the parameters for the stored procedure here
    @orderNumber varchar(50) = 'VPSORD8013',
 @merchantAccount varchar(50),
    @userlogin  varchar(50)
AS
BEGIN
DECLARE @ReprintCount int
DECLARE @UserName varchar(50)

    SET NOCOUNT ON;
SET @ReprintCount=(SELECT COUNT(*) AS Expr1 FROM  WebAdminDBTest.dbo.AuditTable WHERE
 (WebAdminDBTest.dbo.AuditTable.masterAccount = @merchantAccount) AND (WebAdminDBTest.dbo.AuditTable.userName = @userlogin)
AND WebAdminDBTest.dbo.AuditTable.description = @orderNumber)


SET @UserName=(SELECT WebAdminDBTest.dbo.WebUserSales.subuser_name FROM  WebAdminDBTest.dbo.WebUserSales WHERE (WebAdminDBTest.dbo.WebUserSales.voucher_orderno = @orderNumber))


SELECT     @UserName AS username,@ReprintCount AS reprintcount,dbo.TSSMerchant.MerchantAccount, dbo.TSSMerchant.CompanyName,
dbo.TMPVoucherOrder.OrderNo,
 dbo.TMPVendor.CompanyName AS Vendor,
 TMPVendor.Description1 AS Instruction1, TMPVendor.Description2 AS Instruction2,
TMPVendor.Description3 AS Instruction3, TMPVendor.Description4 AS Instruction4, TMPVoucher.ExpiryDate,
dbo.TMPVoucherType.Description as VoucherType,
                      dbo.TMPVoucherType.VoucherCode, dbo.TMPVoucherOrderDetail.TotalSellingPrice,TMPVoucherType.FaceValuePrice,TMPVoucherType.AirtimeWindow, dbo.TMPVoucherOrderDetail.Quantity, dbo.TMPVoucher.S1,
                      dbo.TMPVoucher.S2, dbo.TMPVoucher.S3,dbo.TMPVoucher.RechargePin, dbo.TMPServerOrder.ImportDate, dbo.TMPVoucherOrder.OrderDate
FROM         dbo.TMPVoucherType INNER JOIN
                      dbo.TMPVoucher ON dbo.TMPVoucherType.TMPVoucherTypeID = dbo.TMPVoucher.TMPVoucherTypeID INNER JOIN
                      dbo.TMPVoucherOrder ON dbo.TMPVoucher.TMPVoucherOrderID = dbo.TMPVoucherOrder.TMPVoucherOrderID INNER JOIN
                      dbo.TMPServerOrder ON dbo.TMPVoucher.TMPServerOrderID = dbo.TMPServerOrder.TMPServerOrderID INNER JOIN
                      dbo.TSSMerchant ON dbo.TMPVoucher.TSSMerchantID = dbo.TSSMerchant.TSSMerchantID INNER JOIN
                      dbo.TMPVendor ON dbo.TMPVoucherType.TMPVendorID = dbo.TMPVendor.TMPVendorID INNER JOIN
                      dbo.TMPVoucherOrderDetail ON dbo.TMPVoucherType.TMPVoucherTypeID = dbo.TMPVoucherOrderDetail.TMPVoucherTypeID AND
                      dbo.TMPVoucherOrder.TMPVoucherOrderID = dbo.TMPVoucherOrderDetail.TMPVoucherOrderID
WHERE     (dbo.TMPVoucherOrder.OrderNo = @orderNumber);
END

Tuesday, September 4, 2012

Reading XML Document using XML Reader C#

Reading XML Document using XML Reader C#



StringBuilder myresult = new StringBuilder(); Dictionary myHeader = new Dictionary();
string response1 =










XmlReader xRead = XmlReader.Create(new System.IO.StringReader(response1));
XmlReader result = xRead;
public string processXMLTextReader(XmlReader xRead, String element)
{
                       value = "";
                      try
                           {
                               while (xRead.Read())
                                 {
                                    if (xRead.NodeType == XmlNodeType.Element)
                                           {
                                               if (xRead.Name == element)
                                              {
                                                  value = xRead.GetAttribute("AccountNo").ToString();                                             
                                                  break;
                                              }
                                           }
                                        }
                                 }
                                catch (Exception e)
                                {
                               value = e.Message;

                               }
  return value;
 }


Sample 2:

public Dictionary(int,string) processReader(XmlReader xRead, String element)
{
                   xRead.ReadToFollowing(element);
                   xRead.MoveToFirstAttribute();
                  Dictionary(int,string) mydict = new Dictionary();
              for (int i = 0; i < xRead.AttributeCount; i++)
                {
                        //string genre = .ToString(); 
                         mydict.Add(i, xRead.GetAttribute(i)); 
                   }
                 return mydict;
}

reading Response:


//the below method call returns result in XML format string response = ConfirmMuncipality(mRequest);


XmlReader xReadx = XmlReader.Create(new System.IO.StringReader(response(OR)(Response1)));
 XmlReader resultb = xReadb;
  myHeader = new Dictionary();
//pass the name of the XML element you want to get attributes for
//for EG: ns0:ConfirmPaymentItems
  myHeader = syntelTest.processReader(resultx, "ns0:ConfirmPaymentItems");
  if (myHeader.Count == 1)
            {
string AccountNo="";
                AccountNo = myHeader[0];
             
            }





Reading XML file


http://forum.codecall.net/topic/58239-c-tutorial-reading-and-writing-xml-files/
http://csharptutorial.blogspot.com/2006/10/reading-xml-fast.html
using System;
using System.Collections.Generic;
using System.Xml;
namespace TempCSharp
{
class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
doc.Load("test.xml");
XmlNodeList customers = doc.SelectNodes("//customer");
foreach (XmlNode customer in customers)
{
Console.WriteLine("Age = {0}, Gender = {1}",
customer.Attributes["age"].Value, customer.Attributes["gender"].Value);
}
}
}
}

Monday, September 3, 2012

Round double in two decimal places in C#


You can try any of the following ways:

inputValue = Math.Round(inputValue, 2);

decimalVar.ToString ("#.##");
ToString("0.00"); //2dp Number
ToString("n2"); // 2dp Number
ToString("c2"); // 2dp currency


http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx