I'd be glad if someone could help with how to write a code for autocomplete TextBox in asp.vb.net.
I'm able to do that in VB.Net Windows application, but unable to do same in the Web Application. The following is what I've tried under Page_load:

 Try
        cmd = New SqlCommand("Select SubjectName from ProgramDetails.Subjects", cn)
        dr1 = cmd.ExecuteReader
        While dr1.Read
            txtSearch.AutoCompleteType.GetType.Name.Trim(dr1(0))
        End While
        dr1.Close()
        dr1 = Nothing
    Catch ex As Exception
        MsgBox(ex.Message)
    Finally
    End Try
Amitnamdeo commented: how to wite this code in vb.net +0

Recommended Answers

All 14 Replies

I'm moving this from vb.net to ASP.net

commented: Thanks eakaglo, It appears your code could work perfectly but how to re-code it in VB.Net is my problem. +0
Member Avatar for LastMitch

I'd be glad if someone could help with how to write a code for autocomplete TextBox in asp.vb.net.

This is an tutorial on how to write a simple Auto-Complete TextBox:

http://www.asp.net/ajaxlibrary/act_AutoComplete_Simple.ashx

WeLL I usually code in C#.Net .. I have this code working.. You can convert and modify it accordingly..
I have use ajax autocomplete extender to achieve the functionality...

Include these namespaces:

using System.Collections.Generic;
using System.Data.SqlClient;

and use the following code:

    [System.Web.Script.Services.ScriptMethod()]
    [System.Web.Services.WebMethod]
    public static List<string> SearchChassis(string prefixText, int count)
    {
      using (SqlConnection conn = new SqlConnection())
       {
        conn.ConnectionString = ConfigurationManager
                .ConnectionStrings["ConnectionString"].ConnectionString;
        using (SqlCommand cmd = new SqlCommand())
        {
            cmd.CommandText = "Select ChassisNo from tblVehicle where "  +
            "ChassisNo like @SearchText + '%'";
            cmd.Parameters.AddWithValue("@SearchText", prefixText);
            cmd.Connection = conn;
            conn.Open();
            List<string> chassis = new List<string>();
            using (SqlDataReader sdr = cmd.ExecuteReader())
            {
                while (sdr.Read())
                {
                    chassis.Add(sdr["ChassisNo"].ToString());
                }
            }
            conn.Close();
            return chassis;
          }
        }

Thanks Vishal, It appears your code could work perfectly but how to re-code it in VB.Net is my problem.

I've tried to follow Mudassar's example on the link I'm still unable to resolve the problem.

I'm Visual Studio 2008 and for that matter I downloaded and installed Ajax ToolKit 3.5. With my TextBox on the form and having dragged and dropped ScriptManager onto the form, I was unable to drag the AutoCompleteExtender on the form, but was able to drag it to the ASPX code view of the form and scrictly followed your example. However, even though I don't get any error when the program is run autocomplete textbox doesn't work.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Subjects.aspx.vb" Inherits="Subjects" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>



<%@ Register assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI" 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>Subject Master</title>
    <style type="text/css">
        .style2
        {
        }
        .style4
        {
            width: 297px;
        }
        .style6
        {
            width: 268px;
        }
        .style3
        {
            height: 23px;
            width: 146px;
        }
        .style5
        {
            height: 23px;
            width: 297px;
        }
        .style1
        {
            height: 23px;
            width: 268px;
        }
        .style7
        {
        }
    </style>
</head>

<body>
    <form id="frmSubjects" runat="server">
    <asp:Panel ID="Panel1" runat="server" BackColor="#FFFF99" BorderColor="#00CC00" 
        BorderStyle="Double" Height="721px">
        <div>

        </div>
        <p>
            <asp:TextBox ID="txtIdNumber" runat="server" Visible="False" 
            Width="26px"></asp:TextBox>
        </p>
        <table style="width: 66%; height: 552px;" align="center">
            <tr>
                <td class="style2" colspan="2">
                    Subject Code</td>
                <td class="style4" colspan="2">
                    <asp:TextBox ID="txtSubjectCode" runat="server"></asp:TextBox>
                </td>
                <td class="style6" colspan="2">
                    <asp:TextBox ID="txtSearch" runat="server" Width="169px"></asp:TextBox>
                       <cc1:autocompleteextender ServiceMethod="SearchSubjects"
                        ID="Autocompleteextender1"
                        MinimumPrefixLength="1"
                        CompletionInterval="100" EnableCaching="False" CompletionSetCount="10" 
                        TargetControlID="txtSearch"
                        runat="server">
                        </cc1:autocompleteextender>                                         
                </td>
            </tr>
            <tr>
                <td class="style3" colspan="2">
                    Subject Name</td>
                <td class="style5" colspan="2">
                    <asp:TextBox ID="txtSubjectName" runat="server" Width="270px"></asp:TextBox>
                </td>
                <td class="style1" colspan="2">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2" colspan="2">
                    Subject Type</td>
                <td class="style4" colspan="2">
                    <asp:RadioButton ID="rbnCore" runat="server" Text="Core" />
                    <asp:RadioButton ID="rbnElective" runat="server" Text="Elective" />
                </td>
                <td class="style6" colspan="2">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2" colspan="2">
                    Enter User Name</td>
                <td class="style4" colspan="2">
                    <asp:TextBox ID="txtUserId" runat="server"></asp:TextBox>
                </td>
                <td class="style6" colspan="2">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style2" colspan="2">
                    Enter Password</td>
                <td class="style4" colspan="2">
                    <asp:TextBox ID="txtPassword" runat="server"></asp:TextBox>
                </td>
                <td class="style6" colspan="2">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style7">
                    <asp:Button ID="btnSave" runat="server" style="text-align: center" Text="Save" 
                    Width="93px" />
                </td>
                <td class="style2" colspan="2">
                    <asp:Button ID="btnClear" runat="server" Text="Refresh" Width="92px" />
                </td>
                <td class="style2" colspan="2">
                    <asp:Button ID="btnUpdate" runat="server" Text="Update" Width="96px" />
                </td>
                <td class="style2">
                    <asp:Button ID="btnDelete" runat="server" Text="Delete" Width="97px" />
                </td>
            </tr>
            <tr>
                <td class="style7">
                    &nbsp;</td>
                <td class="style2" colspan="2">
                    &nbsp;</td>
                <td class="style2" colspan="2">
                    &nbsp;</td>
                <td class="style2">
                    &nbsp;</td>
            </tr>
            <tr>
                <td class="style7" colspan="6">
                    <asp:Label ID="lblMessage" runat="server" 
                    style="text-align: center" Text="Label"></asp:Label>
                </td>
            </tr>
            <tr>
                <td class="style7" colspan="6">
                    <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="True">  
                    </asp:ScriptManager>
                </td>
            </tr>
            <tr>
                <td class="style7" colspan="6">

                    <asp:Panel ID="Panel2" runat="server" Height="352px" Width="615px" 
                        ScrollBars="Both">

                        <asp:GridView ID="GridView1" runat="server" CellPadding="2" ForeColor="#333333" 
                            GridLines="None" style="text-align: left; top: 1px; left: 180px;" Width="600px">
                            <RowStyle BackColor="#E3EAEB" />
                            <EmptyDataRowStyle BorderStyle="Solid" />
                            <FooterStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                            <PagerStyle BackColor="#666666" ForeColor="White" HorizontalAlign="Center" />
                            <SelectedRowStyle BackColor="#C5BBAF" Font-Bold="True" ForeColor="#333333" />
                            <HeaderStyle BackColor="#1C5E55" Font-Bold="True" ForeColor="White" />
                            <EditRowStyle BackColor="#7C6F57" />
                            <AlternatingRowStyle BackColor="White" />
                        </asp:GridView>
                        </asp:Panel>
                </td>
            </tr>
        </table>
    </asp:Panel>
        <div>
     </div>

    </form>
</body>
</html>




<System.Web.Script.Services.ScriptMethod(), System.Web.Services.WebMethod()> _
  Public Shared Function SearchSubjects(ByVal prefixText As String, ByVal count As Integer) As List(Of String)
        Dim conn As SqlConnection = New SqlConnection
        conn.ConnectionString = ConfigurationManager _
             .ConnectionStrings("Data Source=EBENEZERAKAGLO\SQLEXPRESS;Initial Catalog=Naass;Integrated Security=True").ConnectionString
        Dim cmd As SqlCommand = New SqlCommand
        cmd.CommandText = "select SubjectName from ProgramDetails.Subjects where" & _
            " SubjectName like @txtSearch + '%'"
        cmd.Parameters.AddWithValue("@txtSearch", prefixText)
        cmd.Connection = conn
        conn.Open()
        Dim Subjects As List(Of String) = New List(Of String)
        Dim sdr As SqlDataReader = cmd.ExecuteReader
        While sdr.Read
            Subjects.Add(sdr("SubjectName").ToString)
        End While
        conn.Close()
        Return Subjects
    End Function

It seems like you have some issue with the AJAX version in web.config.. You have to modify your web.config as well to make ajax auto complete extender work..

Please Paste your web.config as well..

Please I don't quite get you. What Should I do specifically in my web.config?

what version of visual studio are u using?

Visual Studio 2008 Professional Edition

Thanks for providing the code, it helped me in transferring my project from VB.Net to Asp.Net.

JQuery with Ajax has simplified Autocomplete textbox. No need to write too many codes at server side. Though WebService is outdated, it still helps in many ways, if you want to keep it simple and light weight. But yes, just be a little carefull with the business logics you write.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.