Thursday, December 6, 2018

Using of AllowAnonymous in Login and Register to Secure Method



 [AllowAnonymous]
 public ActionResult Login(string returnUrl)

 [HttpPost]
 [AllowAnonymous]
 [ValidateAntiForgeryToken]
 public ActionResult Login(LoginModel model, string returnUrl)

 [AllowAnonymous]
 public ActionResult Register()

 [HttpPost]
 [AllowAnonymous]
 [ValidateAntiForgeryToken]
 public ActionResult Register(RegisterModel model)

Convert Database Data to JSON Data


public string ConvertDataTabletoString()
{
         DataTable dt = new DataTable();
         using (SqlConnection con = new SqlConnection("Data Source=XXX;Initial Catalog=master;Integrated Security=true"))
         {
               using (SqlCommand cmd = new SqlCommand("select id,name,age from employee", con))
                {
                  con.Open();
                  SqlDataAdapter da = new SqlDataAdapter(cmd);
                  da.Fill(dt);
                  System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                  List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                   Dictionary<string, object> row;
                               foreach (DataRow dr in dt.Rows)
                                {
                                    row = new Dictionary<string, object>();
                                    foreach (DataColumn col in dt.Columns)
                                    {
                                         row.Add(col.ColumnName, dr[col]);
                                    } 
                                    rows.Add(row);
                                 }
                                return serializer.Serialize(rows);
                      }
                }
           }

Note: You just replace datasource (XXX) then it will take data from your table and covert to JSON. 
Thanks for your comment.

Wednesday, December 5, 2018

Searching Tools supports MVC


Knockout tools

Knockout Tools: Get here

Sample: Click here

JSFiddle: Click Me

Any other tools? Please comment... Thanks


Tuesday, December 4, 2018

Bitmap Combine Multiple Images

public static System.Drawing.Bitmap CombineBitmap(string[] files)
{
    //read all images into memory
    List<System.Drawing.Bitmap> images = new List<System.Drawing.Bitmap>();
    System.Drawing.Bitmap outcomeImage = null;

    try
    {
        int width = 0;
        int height = 0;

        foreach (string image in files)
        {
            //create a Bitmap from the file and add it to the list
            System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);

            //update the size of the final bitmap
            width += bitmap.Width;
            height = bitmap.Height > height ? bitmap.Height : height;

            images.Add(bitmap);
        }

        //create a bitmap to hold the combined image
        outcomeImage = new System.Drawing.Bitmap(width, height);

        //get a graphics object from the image so we can draw on it
        using (System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(outcomeImage))
        {
            //set background color
            g.Clear(System.Drawing.Color.Black);

            //go through each image and draw it on the final image
            int offset = 0;
            foreach (System.Drawing.Bitmap image in images)
            {
                g.DrawImage(image,
                  new System.Drawing.Rectangle(offset, 0, image.Width, image.Height));
                offset += image.Width;
            }
        }

        return outcomeImage;
    }
    catch (Exception ex)
    {
        if (outcomeImage != null)
            outcomeImage.Dispose();

        throw ex;
    }
    finally
    {
        //clean up memory
        foreach (System.Drawing.Bitmap image in images)
        {
            image.Dispose();
        }
    }
}

Thursday, November 29, 2018

Content Management System Structure



                              Fig: Structure of a Content Management System.



                                        Fig: Content Management System Layers.

C# MVC load the PDF without using Acrobat Reader, then send the PDF pages as images to the printer

C# code:

        private RasterImage PDFImage = null;
        private int currentPrintPageNumber;

        private void menuItem1_Click(object sender, EventArgs e)
        {
            using (RasterCodecs codecs = new RasterCodecs())
            {
                codecs.Options.Load.AllPages = true;

               // Load PDF as Image
               PDFImage = codecs.Load(@"Source.pdf");

               // Print
                using(PrintDocument document = new PrintDocument())
                {
                    currentPrintPageNumber = 1;
                    document.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
                    document.Print();
                }
                PDFImage.Dispose();
            }
        }

        private void printDocument_PrintPage(object sender, PrintPageEventArgs e)
        {
            PrintDocument document = sender as PrintDocument;
            RasterImagePrinter printer = new RasterImagePrinter();
            printer.PrintDocument = document;
            printer.SizeMode = RasterPaintSizeMode.FitAlways;
            printer.HorizontalAlignMode = RasterPaintAlignMode.Center;
            printer.VerticalAlignMode = RasterPaintAlignMode.Center;
            printer.PageRectangle = Rectangle.Empty;
            printer.ImageRectangle = Rectangle.Empty;
            printer.UseMargins = false;
            printer.Print(PDFImage, currentPrintPageNumber, e);
            currentPrintPageNumber++;
           
            if(currentPrintPageNumber <= PDFImage.PageCount)
                e.HasMorePages = true;
            else
                e.HasMorePages = false;
            printer.PrintDocument = null;
        }  

Wednesday, November 28, 2018

Best way to put SQL query result in a .NET chart control

Hi guys,
I am working on a web app using asp.net, c#. I am new to this charts control. I have a below SQL query and its result want to put in a pie chart? " SELECT A,B,C,(A+B+C) as Total FROM [dbo].[Data] " A B C Total 30 10 50 200 I'm tried linking with SQL query but it is having 4 columns. not sure how to connect. Thanks