c#

How to get Current Fiscal year starting and Ending Date

by Ali Raza Zaidi on March 1, 2012

For starting Date

int month = DateTime.Now.Month;
int year = DateTime.Now.Year;

// if month is october or later, the FY started 10-1 of this year
// else it started 10-1 of last year
return month > 9 ? new DateTime( year, 10, 1 ) : new DateTime( year - 1, 10, 1 );

For Ending Date

// get the current month and year
int month = DateTime.Now.Month;
int year = DateTime.Now.Year;

// if month is october or later, the FY ends 9/30 next year
// else it ends 9-30 of this year
return month > 9 ? new DateTime( year + 1, 9, 30 ) : new DateTime( year, 9, 30 );

{ Comments on this entry are closed }

How to get site visitor Api in asp.net

by Ali Raza Zaidi on February 25, 2012

How to get IP Host Address of remote Client

// To get IP of the client meachine
// There is two methods to obtain IP address

// Method 1
//     Gets the IP host address of the remote client.
// Returns:
//     The IP address of the remote client.
string ipMethod1 = Request.UserHostAddress; // HttpContext.Current.Request.UserHostAddress;

//Methord 2
//Request.ServerVariables is a Name Value Collection
//     Gets a collection of Web server variables.
// Returns:
//     A System.Collections.Specialized.NameValueCollection of server variables.
string ipMethod2 = Request.ServerVariables["REMOTE_ADDR"]; //HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
Response.Write(ipAdddress);

{ Comments on this entry are closed }

You know the word “@@IDENTITY” it really has magic, It returns me the latest inserted rows identity in my access database using oldebcommand. For this purpose i have to execute command object two times first for insert query and second time by execute secular for getting value back

 

public void InsertReviews(string _Nick, string _Name, string _EmailAddress, string _RealStateTitle, string _Address, string _City, string _ZipCode, string _Country, string _Comments, string _FromIP, string _Status, string _Langi, string _Lit)
{
string ConnString = Util.GetConnString();
string SqlString = "Insert Into ClientInfo (Nick,Name,EmailAddress,RealStateTitle,Address,City,ZipCode,Country,Comments,FromIP,Status,Langi,Lit) Values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
  string SqlString2 = "Select @@Identity";
using (OleDbConnection conn = new OleDbConnection(ConnString))
{
using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
{
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("Nick", _Nick);
cmd.Parameters.AddWithValue("Name", _Name);
cmd.Parameters.AddWithValue("EmailAddress", _EmailAddress);
cmd.Parameters.AddWithValue("RealStateTitle", _RealStateTitle);
cmd.Parameters.AddWithValue("Address", _Address);
cmd.Parameters.AddWithValue("City", _City);
cmd.Parameters.AddWithValue("ZipCode", _ZipCode);
cmd.Parameters.AddWithValue("Country", _Country);
cmd.Parameters.AddWithValue("Comments", _Comments);
cmd.Parameters.AddWithValue("FromIP", _FromIP);
cmd.Parameters.AddWithValue("Status", _Status);
cmd.Parameters.AddWithValue("Langi", _Langi);
cmd.Parameters.AddWithValue("Lit", _Lit);
conn.Open();
cmd.ExecuteNonQuery();
cmd.CommandText = SqlString2;
  int _Count = (int) cmd.ExecuteScalar();
}
}

}

Its working for me

{ Comments on this entry are closed }

Simple Data Access Class for Access database

by Ali Raza Zaidi on February 19, 2012

After little time i have to wrote a simple data access layer for MS Access database,

For Connection string I wrote a simple Util Class as

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Configuration; 

/// <summary>
/// Summary description for Util
/// </summary>
public static  class Util
{

    public static string GetConnString()
    {
        return WebConfigurationManager.ConnectionStrings["myConnStr"].ConnectionString;
    }
}

 

Then i Wrote another class where Insert , update ,delete and simple select method placed as

Insert method

 public void InsertReviews(string _Nick, string _Name, string _EmailAddress, string _RealStateTitle, string _Address, string _City, string _ZipCode, string _Country, string _Comments, string _FromIP, string _Status, string _Langi, string _Lit)
    {
        string ConnString = Util.GetConnString();
        string SqlString = "Insert Into ClientInfo (Nick,Name,EmailAddress,RealStateTitle,Address,City,ZipCode,Country,Comments,FromIP,Status,Langi,Lit) Values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
        using (OleDbConnection conn = new OleDbConnection(ConnString))
        {
            using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("Nick", _Nick);
                cmd.Parameters.AddWithValue("Name", _Name);
                cmd.Parameters.AddWithValue("EmailAddress", _EmailAddress);
                cmd.Parameters.AddWithValue("RealStateTitle", _RealStateTitle);
                cmd.Parameters.AddWithValue("Address", _Address);
                cmd.Parameters.AddWithValue("City", _City);
                cmd.Parameters.AddWithValue("ZipCode", _ZipCode);
                cmd.Parameters.AddWithValue("Country", _Country);
                cmd.Parameters.AddWithValue("Comments", _Comments);
                cmd.Parameters.AddWithValue("FromIP", _FromIP);
                cmd.Parameters.AddWithValue("Status", _Status);
                cmd.Parameters.AddWithValue("Langi", _Langi);
                cmd.Parameters.AddWithValue("Lit", _Lit);
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }

    }

Update Method

  public void UpdateReviews(int _ID,string _Nick, string _Name, string _EmailAddress, string _RealStateTitle, string _Address, string _City, string _ZipCode, string _Country, string _Comments, string _FromIP, string _Status, string _Langi, string _Lit)
    {
        string ConnString = Util.GetConnString();
        string SqlString = "Update ClientInfo Set Nick=?,Name=?,EmailAddress=?,RealStateTitle=?,Address=?,City=?,ZipCode=?,Country=?,Comments=?,FromIP=?,Status=?,Langi=?,Lit=? where IDs=?";
        using (OleDbConnection conn = new OleDbConnection(ConnString))
        {
            using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("Nick", _Nick);
                cmd.Parameters.AddWithValue("Name", _Name);
                cmd.Parameters.AddWithValue("EmailAddress", _EmailAddress);
                cmd.Parameters.AddWithValue("RealStateTitle", _RealStateTitle);
                cmd.Parameters.AddWithValue("Address", _Address);
                cmd.Parameters.AddWithValue("City", _City);
                cmd.Parameters.AddWithValue("ZipCode", _ZipCode);
                cmd.Parameters.AddWithValue("Country", _Country);
                cmd.Parameters.AddWithValue("Comments", _Comments);
                cmd.Parameters.AddWithValue("FromIP", _FromIP);
                cmd.Parameters.AddWithValue("Status", _Status);
                cmd.Parameters.AddWithValue("Langi", _Langi);
                cmd.Parameters.AddWithValue("Lit", _Lit);
                cmd.Parameters.AddWithValue("IDs", _ID);
               conn.Open();
                cmd.ExecuteNonQuery();
            }
        }
    }

Delete Method

 public void DelteReviews(int _ID)
    {
        string ConnString = Util.GetConnString();
        string SqlString = "Delete * From ClientInfo where IDs=?";
        using (OleDbConnection conn = new OleDbConnection(ConnString))
        {
            using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("IDs", _ID);
                conn.Open();
                cmd.ExecuteNonQuery();
            }
        }

    }

Select Method as

 public DataSet SelectReviews(int _ID)
    {
        string ConnString = Util.GetConnString();
        string SqlString = "Select * From ClientInfo where IDs=?";
        DataSet _ds = new DataSet();
        using (OleDbConnection conn = new OleDbConnection(ConnString))
        {
            using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
            {
                cmd.CommandType = CommandType.Text;
                cmd.Parameters.AddWithValue("IDs", _ID);
                OleDbDataAdapter _Adp = new OleDbDataAdapter();
                _Adp.SelectCommand = cmd;
                _Adp.Fill(_ds);
                _Adp.Dispose();
                conn.Open();
            }

        }
        return _ds;
    }

Chears :)

{ Comments on this entry are closed }

how to write a tab delimited text file using C#.net

by Ali Raza Zaidi on February 2, 2012

Use following Code

StreamReader sr = new StreamReader(@"d:simpleTabDelimeterFile.txt");
                StreamWriter outputFile = new StreamWriter(@"d:out.txt");
                string strline = "";            

                string[] _values = null;
                while (!sr.EndOfStream)
                {
                    strline = sr.ReadLine();
                    _values = strline.Split('t');
                    if( _values.Length == 3 )
                    {
                        strline = _values[0] + 't' + _values[1] + 't' + _values[2];
                        outputFile.WriteLine(strline);
                    }
                }
                sr.Close();
                outputFile.Close();

{ Comments on this entry are closed }

whats new in asp.net 4.0

by Ali Raza Zaidi on June 26, 2009

read the following link

http://www.asp.net/learn/whitepapers/aspnet40/

{ Comments on this entry are closed }

How to get month date view between two dates in c#

by Ali Raza Zaidi on March 24, 2009

Scenario I got while developing some application. I required month and year view in drop down list between contract starting and ending date. Like “March 2009” “April 2009” until march 2011 so on.

I handled it as follow

DateTime _LoopDate = “1/1/2009”;
string month = string.Empty;
DateTime _EndDate = “1/1/2011”;

while (_LoopDate <= _EndDate)
{
System.Globalization.DateTimeFormatInfo d = new System.Globalization.DateTimeFormatInfo();
month = d.MonthNames[_LoopDate.Month - 1];

drpDate.Items.Add(new ListItem(month + ” ” + _LoopDate.Year.ToString(), _LoopDate.Month.ToString() + ” ” + _LoopDate.Year.ToString()));
//drpdate is dropdownlist
_LoopDate= _LoopDate.AddMonths(1);
}

{ Comments on this entry are closed }

very helpful link i found on internet http://csharpdotnetfreak.blogspot.com/2008/12/export-gridview-to-pdf-using-itextsharp.html

{ Comments on this entry are closed }

how to get oracle sequence numbers in .net

by Ali Raza Zaidi on October 24, 2008

hi the main Query to get sequence number is follow


select JOB_ID_SEQ.NEXTVAL from dual;

i get the required Sequence number value form following method

public int GetOracleSequenceValue(string _SequenceValue)
{
OdbcCommand _command = new OdbcCommand();
int _ItemValue = -1;
OdbcConnection _con = new OdbcConnection(System.Web.Configuration.WebConfigurationManager.ConnectionStrings["RCConnectionString"].ToString());
try
{
_con.Open();
_command.Connection = _con;
string _QueryString = "select "+ _SequenceValue +" from dual ";
_command.CommandText = _QueryString;
_ItemValue = Convert.ToInt32(_command.ExecuteScalar().ToString());
_con.Close();
}
catch (Exception ex)
{
Util.LogErrorToFile(ex, "MYExecuteNonQuery", "error");
}
finally
{
_con.Close();
}
return _ItemValue;
}

Chears

{ Comments on this entry are closed }

How to resize image in .net

by Ali Raza Zaidi on October 9, 2008

use follwing Code change image path at load event method for downloading Code

Imports System
Imports System.Drawing
Imports System.Drawing.Imaging
Imports System.Drawing.Drawing2D
Public Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim WorkingDirectory As String = "c:AliPicture"

'create a image object containing a verticel photograph
Dim imgPhotoVert As Image = Image.FromFile(WorkingDirectory + "pravs-j-the-first-step.jpg")
Dim imgPhoto As Image = Nothing
imgPhoto = FixedSize(imgPhotoVert, 100, 100)
imgPhoto.Save(WorkingDirectory + "imageresize_3.jpg", ImageFormat.Jpeg)
imgPhoto.Dispose()

End Sub

Private Shared Function FixedSize(ByVal imgPhoto As Image, ByVal Width As Integer, ByVal Height As Integer) As Image
Dim sourceWidth As Integer = imgPhoto.Width
Dim sourceHeight As Integer = imgPhoto.Height
Dim sourceX As Integer = 0
Dim sourceY As Integer = 0
Dim destX As Integer = 0
Dim destY As Integer = 0

Dim nPercent As Single = 0
Dim nPercentW As Single = 0
Dim nPercentH As Single = 0

nPercentW = (CSng(Width) / CSng(sourceWidth))
nPercentH = (CSng(Height) / CSng(sourceHeight))

'if we have to pad the height pad both the top and the bottom
'with the difference between the scaled height and the desired height
If nPercentH < nPercentW Then
nPercent = nPercentH
destX = CInt(((Width - (sourceWidth * nPercent)) / 2))
Else
nPercent = nPercentW
destY = CInt(((Height - (sourceHeight * nPercent)) / 2))
End If

Dim destWidth As Integer = CInt((sourceWidth * nPercent))
Dim destHeight As Integer = CInt((sourceHeight * nPercent))

Dim bmPhoto As New Bitmap(Width, Height, PixelFormat.Format24bppRgb)
bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution)

Dim grPhoto As Graphics = Graphics.FromImage(bmPhoto)
grPhoto.Clear(Color.Red)
grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic

grPhoto.DrawImage(imgPhoto, New Rectangle(destX, destY, destWidth, destHeight), New Rectangle(sourceX, sourceY, sourceWidth, sourceHeight), GraphicsUnit.Pixel)

grPhoto.Dispose()
Return bmPhoto
End Function

End Class

{ Comments on this entry are closed }