how to create wcf service in asp.net c#

  • Open Visual Studio and select File => New => Project => WCF Service Application.
  • Now appear IService1.cs and Service1.svc page in solution explorer.
  • You can remove the default methods and class. 
  • First of all create a class and its properties in IService1.cs page like below.
 
         [DataContract]
         public class Country
        {
                [DataMember]
                public int ID { get; set; }

                [DataMember]
                public string CountryName { get; set; }
        }

(Note : Here assign a [DataContract] attribute to class and [DataMember] attribute to properties.)

  • Now create the service methods with [OperationContract] attribute within Interface like below.

       [ServiceContract]
       public interface IService1
       {
              [OperationContract]
              List<Country> GetCountry();
       }

(Note : Here the List is a return type, Country is a class name and GetCountry is a method name.)
 
  • Now go to the Service1.svc page.
  • Keep the cursor on inherited interface IService1 then appear down arrow option and click on it.
  • Under the down arrow option, Two option appear, select "implement interface IService1" option.
  • Now all methods of IService1 interface will come in Service1 page automatically like below.

      public class Service1 : IService1
      {
               public List<Country> GetCountry()
              {
                       throw new NotImplementedException();
               }
      }

  • Now type your code into the above inherited method like below  and you can remove the 
     throw new NotImplementedException();
 
      public List<Country> GetCountry()
      {
             XElement el = XElement.Load("CountryOrState.xml");
             List<Country> Coun = new List<Country>();
             Coun = (from myRow in el.Elements()

                    select new Country()
                    {
                          CountryName = myRow.Attribute("Name").Value,
                          ID = Convert.ToInt32(myRow.Attribute("ConId").Value)
                    }).Distinct().ToList();
            return Coun;
        }
 
  • Now You can give the service reference to the application.
 

Difference between stored procedure and function in sql server

    • Function must return a value whereas Stored Procedure it is optional.
    • Functions can have only input parameters for it whereas Procedures can have input/output parameters.
    • Function takes one input parameter it is mandatory but Stored Procedure may take o to n input parameters.
    •  Functions can be called from Procedure whereas Procedures cannot be called from Function.
    • Procedure allows SELECT/INSERT/UPDATE/DELETE statement in it whereas Function allows  only SELECT statement in it.
    • We can go for Transaction Management in Procedure whereas we can't go in Function.

    how to convert list to datatable in c#

    • First of all create the Function for convert data from List to Datatable as below.
               
             static DataTable ConvertListToDataTable(List<string[]> list)
             {
                  DataTable table = new DataTable();
                  int columns = 0;
                  foreach (var array in list)
                  {
                       if (array.Length > columns)
                       {
                             columns = array.Length;
                       }
                  }
                  for (int i = 0; i < columns; i++)
                  {
                      table.Columns.Add();
                  } 
                 foreach (var array in list)
                 {
                     table.Rows.Add(array);
                 }
                return table;
            }

     
    • Now bind the List and convert into Datatable using above function as below. 
             List<string[]> list = new List<string[]>();
             "Bind the List here" 

             DataTable table = ConvertListToDataTable(list);

    autocomplete textbox using ajax in asp.net

    • First register the Ajax control toolkit in .aspx page like below.
              <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit"
               TagPrefix="cc1" %>

    • Put the ToolkitScriptManager from Ajax Control Toolkit in .aspx page.
            <cc1:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
            </cc1:ToolkitScriptManager>
    • Now assign the ajax control "AutoCompleteExtender" to theTextBox. 
              <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
              <cc1:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server"
                       TargetControlID="TextBox1" CompletionInterval="1000" 
                       MinimumPrefixLength="0" EnableCaching="True"
                       CompletionSetCount="1" ServiceMethod="GetCountries">
              </cc1:AutoCompleteExtender>

    • Now create the service method for fetch data from database as per textbox requirement in .aspx.cs page.
              using System.Web.UI.WebControls;

              [System.Web.Services.WebMethod]
              public static List<string> GetCountries(string prefixText)
              { 
                    SqlConnection con = new SqlConnection("connection string") ;
                    con.Open();

                    SqlDataAdapter da = new SqlDataAdapter("select * from tblCountry
                    where CountryName like '" +  prefixText + "'+'%'", con);

                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    con.Close();
                
                    List<string> CountryNames = new List<string>();
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                            CountryNames.Add(dt.Rows[i][1].ToString());
                    }
                    return CountryNames;
              }
                    
                   

    how to redirect to another page using javascript

    • In this example, We explain how to redirect to .aspx File using Javascript Function.
    • There is a sitution when we set timer in javascript function when the time is up then the page is redirect to specify page like below.
              <script type="javascript">
                function demo()
                {
                        window.location.href = "Default.aspx";
                }
              </script>