Link to home
Create AccountLog in
Avatar of J_Kogan

asked on

How to populate list box from stored procedure with one parameter?

I have a stored procedure – “SP_Load_ListBox” and one parameter in this stored procedure- @Param.
This stored procedure select two fields – FieldForDataTextfield – field for data text field of my list box and FieldForDataValueField – field for data value field of my list box.
User must enter the parameter value in the textbox1, click button and list box must be populated from this stored procedure with parameter.  
How can I populate my list box from this store procedure with parameter (data text field and data value field of my list box)?

Avatar of ErezMor
Flag of Australia image

if you go to "configure data source..." of the listbox, it will take you along a wizard to state the stored proc name, the parameter, and let you set the parameter's source to be a control - which you should set to your textbox.

the point to remember here is that it will run BEFORE the page loads - which means your user didnt have the chance to enter data yet... and therefore - set a default value (in the same step where you connect to the control) - you can set it to something that wouldnt yield any results if you want the list to be empty before the user selected anything
Avatar of J_Kogan


I am sorry, I don't have a wizard, I use VS 2003/VB.NET
Can you give me a code sample?  I need to bind  listbox with store proc.
Avatar of rajeeshmca
Flag of India image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of J_Kogan


Thank you very much.
It works, but in VS2003 I don’t have AddWithValue method, I put:
cmdListBox.Parameters.Add(New SqlParameter("@POST_CODE", SqlDbType.NVarChar, 4))
cmdListBox.Parameters("@POST_CODE").Value = TextBox1.Text

This code didn’t work:
 Dim objDataTable As New DataTable
ddl.DataSource = objDataTable

I put:
Dim objDataTable As New DataTable
sqaListBox.Fill(dstListBox, "objDataTable")  'dstListBox-dataset
Dim tbTable As New DataTable
tbTable = dstListBox.Tables(0)
lstAppYear.DataSource = tbTable

All the rest code is OK,
Thank you