Saturday, April 7

Auto Complete Textbox To Store Id While Displaying Name

This is my overall view of the application

























Now the below Diagram shows the overall View of My Database:-



























Now the below Diagram shows the Design View of My web Page Default.aspx:-









Now the below Diagram shows the Source View of My web Page Default.aspx:-














You can Copy the Source code of My web Page Default.aspx here :-

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxtoolkit" %>
<!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>Untitled Page</title>
    <script type="text/javascript" language="javascript">
        function GetCode(source, eventArgs) {
            document.getElementById("hCode").value = eventArgs.get_value();
            document.getElementById("hdnCompanyName").value = source;
        }
        function hCode_onclick() {
        }

    </script>
    <style type="text/css">
        .style1
        {
            width: 100%;
        }
        .style2
        {
            width: 133px;
        }
        .style3
        {
            color: #336600;
        }
        .style4
        {
            color: #800000;
        }
        .style5
        {
            width: 133px;
            color: #000099;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:ScriptManager ID="ScriptManager1" runat="server"> <Services><asp:ServiceReference Path="CompanyServiceForAutoSuggest.asmx" /> </Services></asp:ScriptManager>
    <div> <h1><span class="style4">Om Namah Shivaya(</span><span class="style3">Auto Complete Textbox To Store Id While Displaying Name</span><span class="style4">)</span></h1>
        <table class="style1"><tr> <td class="style5"><strong>Shibashish Name:</strong></td><td><asp:TextBox ID="txtManufacturer" runat="server"/>
    <ajaxToolkit:AutoCompleteExtender ID="AutoCompleteExtender1" runat="server" TargetControlID="txtManufacturer" ServicePath="CompanyServiceForAutoSuggest.asmx"
   ServiceMethod="GetCountries" CompletionInterval="100"  CompletionSetCount="20"  MinimumPrefixLength="1" OnClientItemSelected="GetCode" /></td> </tr>  <tr> <td class="style2">
     &nbsp;</td><td> &nbsp;</td></tr> <tr> <td class="style2">  &nbsp;</td><td> &nbsp;</td>  </tr><tr> <td class="style5"> <strong>Hidden stored ID:</strong></td>
    <td><input type="text" id="hCode" runat="server" value="0" /></td> </tr>  <tr> <td class="style2"> &nbsp;</td> <td> &nbsp;</td></tr></table> </div>
 </form>
</body>
</html>

Now the below Diagram shows the code of  Web.cofig:-










You can Copy the  code of  Web.cofig  here and change it according to your connectionstring :-


<?xml version="1.0"?>
<configuration>
  <connectionStrings>
 <add name="TestConnectionString" connectionString="Data Source=SWASH-DBS\SWASHSQLINT;Initial Catalog=Test;Persist Security Info=True;User ID=ken;Password=kc@2011"
      providerName="System.Data.SqlClient"/>
    
  </connectionStrings>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
  </system.web>
  <system.serviceModel>
    <bindings/>
    <client/>
  </system.serviceModel>
</configuration>


Here is the web Service Code That I have used in My application

<%@ WebService Language="C#" Class="CompanyServiceForAutoSuggest" %>
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
[System.Web.Script.Services.ScriptService]
public class CompanyServiceForAutoSuggest  : System.Web.Services.WebService {
    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }
    [WebMethod]
    public List<string> GetCountries(string prefixText)
    {
        string constring = ConfigurationManager.ConnectionStrings["TestConnectionString"].ConnectionString;
        SqlConnection con = new SqlConnection(constring);
        if (con.State != ConnectionState.Open)
        {
            con.Open();
        }
        SqlCommand cmd = new SqlCommand("select * from TextboxAutoImpliment where Name like @Name", con);
        cmd.Parameters.AddWithValue("@Name",prefixText+'%');
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        List<string> Name = new List<string>();
        if (dt.Rows.Count > 0){
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Name.Add(AjaxControlToolkit.AutoCompleteExtender.CreateAutoCompleteItem(dt.Rows[i][1].ToString(), Convert.ToString(dt.Rows[i][0].ToString())));
            }
        }
        return Name;

    }
   
}


After running the application:-







































Thanks
      Shibashish Mohanty






No comments:

Post a Comment

Please don't spam, spam comments is not allowed here.

.

ShibashishMnty
shibashish mohanty