I'm designing a C# Form program that uses a listbox to display search results from a SQL database. The search function works but now I want to be able to select one of the listbox rows and load the selected data (which is from the SQL database) to a new form. The program keeps track of clients at our company. The listbox gets populated when the user types in some criteria. I'm new to SQL and C# form design, so any help would be great. I left a screenshot below of the listbox and search boxes.
I had a quick search many month ago.This is my code.I hope it help you:
It is a full code of my stored procedure in SQL:
if you don't know about stored procedure you can search about it.
and it's my C# code to send data to the stored procedure and get data form it:
and you should change the
SqlConnection connection = new SqlConnection("Data Source=(local);Initial Catalog=Khane;Integrated Security=True");
using your database's dataand it is my code for showing the data in a ListView:
and my
Common.CommonPersonSerchResult
was just a class of properties.be careful It was my code and you should change it as you need to use it in your project
for showing data in new form you can save the data that you got from DB in listbox's tag and after that get the data in new form's constructor and work with it in you new form.It is so easy.To make a new form you can work with Listbox's selected change event like this:
this code was for a ListView and I think there is just a SelectedItem in ListBox and your method will be like this:
and you should give to list box your Data as a listbox's Item and you should override .ToString() method of you Data calss to Show the your needed data in ListBox.