Monday, July 28, 2014

Show Popup Message in C#.Net

Show Popup Message in C#.Net

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        ShowPopUpMsg ("This is popup message.");
    }

    private void ShowPopUpMsg(string msg)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.Append("alert('");
        sb.Append(msg.Replace("\n", "
\\n").Replace("\r", "").Replace("'", "\\'"));
        sb.Append("');");
        ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "showalert", sb.ToString(), true);
    }
}

Wednesday, July 16, 2014

Export to Excel in C#.Net

Sample Export to Excel from C#.Net

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using CarlosAg.ExcelXmlWriter;

public class ExportData
{
 [STAThread]
 public void ExportToExcel()
 {
  Workbook book = new Workbook();
  Worksheet sheet = book.Worksheets.Add("Sheet1"); 
  WorksheetRow row =  sheet.Table.Rows.Add(); 
  row.Cells.Add("Hello World");
  book.Save(@"D:\Test1.xls");
 }
}

Sample Web Service and Client project


Sample Web Service Code file:

using System;
using System.Web.Services;
using System.Xml.Serialization;
[WebService(Namespace = "http://localhost/MyWebServices/")]
public class FirstService : WebService
{
    [WebMethod]
    public int Add(int a, int b)
    {
        return a + b;
    }
    [WebMethod]
    public String SayHello()
    {
        return "Hello World";
    }
}

 
Sample Web Service Client Code file:

using System;
using System.Data;
using System.Configuration;
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;
using System.IO;
using FirstService;
public partial class _Default : System.Web.UI.Page
{
    protected void runSrvice_Click(Object sender, EventArgs e)
    {
        FirstService.FirstService mySvc = new FirstService.FirstService();
        Label1.Text = mySvc.SayHello();
        Label2.Text = mySvc.Add(Convert.ToInt32(txtNum1.Text), Convert.ToInt32(txtNum2.Text)).ToString();
    }
}