Friday, 17 August 2012
Sunday, 5 August 2012
How to Calculate Age in Sql Server OR Find age using SQL query
Calculate age of employee using SQL Server
To calculate an age from the SQL table is little bit tricky. We are demonstrating how to calculate the age from the date of birth fields in the SQL table. While fill the application form it’s better to fill the Date Of Birth field instead of age field. Because age field can calculate from the Date Of Birth field easily and accurately.
Find the age from the Date Of Birth using SQL Server
Here we are having a table called Employee and having coloumns ID,Name and DOB. From this table we need to find the age of each employee using SQL script.

SELECT DOB AS DateOfBirth,
GETDATE() AS CurrentDate,
DATEDIFF(YEAR,DOB,GETDATE()) -
(CASE WHEN
DATEADD(YY,DATEDIFF(YEAR,DOB,GETDATE()),DOB) > GETDATE()
THEN 1
ELSE 0 END) AS Age
FROM Employee
Tuesday, 24 July 2012
How to pass table to stored procedures in SQL
What is Table – Valued Parameter in SQL Server 2008?
Table-valued parameters provide an easy way to marshal multiple rows of data from a client application to SQL Server without requiring multiple round trips or special server-side logic for processing the data. You can use table-valued parameters to encapsulate rows of data in a client application and send the data to the server in a single parameterized command. The incoming data rows are stored in a table variable that can then be operated on by using Transact-SQL.
What we are going to do with Table – Valued Parameter?
We are going to demonstrate a very simple example for using Table – Valued parameter. In this sample project we will insert bulk amount of data into the table by passing a bulk data using datatable in C# to SQL stored procedure.
Create a Table for insert data using Table – Valued Parameter
Here we are having a table named Officer and having three fields ID,Name and Salary. We are going to fill the table with bulk data.
Table-valued parameters provide an easy way to marshal multiple rows of data from a client application to SQL Server without requiring multiple round trips or special server-side logic for processing the data. You can use table-valued parameters to encapsulate rows of data in a client application and send the data to the server in a single parameterized command. The incoming data rows are stored in a table variable that can then be operated on by using Transact-SQL.
What we are going to do with Table – Valued Parameter?
We are going to demonstrate a very simple example for using Table – Valued parameter. In this sample project we will insert bulk amount of data into the table by passing a bulk data using datatable in C# to SQL stored procedure.
Create a Table for insert data using Table – Valued Parameter
Here we are having a table named Officer and having three fields ID,Name and Salary. We are going to fill the table with bulk data.
CREATE TABLE Officer( ID INT PRIMARY KEY IDENTITY(1,1), NAME VARCHAR(50), SALARY DECIMAL(18, 0))Stored Procedure for insert data by accepting Table Valued Parameter
Now we are going to create a Stored Procedure that accepting a table type as parameter and insert values in this type into the table.
CREATE PROCEDURE InsertOfficerDetails ( @OfficerData OfficerDetails readonly ) AS INSERT INTO Officer (Name, Salary) SELECT Name, Salary FROM @OfficerData;
Wednesday, 27 June 2012
A visual Git reference
A brief, visual reference for the most common commands in git.
The four commands between the working directory, the stage (also called the index), and the history (in the form of commits).
•git add files copies files (at their current state) to the stage.
•git commit saves a snapshot of the stage as a commit.
•git reset -- files unstages files; that is, it copies files from the latest commit to the stage. Use this command to "undo" a git add files. You can also git reset to unstage everything.
•git checkout -- files copies files from the stage to the working directory. Use this to throw away local changes.
Ref:-http://marklodato.github.com/visual-git-guide/index-en.html
The four commands between the working directory, the stage (also called the index), and the history (in the form of commits).
•git add files copies files (at their current state) to the stage.
•git commit saves a snapshot of the stage as a commit.
•git reset -- files unstages files; that is, it copies files from the latest commit to the stage. Use this command to "undo" a git add files. You can also git reset to unstage everything.
•git checkout -- files copies files from the stage to the working directory. Use this to throw away local changes.
Ref:-http://marklodato.github.com/visual-git-guide/index-en.html
Wednesday, 13 June 2012
Outlook configuration
For Outlook
1. In Outlook, go to the Tools menu and click on Email Accounts.
2. Select Add a new email account and then click Next.
3. Select POP3 and then click Next.
4. Enter your email information:
Your Name: your name
Email Address: matt@castleman.net (the email address the messages will be sent from)
Incoming Mail server (POP3): mail.castleman.net
Outgoing Mail server (SMTP): mail.castleman.net
User Name: matt@castleman.net
Password: the password for the email address
5. Click on More Settings and Select the Outgoing Server Tab. Check My outgoing server (SMTP) requires authentication. Select Use same settings as my incoming mail server.
6.Click Ok. Click Next. Click Finish.
If you cannot send email using mail.yourdomainname.com as your SMTP server,
it may be because your ISP is blocking port 25 on their network, used for sending outoing mail via the SMTP protocol. In this instance, we recommend using your ISPs outgoing mail server, which can be obtained from your ISP.
1. In Outlook, go to the Tools menu and click on Email Accounts.
2. Select Add a new email account and then click Next.
3. Select POP3 and then click Next.
4. Enter your email information:
Your Name: your name
Email Address: matt@castleman.net (the email address the messages will be sent from)
Incoming Mail server (POP3): mail.castleman.net
Outgoing Mail server (SMTP): mail.castleman.net
User Name: matt@castleman.net
Password: the password for the email address
5. Click on More Settings and Select the Outgoing Server Tab. Check My outgoing server (SMTP) requires authentication. Select Use same settings as my incoming mail server.
6.Click Ok. Click Next. Click Finish.
If you cannot send email using mail.yourdomainname.com as your SMTP server,
it may be because your ISP is blocking port 25 on their network, used for sending outoing mail via the SMTP protocol. In this instance, we recommend using your ISPs outgoing mail server, which can be obtained from your ISP.
Wednesday, 9 May 2012
How to Enable Network Access in SQL Server Configuration Manager
To enable a network protocol
- On the Start menu, choose All Programs, point to Microsoft SQL Server and then click SQL Server Configuration Manager.
Optionally, you can open Computer Manager by right-clicking My Computer and choosing Manage. In Computer Management, expand Services and Applications, expand SQL Server Configuration Manager. - Expand SQL Server Network Configuration, and then click Protocols for InstanceName.
- In the list of protocols, right-click the protocol you want to enable, and then click Enable.
The icon for the protocol will change to show that the protocol is enabled. - To disable the protocol, follow the same steps, but choose Disable in step 3.
To configure a network protocol
- On the Start menu, right-click My Computer, and then choose Manage.
- In Computer Management, expand Services and Applications, expand SQL Server Configuration Manager, expand Server Network Configuration, expand Protocols for InstanceName, and then click the protocol you want to configure.
- Right-click the protocol you want to configure, and then choose Properties.
- In Properties, you can set the protocol-specific options.
Tuesday, 24 April 2012
How to create thumbnail image in ASP.Net/C#
How to reduce the size of image before uploaded to the server in ASP.Net/C#
In most of the ASP.Net application with uploaded image facility will face the performance issue while loading uploaded images to display for the user. We can limited the maximum size of the image that a user can upload but it may not be a good solution because a user has to facing very difficult situation for uploading a large image. He has to reduce the image from other tools and upload again.
The better approach to solve this issue, we are not going to giving any maximum size limit for uploading image. Instead we are internally reducing the size of the image that a user going to upload by checking whether it is exceeds affordable size. Then there is no constraints are given to user so that he doesn’t care about the size and quality of the image.
Image Size 764 KB
Simple steps to reduce the size of image before upload in ASP.Net/C#
Here we are going to demonstrate a very simple and best approach to reduce the size of the image before uploaded to the server. In some application we need to show thumbnail image with uploaded images. In that case there is no need to store very large image in the server. So before going to uploading to the server, we are going to create thumbnail image or reduce the size of image as we wish using C#/ASP.net. This is very simple sample of reducing image and can be easily understand by beginners also.
Step by Step process to create an application for reducing size of image before uploaded to the server in ASP.Net/C#
Step 1 . Create a ASP.Net project and create a web page.
Step 2. Drag a file uploader, button and Datalist(for display uploaded images)
ASPX Page
<asp:Panel runat="server">
<asp:FileUpload ID="fileupload1" runat="server" />
<asp:Button ID="btnsave" runat="server" Text="Upload" OnClick="btnsave_Click" />
</div>
<div>
<asp:DataList ID="dtlist" runat="server" RepeatColumns="3" CellPadding="5">
<ItemTemplate>
<asp:Image ID="Image1" ImageUrl='<%# Bind("Name", "~/Images1/{0}") %>' runat="server" />
<br />
<asp:HyperLink ID="HyperLink1" Text='<%# Bind("Name") %>'
NavigateUrl='<%# Bind("Name", "~/Images1/{0}") %>'
runat="server" />
</ItemTemplate>
<ItemStyle BorderColor="Brown" BorderStyle="dotted" BorderWidth="3px"
HorizontalAlign="Center"
VerticalAlign="Bottom" />
</asp:DataList>
</asp:Panel>
Step 3 . In page load call function ‘BindDataList’ to display uploaded images
Step 4. On Save button click event, call function ‘GenerateThumbnails’ for reduce of image.
Code Behind Page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindDataList();
}
}
protected void BindDataList()
{
DirectoryInfo dir = new DirectoryInfo(MapPath("Images1"));
FileInfo[] files = dir.GetFiles();
ArrayList listItems = new ArrayList();
foreach (FileInfo info in files)
{
listItems.Add(info);
}
dtlist.DataSource = listItems;
dtlist.DataBind();
}
protected void btnsave_Click(object sender, EventArgs e)
{
string filename = Path.GetFileName(fileupload1.PostedFile.FileName);
string targetPath = Server.MapPath("Images1/" + filename);
Stream strm = fileupload1.PostedFile.InputStream;
var targetFile = targetPath;
//Based on scalefactor image size will vary
GenerateThumbnails(0.5, strm, targetFile);
BindDataList();
}
private void GenerateThumbnails(double scaleFactor, Stream sourcePath,
string targetPath)
{
using (var image = Image.FromStream(sourcePath))
{
// can given width of image as we want
var newWidth = (int)(image.Width * scaleFactor);
// can given height of image as we want
var newHeight = (int)(image.Height * scaleFactor);
var thumbnailImg = new Bitmap(newWidth, newHeight);
var thumbGraph = Graphics.FromImage(thumbnailImg);
thumbGraph.CompositingQuality = CompositingQuality.HighQuality;
thumbGraph.SmoothingMode = SmoothingMode.HighQuality;
thumbGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
var imageRectangle = new Rectangle(0, 0, newWidth, newHeight);
thumbGraph.DrawImage(image, imageRectangle);
thumbnailImg.Save(targetPath, image.RawFormat);
}
}
Image Size 36 KB
By using above application, we can reduce the size of images before uploaded to the server. If we check the size of the image that we uploaded, that will be less than the size of image that having in the local folder. We can implement this mechanism in most of the shopping site and photo gallery sites so that we can store large amount of images with small size and it will help the performance of the application. We can identify that even reducing the size of image there is no variation in quality or clarity of the image. So we can easily reduce the resolution of the image using above code in ASP.Net/C# application.
Subscribe to:
Posts (Atom)