Monday, March 26, 2012

Populate a ListBox based on database search

I am trying to populate a ListBox with values retrieved from a database based on a search. I have a TextBox in which the user will enter the text they wish to search for, a Button which will submit the text, and then a ListBox which is invisible until values are returned from the database. I have a SqlDataSource with a SelectCommand saying "SELECT * FROM [table] WHERE [name] LIKE '@dotnet.itags.org.Parameter'". I want to know how to bind the value in the TextBox to the "@dotnet.itags.org.Parameter" in the Sql statement. I am quite new at programming, and I'm sure this is quite a simple task. Any help would be appreciated.

MYSQL:

myCommand.Parameters.Clear();
myCommand.CommandText = "SELECT * FROM [table] WHERE [name] LIKE (?)";
myCommand.Parameters.Add(new OdbcParameter(null, txtSearch.Text));

MSSQL:

myCommand.Parameters.Clear();
myCommand.CommandText = "SELECT * FROM [table] WHERE [name] LIKE @.search";
myCommand.Parameters.Add(new SqlParameter("@.search", txtSearch.Text));


Thanks for the reply. Where would I put this code? I'm using MSSql, but I am not using any stored procedures. All my SQL code is on my .aspx page. Should this go somewhere on my .aspx page or in my VB codebehind?

This would go in your code behind, in the onclick method from your button (double click your submit button and you'll go automatically to your onclick method).

Remember the code I gave in the example is C# code, I don't know the correct syntax in VB.net but it should get you started.


This should help:

http://www.asp.net/learn/dataaccess/default.aspx?tabid=63

Cheers

0 comments:

Post a Comment