Hi pramshi,
Refer below sample.
Database
I have made use of the following table Customers with the schema as follows.
I have already inserted few records in the table.
You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
<asp:ScriptManager runat="server"></asp:ScriptManager>
<ajaxToolkit:ComboBox ID="cbCustomers" runat="server" AutoCompleteMode="SuggestAppend"></ajaxToolkit:ComboBox>
<asp:Button ID="btnSet" runat="server" Text="Set Value" />
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
$('[id*=btnSet]').on('click', function () {
$find("<%= cbCustomers.ClientID %>").get_textBoxControl().value = "Mudassar Khan";
return false;
});
});
</script>
Namespaces
C#
using System.Data;
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT CustomerId, Name FROM Customers"))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
cbCustomers.DataSource = cmd.ExecuteReader();
cbCustomers.DataTextField = "Name";
cbCustomers.DataValueField = "CustomerId";
cbCustomers.DataBind();
con.Close();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT CustomerId, Name FROM Customers")
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
cbCustomers.DataSource = cmd.ExecuteReader()
cbCustomers.DataTextField = "Name"
cbCustomers.DataValueField = "CustomerId"
cbCustomers.DataBind()
con.Close()
End Using
End Using
End If
End Sub
Screenshot