Radiant Software

Radiant Software

  • NA
  • 72
  • 30.4k

Listbox

Aug 31 2015 3:23 AM
hii
  i have create one web form.
in that form i had added one textbox n one listbox.
On page load listbox display all client name from database in lixtbox.
Now I want to display--
1- while i type '1' in textbox then listbox only display clientname of '1' clientid.
2-while i typing name starting from 'j' then list box displays all cliename which matches to input that i i typing in textbox.
anybody can help me?
please. 

Answers (4)

1
Aman

Aman

  • 0
  • 4.4k
  • 1.5m
Aug 31 2015 6:25 AM

Attachment: dbsolve31_08.rar

Hi , 
You can do this as well. However I have implemented in a winForm but you can apply same concept on your web form.

just inside the textChange Event of TextBox add the following Line of Code 

private void txtBoxInput_TextChanged(object sender, EventArgs e)
{
int employeeID;
string employeeName = txtBoxInput.Text;
if (Int32.TryParse(employeeName, out employeeID))
{
IDSearch(employeeID);
}
else
{
NameSearch(employeeName);
}

}
 
 Now in Both the Method we have used add the following line of code :
public void NameSearch(string name)
{
string connString = ConfigurationManager.ConnectionStrings["EMSYSDbConn"].ToString();
SqlConnection conn = new SqlConnection(connString);
string query = String.Format("SELECT EmployeeName FROM Employee WHERE EmployeeName like '{0}%'",name);
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);
listBoxData.DataSource = dataTable;
listBoxData.DisplayMember = "EmployeeName";
}
public void IDSearch(int employeeID)
{
string connString = ConfigurationManager.ConnectionStrings["EMSYSDbConn"].ToString();
SqlConnection conn = new SqlConnection(connString);
try
{
string query = String.Format("SELECT EmployeeName FROM Employee WHERE EmployeeID={0}", employeeID);
SqlCommand cmd = new SqlCommand(query, conn);
SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
DataTable dataTable = new DataTable();
dataAdapter.Fill(dataTable);
listBoxData.DataSource = dataTable;
listBoxData.DisplayMember = "EmployeeName";
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Note :Here I have used different Database for my project 

If you have any query feel free to ask, and accept it as answer if it helps
0
Radiant Software

Radiant Software

  • 0
  • 72
  • 30.4k
Sep 3 2015 3:34 AM
Thanku Aman...
I have imlement this code.. this is working.
But i want to search item alphabetically...Means When I type 'P' in textbox then listbox will show all the items which are starting from 'P'..then nxt i'll type 'a' then  istbox will show all the items which containing starting from'Pa'...
help me Plz 
0
Sreeni Dony

Sreeni Dony

  • 0
  • 18
  • 23.8k
Aug 31 2015 7:05 AM
By using linq we can filter the search item
eg:from item in items where item.id contains(searchItem) 
0
Khan Abrar Ahmed

Khan Abrar Ahmed

  • 0
  • 4.9k
  • 969.4k
Aug 31 2015 4:33 AM
Hi try this links
 
https://joshsmithonwpf.wordpress.com/2007/06/12/searching-for-items-in-a-listbox/
http://www.c-sharpcorner.com/Forums/Thread/71890/-search-a-list-box-while-typing-in-a-text-box-in-winforms.aspx