Afshin_Zavar Level: Professor

 Registered: 17-07-2003 Posts: 74
|
AutoComplete TextBox using AjaxToolkit
What's wrong with my code ?!
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication1._Default" %>
<%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="asp" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</asp:ToolkitScriptManager>
<asp:TextBox ID="txtMovie" runat="server"></asp:TextBox>
<asp:AutoCompleteExtender
ID="AutoCompleteExtender1"
runat="server"
TargetControlID="txtmovie"
ServicePath="AutoComplete.asmx"
ServiceMethod="GetProducts"
MinimumPrefixLength="2"
EnableCaching="false">
</asp:AutoCompleteExtender>
</div>
</form>
</body>
</html>
|
and
using System.Collections;

using System.Web.Services;

using System.Web.Services.Protocols;

using System.Data;
using System.Data.SqlClient;

using System.Configuration;


namespace WebApplication1

{

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.ComponentModel.ToolboxItem(false)]

public class AutoComplete : System.Web.Services.WebService

{
[System.Web.Services.WebMethod]

[System.Web.Script.Services.ScriptMethod]

public string[] GetProducts(string prefixText)

{

SqlConnection cnn = new SqlConnection(@"Data Source=.;Initial Catalog=Northwind;Persist Security Info=True;User ID=sa;Password=123");

string sql = "Select * from [products] Where [ProductName] Like @prefixText";
SqlDataAdapter da = new SqlDataAdapter(sql, cnn);

da.SelectCommand.Parameters.Add("@prefixText", SqlDbType.VarChar, 50).Value = prefixText + "%";

DataTable dt = new DataTable();

da.Fill(dt);

string[] items = new string[dt.Rows.Count];

int i = 0;

foreach (DataRow dr in dt.Rows)
{

items.SetValue(dr["ProductName"].ToString(), i);

i++;

}

return items;

}
 }
}

|
[Edited by Afshin_Zavar on 30-07-2010 at 06:08 PM GMT]
____________________________
Persia
|