Monday, January 18, 2016

Generic in C# with example?



Generic can be defined as <T> sign after the class name, by using the generic there is no need of boxing and unboxing. Generic also can be implemented with interfaces, delegates and methods.

Example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    class Class1<T>
    {
        T i;
        public T prop1
        {
            get
            {
                return i;
            }
            set
            {
                i = value;
            }
        }
    }
}


Code on button click:
Class1<int> obj = new Class1<int>();
        private void button1_Click(object sender, EventArgs e)
        {

            obj.prop1 = 100;
            int x = obj.prop1;
            MessageBox.Show(x.ToString());
        }

Output : 100

Friday, January 1, 2016

Write Dynamically Text on Image in ASP.NET C#

In this article, I am going to explain how to write some text on a image in ASP. net c#.

Follow given below step to write text on an image in asp.net c#

Step 1

Go to Menu File->New-Project


Step 2

After clicking on project a new window will be open

select ASP.NET Web Application and write the name "DynamicTextOnImage" and click on "OK" button.


Code that write text on image

string name = "Dear Reader,";
 
                Bitmap bitMapImage = new System.Drawing.Bitmap(Server.MapPath(@"happynewyear.jpg"));
                Graphics graphicImage = Graphics.FromImage(bitMapImage);
 
                Color colorStringColor = System.Drawing.ColorTranslator.FromHtml("#ffffff");
                graphicImage.DrawString(name, new Font("Arial", 15, FontStyle.Bold), new SolidBrush(colorStringColor), new Point(100, 8));
 
                string stringOutPutFileName = Server.MapPath("Image\\happynewyear.jpg");
 
                using (MemoryStream memory = new MemoryStream())
                {
                    using (FileStream fs = new FileStream(stringOutPutFileName, FileMode.Create, FileAccess.ReadWrite))
                    {
                        bitMapImage.Save(memory, ImageFormat.Jpeg);
                        byte[] bytes = memory.ToArray();
                        fs.Write(bytes, 0, bytes.Length);
                    }
                }
                graphicImage.Dispose();
                bitMapImage.Dispose();
                bitMapImage.Dispose();

aspx.cs code

using System;
using System.Drawing;
using System.IO;
using System.Drawing.Imaging;
 
namespace DynamicTextOnImage
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
            try
            {
                string name = "Dear Reader,";
 
                Bitmap bitMapImage = new System.Drawing.Bitmap(Server.MapPath(@"happynewyear.jpg"));
                Graphics graphicImage = Graphics.FromImage(bitMapImage);
 
                Color colorStringColor = System.Drawing.ColorTranslator.FromHtml("#ffffff");
                graphicImage.DrawString(name, new Font("Arial", 15, FontStyle.Bold), new SolidBrush(colorStringColor), new Point(100, 8));
 
                string stringOutPutFileName = Server.MapPath("Image\\happynewyear.jpg");
 
                using (MemoryStream memory = new MemoryStream())
                {
                    using (FileStream fs = new FileStream(stringOutPutFileName, FileMode.Create, FileAccess.ReadWrite))
                    {
                        bitMapImage.Save(memory, ImageFormat.Jpeg);
                        byte[] bytes = memory.ToArray();
                        fs.Write(bytes, 0, bytes.Length);
                    }
                }
                graphicImage.Dispose();
                bitMapImage.Dispose();
                bitMapImage.Dispose();
            }
            catch (Exception ex)
            {
 
            }
        }
    }
}
 
Image before writing text on image
 
image source
 


After writing text on image
  


Tuesday, December 29, 2015

how to read xml file in sql server stored procedure

In this article, I am going to explain how to read or select xml data in SQL.

Firstly I show you xml which one is ready in SQL procedure. The given below xml have Empcode,name,DOB and age fileds, which one shown by sql in a table

<EmpDetails>
    
<NewDataSet>
        
<EmpCode>EMP014</EmpCode>
        
<name>Sharad</name>
        
<DOB>12/24/1978 12:00:00 AM</DOB>
        
<age>35</age>
        
<location>x</location>
        
<gender>Male</gender>
        
<StateID>1</StateID>
        
<CityID>1</CityID>
        
<designation>1 </designation>
    
</NewDataSet>
    
<NewDataSet>
        
<EmpCode>EMP015</EmpCode>
        
<name>Devesh </name>
        
<DOB>12/24/1983 12:00:00 AM</DOB>
        
<age>31</age>
        
<location>Delhi</location>
        
<gender>Male</gender>
        
<StateID>1</StateID>
        
<CityID>1</CityID>
        
<designation>1</designation>
    
</NewDataSet>
</EmpDetails>

Now i write a Procedure that read xml and return result in table format.

Create PROCEDURE usp_ReadXml (
                @xmlString NVARCHAR(MAX) = '<EmpDetails>
    <NewDataSet>
        <EmpCode>EMP014</EmpCode>
        <name>Sharad</name>
        <DOB>12/24/1978 12:00:00 AM</DOB>
         <age>35</age>
        <location>x</location>
        <gender>Male</gender>
        <StateID>1</StateID>
        <CityID>1</CityID>
        <designation>1 </designation>
    </NewDataSet>
    <NewDataSet>
        <EmpCode>EMP015</EmpCode>
        <name>Devesh </name>
        <DOB>12/24/1983 12:00:00 AM</DOB>
        <age>31</age>
        <location>Delhi</location>
        <gender>Male</gender>
        <StateID>1</StateID>
        <CityID>1</CityID>
        <designation>1</designation>
    </NewDataSet>
</EmpDetails>
'
                )
AS
BEGIN
                DECLARE @xmlHandle INT
 
                EXEC sp_xml_preparedocument @xmlHandle OUTPUT,  @xmlString
 
                SELECT NAME,
                                Age,
                                Gender,
                                Location,
                                designation,
                                StateID,
                                CityID,
                                EmpCode,
                                DOB
                FROM OPENXML(@xmlHandle, '/EmpDetails/NewDataSet', 2) WITH (
                                                name NVARCHAR(100),
                                                age INT,
                                                gender VARCHAR(10),
                                                location VARCHAR(50),
                                                designation INT,
                                                StateID INT,
                                                CityID INT,
                                                EmpCode VARCHAR(50),
                                                DOB DATETIME
                                                )
 
                EXEC sp_xml_removedocument @xmlHandle
END

Output is

Run this command "Exec usp_ReadXml" and now output is