ali raza

C#, .Net ,Asp.net and my thoughts

Category Archives: c#

whats new in asp.net 4.0

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

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

How to get Month name from current date using c#

Hi i found two methods to do so.

Using Globliziation

System.Globalization.DateTimeFormatInfo d = new System.Globalization.DateTimeFormatInfo();
string month = d.MonthNames[DateTime.Now.Month-1];

Using new date object as follow

string MonthName = GetMonthName(DateTime.Now.Month);
static string GetMonthName(int monthNum)
{
return GetMonthName(monthNum, false);
}
static string GetMonthName(int monthNum, bool abbreviate)
{
if (monthNum < 1 || monthNum > 12)
throw new ArgumentOutOfRangeException(“monthNum”);
DateTime date = new DateTime(1, monthNum, 1);
if (abbreviate)
return date.ToString(“MMM”);
else
return date.ToString(“MMMM”);
}

Chears

Export GridView from aspx page to Pdf using iTextSharp in asp.net

how to get oracle sequence numbers in .net

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

How to resize image in .net

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

how to insert dateTime in Oracle database using c#

hi all, i have facing problem while Executing Insert query with dataTime on Oracle database. This query is generated by string manuplation using C# at business layer . i fix the date time as in Query

"TO_DATE('" + ProdMonitor.TRANSACTIONDATE.Month+
"/" + ProdMonitor.TRANSACTIONDATE.Day +
"/" + ProdMonitor.TRANSACTIONDATE.Year+
" " + ProdMonitor.TRANSACTIONDATE.ToLongTimeString()+
"','mm/dd/yyyy HH:MI:SS PM'),"

The Whole Query Will be like

 

Insert Into JOBS (JOB_ID , EXECUTION_TIME,CREATION_TIME,SERIAL_NO,STATUS) values(JOB_ID_SEQ.NEXTVAL,TO_DATE('9/29/2008 11:00:00 PM','mm/dd/yyyy HH:MI:SS PM'),TO_DATE('9/26/2008 12:59:59 PM','mm/dd/yyyy HH:MI:SS PM'),'','1')

Chears

how to get server.mappath in c# class

i face a problem that server.mappath did not available in c# class at my utillity framework at app code folder of one of my asp.net application.  While it is available at asp.net page code behind.  Because ASP.NET pages contain a default reference to the System.Web namespace (which contains the
HttpContext class), you can reference the members of HttpContext on an .aspx
page without the fully qualified class reference to HttpContext.

For using Server.MapPath at C# class i used it following

System.Web.HttpContext.Current.Server.MapPath(logpath);

how to change the color of datagridviews row at difference in two dates

use the following Code in application

//This method returns the no of days between today and date comes from database
private int GetDateDifference(DateTime _StartDateTime )
{
DateTime startDate = _StartDateTime;
// End date
DateTime endDate = DateTime.Now;
// Time span
TimeSpan diffDate = endDate.Subtract ( startDate );

return diffDate.Days;
}
//Gridview Row Data Bound Event
protected void grdDetail_RowDataBound(object sender, GridViewRowEventArgs e)
{

if (e.Row.RowType != DataControlRowType.Header && e.Row.RowType != DataControlRowType.Footer )
{

Label _lbl = (Label)e.Row.FindControl("lblDate");

if (_lbl != null)
{
_lbl.Text = DataBinder.Eval(e.Row.DataItem, "TRANSACTION DATE").ToString();
int intDays = GetDateDifference(Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "TRANSACTION DATE")));
if (intDays > 2)
{
// Setting the color of row
e.Row.BackColor = System.Drawing.Color.Red;
}
}
}

Enjoy

how to clear controls in asp.net c#

Use following code in you page. At Calling Statement send reference of page “this”. It Clears all text boxes and set all checkboxes on page to false

private void ClearControls(Control parent)
{
foreach (Control _ChildControl in parent.Controls)
{
if ((_ChildControl.Controls.Count > 0))
{
ClearControls(_ChildControl);
}
else
{
if (_ChildControl is TextBox)
{
((TextBox)_ChildControl).Text = string.Empty;
}
else
if (_ChildControl is CheckBox)
{
((CheckBox)_ChildControl).Checked = false;
}

}
}
}