Thursday, May 30, 2013

How to disable auto complete text from textbox in asp.net

How To Disable Auto Complete Text From Textbox

Some times for security purpose it is required to disable auto complete text from textbox. It also work on asp.net server tag.
To do it, In html Form tag have one property i.e. autocomplete. Set it to off. Like:

autocomplete="off"

Demo:

When autocomplete on:

   E-mail:

 


When autocomplete off:

   E-mail:

 

Saturday, February 16, 2013

Insert and search from database using WCF:


Insert and search from database using  WCF:
INTRO:
Windows Communication Foundation is an acronym of WCF.
Basically WCF is based on Service Oriented Architecture(SOA). It is a technology to develop applications that are based on SOA.

A WCF clients connect to WCF through an Endpoint. Endpoint contains Address, Binding, and Contracts. We can use ABC to remember these keywords.

Address:  Address denotes the address of WCF Service. The address contains  two elements: address of the  service and the transport protocol, used to communicate with the service. 

Binding: Binding denotes how communication done between service and client, means  what protocol is used  to  access the service. Protocols like HTTP, TCP, MSMQ  etc.

Contracts:  Contract means agreement between two parties . Similarly in WCF it is the agreement between client and service.  Contract defines what service does.  It have method definition and return type and data. Mainly it contains,
Service Contract
Operation Contract
Data Contract
Data Member

About Application: In this application you can add new student to database and search name using student id.
Lets Start:

Table name:  Student
Id is primery key (int)
Name is varchar(50)





To create WCF Service Firstly creates Interface that contains Service in IService1.cs:
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;

namespace WcfService2
{
    [ServiceContract]
    public interface IService1
    {
        [OperationContract]
        string setdata(student s);
        [OperationContract]
        student getdata(string nm, int i);
    }

    [DataContract]
    public class student {
          int _id;
          string _name;
       [DataMember]
        public int Id {
            get {
                return _id;
            }
            set {
                _id = value;
            }
        }
        [DataMember]
        public string Name {
            get {
                return _name;
            }
            set {
                _name = value;
            }
        }
    }
}


Now,  implement that interface in Service1.svc

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Web;
using System.Text;
using System.Data.SqlClient;
using System.Configuration;

namespace WcfService2
{
  
    public class Service1 : IService1
    {
        string con = ConfigurationManager.AppSettings["con"];
        SqlConnection conn;
        public string setdata(student s)
        {
            try
            {
                conn = new SqlConnection(con);
                conn.Open();
 SqlCommand com = new SqlCommand("insert into student (name) values(@student)", conn);
 com.Parameters.AddWithValue("@student", s.Name);
 com.ExecuteNonQuery();
                return "inserted";

            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
            finally {
                conn.Close();
           
            }
        }

        public student getdata(string nm,int id)
        {
            student s = new student();
            try
            {
                conn = new SqlConnection(con);
                SqlCommand com = new SqlCommand("select name from student where id=@id", conn);
                conn.Open();
                com.Parameters.AddWithValue("@id", id);
               
                SqlDataReader read = com.ExecuteReader();
                read.Read();
                s.Name = read[0].ToString();
                s.Id =Convert.ToInt32( id);
                return s;
            }
            catch (Exception ex)
            {
                s.Name = "no result found";
                return s;
            }
            finally {
                conn.Close();
            }

        }
    }
}





Then add new project in WCF service solution and  add new web application





Add following code in web form.aspx file inside form tag:
    Id<asp:TextBox runat="server" ID="sid">
    </asp:TextBox>
    <asp:Button ID="search" Text="search" runat="server" onclick="search_Click" /><br />

    <asp:Label runat="server" ID="name">
    </asp:Label><br />
    <asp:Label runat="server" ID="id"></asp:Label>

    </div>
    <hr />
    <br />
    Name<asp:TextBox ID="inm" runat="server"></asp:TextBox>
    <asp:Button ID="insert" runat="server" Text="insert" onclick="insert_Click" />
    <asp:Label runat="server" ID="res"></asp:Label>

Now ASP page looks like :



In code behind file add the following code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ServiceModel;
using WebApplication2.ServiceReference2;

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

        }

        protected void search_Click(object sender, EventArgs e)
        {

            Service1Client client = new Service1Client();
            student s = new student();
           s= client.getdata("",Convert.ToInt32(sid.Text));
           name.Text=s.Name;
            id.Text=s.Id.ToString();
           
        }

        protected void insert_Click(object sender, EventArgs e)
        {

            Service1Client client = new Service1Client();
          
            student s = new student();
            s.Name = inm.Text.ToString();
            string res=client.setdata(s);
            this.res.Text = res;

        }
    }



 You have to copy this line by right clicking on Service1.svc file and click on option View in Browser:           Service1Client client = new Service1Client();






Now, right click on Service1.svc file and click on option View in Browser:
And copy address





IN Web application right and select option ADD SERVICE REFERENCE






Paste copied address in new open small window like this and press GO button:



 
Now right click on webapplication and select set as startup project
Then  set  webform.aspx page as a start page and run project.



Related Posts Plugin for WordPress, Blogger...