What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 2383 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > ASP.NET AJAX > AutoCompleteExtender in Textbox from SQL Table ...
Johnbhatt

AutoCompleteExtender in Textbox from SQL Table

 Code Snippet posted by: Johnbhatt | Posted on: 6/27/2012 | Category: ASP.NET AJAX Codes | Views: 1346 | Status: [Member] | Points: 40 | Alert Moderator   


Hi,
We will learn how to enable AutoCompleteExtender in Textbox Control.

Here, We will learn to Map data from SQL Database to Textbox within Same Page. WebMethod in .cs Page.

Directly to Code:

ASPX Page Code

<table>

<tr>
<td>
Vehicle No :
</td>
<td>
<asp:TextBox ID="txtVehicleNo" runat="server" ToolTip="Enter Vehicle Number (Full)"
Width="168px"></asp:TextBox>
<asp:AutoCompleteExtender ID="txtVehicleNo_AutoCompleteExtender" runat="server" DelimiterCharacters=""
Enabled="True" ServiceMethod="GetVehicleList" ServicePath="" TargetControlID="txtVehicleNo"
UseContextKey="True" MinimumPrefixLength="2">
</asp:AutoCompleteExtender>
</td>
</tr>
</table>


Now Code in ASPX.CS Page.
Add these Namespages for below Code.

using System.Data; // For Dataset
using System.Data.SqlClient; // For SQlConnection/Adaptor
using System.Configuration; // Access Consting from Web.Config.
using System.Collections.Generic;

[System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]

public static string[] GetVehicleList(string prefixText, int count, string contextKey)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConToJohn"].ConnectionString);
SqlDataAdapter adp = new SqlDataAdapter("Select distinct VehicleNo from VehicleDispatch where VehicleNo like '%" + prefixText + "%'", con);
DataSet ds = new DataSet();
adp.Fill(ds, "Vehicles");
string[] VehicleNo = new string[ds.Tables["Vehicles"].Rows.Count];
for (int i = 0; i <= ds.Tables["Vehicles"].Rows.Count - 1; i++)
{
VehicleNo.SetValue(ds.Tables["Vehicles"].Rows[0][0].ToString(), i);
}
return VehicleNo;
}


I have Selected Only Unique Value from Database using SELECT DISTINCT .... command. If you want all records, Simply Change SQL Query to SELECT .....

Will be glad to hear from you...

John Bhatt
Glad to Know, Free to Share.....
http://www.johnbhatt.com
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/25/2013 8:49:37 PM