C# provides us with value types and Reference
Types. Value Types are stored on the stack and
Reference types are stored on the heap. The conversion of value
type to reference type is known as boxing and converting reference type
back to the value type is known as unboxing.
Value
Type
Value types are primitive
types that are mapped directly to the FCL. Like
Int32
maps to
System.Int32,double
maps to
System.double. All value types are stored on stack and all the
value types are derived from System.ValueType. All structures
and enumerated types that are derived from System.ValueType are
created on stack, hence known as ValueType.
Reference Types
Reference Types are different
from value types in such a way that memory is allocated to them from the
heap. All the classes are of reference type. C#
new operator
returns the memory address of the object.
Boxing
Example
int Val = 11;
object Obj = Val; // boxing
The first line we created a Value Type Val and assigned a value to Val. The second line , we created an instance of Object Obj and assign the value of Val to Obj. The above operation (object Obj = i ) we saw converting a value of a Value Type into a value of a corresponding Reference Type . These types of operation is called Boxing
UnBoxing Example
int ValUnBox = Obj; // unboxing
The first line (int ValUnBox = (int) Obj) shows extracts the Value Type from the Object . That is converting a
value of a Reference Type into a value of a Value Type. This operation is called UnBoxing
The following references need to be added
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SmoEnum
Microsoft.SqlServer.ConnectionInfo
ServerConnection oServerCon = new ServerConnection(txtServer.Text, txtUserName.Text, txtPassword.Text);
Server oServer = new Server(oServerCon);
lvDbOptions.Items.Clear();
String Value = String.Empty;
if (ddlOption.SelectedItem.ToString().ToLower() == "tables")
{
foreach (Table oTable in oServer.Databases[txtDatabase.Text].Tables)
{
if (oTable.IsSystemObject == false)
{
Value = oTable.ToString().Substring(oTable.ToString().IndexOf(".") + 2, (oTable.ToString().Length - 1) - (oTable.ToString().IndexOf(".") + 2));
lvDbOptions.Items.Add(new ListViewItem(Value));
}
}
}
else if (ddlOption.SelectedItem.ToString().ToLower() == "stored procedures")
{
foreach (StoredProcedure oProc in oServer.Databases[txtDatabase.Text].StoredProcedures)
{
if(oProc.IsSystemObject == false)
{
Value = oProc.ToString().Substring(oProc.ToString().IndexOf(".") + 2, (oProc.ToString().Length - 1) - (oProc.ToString().IndexOf(".") + 2));
lvDbOptions.Items.Add(new ListViewItem(Value));
}
}
}
else if (ddlOption.SelectedItem.ToString().ToLower() == "functions")
{
foreach (UserDefinedFunction oFunc in oServer.Databases[txtDatabase.Text].UserDefinedFunctions)
{
if (oFunc.IsSystemObject == false)
{
Value = oFunc.ToString().Substring(oFunc.ToString().IndexOf(".") + 2, (oFunc.ToString().Length - 1) - (oFunc.ToString().IndexOf(".") + 2));
lvDbOptions.Items.Add(new ListViewItem(Value));
}
}
}
The following references need to be added
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SmoEnum
Microsoft.SqlServer.ConnectionInfo
ServerConnection oCon = new ServerConnection(server, DBUserID, DBPassword);
Server oServer = new Server(oCon);
Restore oRestore = new Restore();
//sFileName - File Physical Location
if (File.Exists(sFileName))
{
oRestore.Database = DbName;
oRestore.Action = RestoreActionType.Database;
oRestore.Devices.AddDevice(sFileName, DeviceType.File);
oRestore.NoRecovery = false;
oRestore.ReplaceDatabase = true;
oRestore.PercentCompleteNotification = 10;
oRestore.PercentComplete +=
new PercentCompleteEventHandler(bkp_percentComplete);
oRestore.SqlRestore(oServer);
}
protected void bkp_percentComplete(object sender, PercentCompleteEventArgs e)
{
lblStatus.Text = e.Percent.ToString() + "% restored";
}
The following references need to be added
Microsoft.SqlServer.Smo
Microsoft.SqlServer.SmoEnum
Microsoft.SqlServer.ConnectionInfo
ServerConnection oCon = new ServerConnection(Server, DBUserID, DBPassword);
Server oServer = new Server(oCon);
Backup oBackUp = new Backup();
sFileName = BackupLocation + "\\" + DBName
+ "_" + System.DateTime.Now.ToString("dd_MM_yyyy hh_mm") + ".bak";
BackupDeviceItem oBackupDevice =
new BackupDeviceItem(sFileName, DeviceType.File);
oBackUp.Devices.Add(oBackupDevice);
//oBackUp.Devices.AddDevice(@"C:\DBbackup.bak", DeviceType.File);
//oBackUp.Devices.AddDevice(sFileName, DeviceType.File);
oBackUp.Database = DBName;
oBackUp.Action = BackupActionType.Database;
oBackUp.NoRecovery = false;
oBackUp.Initialize = true;
oBackUp.PercentCompleteNotification = 10;
oBackUp.PercentComplete += new PercentCompleteEventHandler(bkp_percentComplete);
oBackUp.SqlBackup(oServer);
protected void bkp_percentComplete(object sender, PercentCompleteEventArgs e)
{
lblStatus.Text = e.Percent.ToString() + "% backed up";
}
The following code can be called in the Page Load Event. the Code given here will be displayed from the (x,y) position (0,0). If the pie chart to be drawn from the top of the page, then the following code is OK. If it is need to be created in the center of the page or on the fly means, create a new aspx page and copy the code in the page load event and pass the aspx file as source for an image tag.
Add the following Namespaces
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
ArrayList aMonths;
ArrayList aProfits;
protected void Page_Load(object sender, EventArgs e)
{
LoadData();
DrawBarChrt("Bar Chart", aMonths, aProfits);
}
//Creating Array for Months and Profits - to populate values for the Report
private void LoadData()
{
aMonths = new ArrayList();
aProfits = new ArrayList();
aMonths.Add("January");
aMonths.Add("February");
aMonths.Add("March");
aMonths.Add("April");
aMonths.Add("May");
aMonths.Add("June");
aMonths.Add("July");
aMonths.Add("August");
aMonths.Add("September");
aMonths.Add("October");
aMonths.Add("November");
aMonths.Add("December");
aProfits.Add(240500);
aProfits.Add(220950);
aProfits.Add(283500);
aProfits.Add(340000);
aProfits.Add(325750);
aProfits.Add(123456);
aProfits.Add(235621);
aProfits.Add(345235);
aProfits.Add(290451);
aProfits.Add(152345);
aProfits.Add(653456);
aProfits.Add(785620);
}
private void DrawBarChrt(String sTitle, ArrayList aMonth, ArrayList aProfit)
{
const int iBarWidth = 30;
const int iBarGap = 20;
const int iMaxHeight = 400;
const int iHeightSpace = 25;
const int iLegendSpace = 30;
const int iTitleSpace = 50;
int iMaxWidth = (iBarWidth + iBarGap) * aMonth.Count + iBarGap;
int iMaxColHeight = 0;
int iTotalHeight = iMaxHeight + iLegendSpace + iTitleSpace;
Bitmap objBitmap = new Bitmap(iMaxWidth, iTotalHeight);
Graphics objGraphics = Graphics.FromImage(objBitmap);
objGraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, iMaxWidth, iTotalHeight);
objGraphics.FillRectangle(new SolidBrush(Color.Lavender), 0, 0, iMaxWidth, iMaxHeight);
// find the maximum value
foreach (int iValue in aProfit)
{
if (iValue > iMaxColHeight)
iMaxColHeight = iValue;
}
int iBarX = iBarGap;
Double iCurrentHeight = 0;
SolidBrush objBrush = new SolidBrush(Color.FromArgb(70, 20, 20));
Font fontLegend = new Font("Arial", 11);
Font fontValues = new Font("Arial", 8);
Font fontTitle = new Font("Arial", 24);
// loop through and draw each bar
for (int iLoop = 0; iLoop <= aMonth.Count - 1; iLoop++)
{
iCurrentHeight = ((Convert.ToSingle(aProfit[iLoop]) / Convert.ToSingle(iMaxColHeight)) * Convert.ToSingle(iMaxHeight - iHeightSpace));
objGraphics.FillRectangle(objBrush, Convert.ToSingle(iBarX), Convert.ToSingle(iMaxHeight - iCurrentHeight), Convert.ToSingle(iBarWidth), Convert.ToSingle(iCurrentHeight));
objGraphics.DrawString(aMonth[iLoop].ToString().Substring(0, 3) , fontLegend, objBrush, Convert.ToSingle(iBarX), Convert.ToSingle(iMaxHeight));
objGraphics.DrawString(String.Format(aProfit[iLoop].ToString(), "#,###"), fontValues, objBrush, Convert.ToSingle(iBarX), Convert.ToSingle(iMaxHeight - iCurrentHeight - 15));
iBarX += (iBarGap + iBarWidth);
}
objGraphics.DrawString(sTitle, fontTitle, objBrush, Convert.ToSingle((iMaxWidth / 2) - sTitle.Length * 6), Convert.ToSingle(iMaxHeight + iLegendSpace));
//objBitmap.Save("C:\inetpub\wwwroot\graph.gif", ImageFormat.GIF)
objBitmap.Save(Response.OutputStream, ImageFormat.Gif);
objGraphics.Dispose();
objBitmap.Dispose();
}
The output is like this :
The following code can be called in the Page Load Event. the Code given here will be displayed from the (x,y) position (0,0). If the pie chart to be drawn from the top of the page, then the following code is OK. If it is need to be created in the center of the page or on the fly means, create a new aspx page and copy the code in the page load event and pass the aspx file as source for an image tag.
Add the following NameSpace
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
int startAngle = 0;
int sweepAngle = 45;
const int width = 500, height = 500;
Response.ContentType = "image/jpeg";
Bitmap objBitmap = new Bitmap(width, height);
Graphics objGraphics = Graphics.FromImage(objBitmap);
// Create a black background for the border
objGraphics.FillRectangle(new SolidBrush(Color.White), 0, 0, width, height);
//To Draw pie chart
SolidBrush objBrush = new SolidBrush(Color.Aqua);
objGraphics.SmoothingMode = SmoothingMode.Default;
Random rand = new Random();
//Loop through 180 back around to 135 degress so it gets drawn correctly.
for(int iLoop = 0;iLoop<= 315;iLoop += 45)
{
startAngle = (iLoop + 180) % 360;
objBrush.Color = Color.FromArgb(rand.Next(255), rand.Next(255), rand.Next(255));
objGraphics.FillPie(objBrush, 50, 50, 300, 300, startAngle, sweepAngle);
}
// Save the image to a file
objBitmap.Save(Response.OutputStream, ImageFormat.Jpeg);
// clean up...
objGraphics.Dispose();
objBitmap.Dispose();
The Output is like this:
Resource File :
• Resource Files are most beneficial while developing multilingual Web applications
• A resource file is a XML file that contains the strings that we want to
a. Translate into different languages.
b. Can be updated dynamically so that user themselves can modify values in resource files once the application is deployed on the server without re-compiling the entire application itself.
• The resource file contains key / value pairs.
• Each pair is an individual resource.
• Key names are not case sensitive.
Types of Resources:
There are 2 types of Resource files :
1.Global Resource file :
This file is present in a specialized folder called /App_GlobalResources, located at the root of the application. All pages, user-controls, etc. can access these resources, so they typically are used as shared resources. The resources which should be available throughout the application are stored in this file.
2.Local Resource file :
These files are present in /App_LocalResources folder. These files follow the naming convention of the associated page, user-control, or master-page, along with the culture definition. These files are only accessible by the associated page.
Sample Code for Resource File :
I Created four label and one dropdown and based on the dropdown selection the language should change. I Created three resource files for English, French and Tamil. In Name Column [Key] I give the Control Name and the Value Column [Value] I give the Text to be displayed.
//To get the Default Language from the browser.
strDefaultLang = Request.Headers["Accept-Language"].Split(',')[0].Trim();
//To specify the Language to be displayed.
if(ddlLanguage.SelectedItem.Text == "English")
strDefaultLang = "en-us";
else if(ddlLanguage.SelectedItem.Text == "Tamil")
strDefaultLang = "ta-IN";
else if (ddlLanguage.SelectedItem.Text == "French")
strDefaultLang = "fr-FR";
//To set the Language for the Control to displayed:
System.Threading.Thread.CurrentThread.CurrentUICulture = System.Globalization.CultureInfo.GetCultureInfo(Convert.ToString(strDefaultLang).ToLower());
lblAddress1.Text = Resources.Language.lbladdress1;
lblAddress2.Text = Resources.Language.lbladdress2;
lblName1.Text = Resources.Language.lblname1;
lblName2.Text = Resources.Language.lblname2;
Language is the name of the Resource file name and the Key name entered in the resource file are populated automatically. Select the key name to populate the value for the control.
The Resource file Name should be unique. the file name in this scenario is Language.resx for default Language [English]. Language.fr.resx for French, Language.ta.resx for Tamil.