Showing posts with label c sharp. Show all posts
Showing posts with label c sharp. Show all posts

Saturday, October 26, 2013

Remove Html tag from String

public string Strip(string text)
 {
     string s = Regex.Replace(text, @”<(.|\n)*?>”, string.Empty);
     s = s.Replace("&nbsp;", " ");
     s = Regex.Replace(s, @"\s+", " ");
     s = Regex.Replace(s, @"\n+", "\n");
     return s;
 }
 

Tuesday, June 11, 2013

Asynchrous Save

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="AsynchrousSave.aspx.cs" Inherits="DUMMY_AsynchrousSave" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    
    <script type='text/javascript'>
        function GetMessage() {
            PageMethods.Message(OnGetMessageSuccess, OnGetMessageFailure);
        }
        function OnGetMessageSuccess(result, userContext, methodName) {
            alert(result);
        }
        function OnGetMessageFailure(error, userContext, methodName) {
            alert(error.get_message());
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID='ScriptManager1' runat='server' EnablePageMethods='true' />
    <div>
    <input type='submit' value='Get Message' onclick='GetMessage();return false;' />
    </div>
    
    </form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class DUMMY_AsynchrousSave : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    [System.Web.Services.WebMethod]
    public static string Message()
    {
        string Output = string.Empty;
        try
        {
            UserDetail objUserDetail = new UserDetail();
            objUserDetail.Id = Guid.NewGuid();
            objUserDetail.FirstName = "FirstName";
            objUserDetail.LastName = "LastName";
            objUserDetail.Email = "test@test.com";
            UserDetailProvider.Save(objUserDetail);
            Output = "Message Successfull";
        }
        catch (Exception ex)
        {
            Output = "Message Not Successfull";
        }
        return Output;//"Hello from the server-side World!";
    }
}

Wednesday, June 15, 2011

How to capture web page screenshot in asp.net using c#

In this tutorial you will learn how to capture webpage screenshot in asp.net using c#. You may got a requirement to capture the screenshot when user submits the form , etc. Most of the people purchase third party dll files and use them in their project to capture the screenshot of webpage. It is quite easy in asp.net and there is no need to purchase any dll. Let's have a look over how to do so.

How to capture web page screenshot in asp.net using c#

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
using System.Web.Services;
using System.Text;
using System.Windows.Forms;//must be included for capturing screenshot
public partial class capture_webpage_screenshot : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        Bitmap bitmap = new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
        Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image);
        graphics.CopyFromScreen(25, 25, 25, 25, bitmap.Size);
        bitmap.Save(@"c:\screenshot\myscreenshot.bmp", ImageFormat.Bmp);
       
    }
}

The code is ready. Just you have to do the following steps.


  1. Right click the project name
  2. Add reference -> .net tab and then choose system.windows.forms namespace then press ok button.
  3. In .aspx code behind file you have to import the System.Windows.Formsnamespace and that’s it.
By customizing parameters used in CopyFromScreen() function, you can figure out how to capture the small portion of the screen, if required. Create a folder and give folder path in c# code just like above i have given. You can customize the code as well and give the dynamic name of bitmap file every time it is generated.

How to get date for every sunday in a month using asp.net with c#

In this programming tutorial you will learn how to get the date for every sunday in a month using asp.net with c#. There are multiple methods to achieve our goal. Let`s have a look over them.

How to get date for every sunday in a month in asp.net using c#

Method 1

using System.Globalization;//Donot forget to declare this namespace
 protected void Page_Load(object sender, EventArgs e)
    {
 DateTime currentDateTime=System.DateTime.Now;
        int year=currentDateTime.Year;
        int months=currentDateTime.Month;
        GetDatesOfSundays(year, months, DayOfWeek.Sunday);
    }
    protected void GetDatesOfSundays(int year, int month, DayOfWeek dayName)
    {
        CultureInfo ci = new CultureInfo("en-US");
        for (int i = 1; i <= ci.Calendar.GetDaysInMonth(year, month); i++)
        {
            if (new DateTime(year, month, i).DayOfWeek == dayName)
                Response.Write(i.ToString() + ",");
        }
    }

Output
1,8,15,22,29

 


Method 2

protected void Page_Load(object sender, EventArgs e)
    {
     Response.Write(GetDatesOfSundays(System.DateTime.Now));
    }
private string GetDatesOfSundays(DateTime DatMonth)
    {
        string sReturn = "";
        int iDayOffset = DatMonth.Day - 1;
        DatMonth = DatMonth.AddDays(System.Convert.ToDouble(-DatMonth.Day + 1));
        DateTime DatMonth2 = DatMonth.AddMonths(1).AddDays(System.Convert.ToDouble(-1));
        while (DatMonth < DatMonth2)
        {
            if (DatMonth.DayOfWeek == System.DayOfWeek.Sunday)
            {
                if (sReturn.Length > 0) sReturn += ",";
                sReturn += DatMonth.ToShortDateString();
            }
            DatMonth = DatMonth.AddDays(1.0);
        }
        return sReturn;
    } 

Output
5/1/2011,5/8/2011,5/15/2011,5/22/2011,5/29/2011


So these are the methods to find the sundays in a month using asp.net with c#.