6 March 2014

sending data from jquery to webservice | calling web service from jquery on ajax/json post in asp.net

hi in this post i will show how to call a webservice from jquery using ajax/Json post in asp.net :


1. add in web.config

    <authorization>
      <allow users="*"/>
    </authorization>
    

2. add a webservice file and in codebehind of it add method to be called on ajax-json post request:

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.Web.Script.Services.ScriptService()]

public class WebService : System.Web.Services.WebService {

    public WebService () {
       
    }

    [WebMethod]
    public string HelloWorld(string message, string Type)
    {
        return "Hello World";
    }
    
}


3.   Jquery code to be used on the html page to call a webservice on ajax/json post request :


   var Tag = '{message: "' + name.val().toString() + '", Type: "' + 'A' + '"}';

                            $.ajax({ type: "POST", url: "WebService.asmx/HelloWorld", cache: false, contentType: "application/json; charset=utf-8", data: Tag, dataType: "json",
                                success: function (msg) {
                                   
                                    alert('Successful');

                                },

                                error: function (response) {

                                    alert(response);

                                }

                            });

Features of .net framework 4.0

Features of .net framework 4.0 ?

a. We can enable and disable viewstate.

b. Output cache extensibility which means we can use hard drives or any other memory resources to save
the cache data. 

c. we can compress the session state values.

d. Response.Redirectpermanent new keyword to permanently redirect to a page.

e. New Keywords introduced in code behind for meta keywords and meta description.
    example : page.metadescription = "Welcome to my website. we provide services for web designing"
                        page.metakeywords = "web designing, professional web designers "

f. Routing in Asp.net 4.0. It helps us to provide a more meaningful URLs.
   http://www.xyz.com/shop.aspx?countryid=91&branchid=6
   using routing we can change it to
   http://www.xyz.com/shop/country/branch

e. Some of the sections from the webconfig file has been moved to the machine config file, which makes our web config file more cleaner and easy to manage.

f. new keyword enviroment.is64bitoperatingsystem introduced to check whether the operating system is a 64 bit or not.

g. Side by side execution : This helps us to run applications simultaneously which are based on different .net frameworks.
 example : one application is on .net 3.5 and other is on .net 4.0 both can run simultaneously.

Reverse a number c#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a value");
            int a = Convert.ToInt16(Console.ReadLine());
            int rev = 0, rem = 0;
            do
            {
                rem = a % 10;
                rev = rev * 10 + rem;
                a = a / 10;
            }
            while (a > 0);
            Console.WriteLine("the reversed no. is "+rev.ToString());
          
            Console.WriteLine("end");
            Console.ReadLine();
        }
    }
}

File Creation with textboxes in C#.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window2.xaml
    /// </summary>
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();
        }
        StreamWriter sw;
        private void button1_Click(object sender, RoutedEventArgse)
        {
            sw = File.CreateText(@"E:\" + txtname.Text + ".txt");
            sw.WriteLine("\t\t\tCurriculam vitae");
            sw.WriteLine("Name\t:" + txtname.Text);
            sw.WriteLine("Email\t:" + txtemail.Text);
            sw.Close();
            System.Diagnostics.Process.Start("notepad.exe",@"E:\" + txtname.Text + ".txt");


        }
    }
}
 

 

Directory Creation in C#.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for Window3.xaml
    /// </summary>
    public partial class Window3 : Window
    {
        public Window3()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, RoutedEventArgse)
        {
            Directory.CreateDirectory(@"E:\abc");
        }
    }
}