views:

52

answers:

3

I have loaded my database with one table under "Data Connections" in the "Server Explorer" pane.

What is the standard / best-practices way to handle a simple query in a VB ASPX page?

My left <div> would be a set of form elements to filter rows, and when the button is clicked, the main <div> would show the columns I want for the rows returned.

Note: Answers in C# are okay too, I'll just translate.

+1  A: 
<%
    using(SqlConnection conn = new SqlConnection(someConnectionString))
    {
        SqlCommand command = new SqlCommand("select * from table", conn);

        DataTable results = new DataTable();

        SqlDataAdapter adapter = new SqlDataAdapter(command);

        conn.Open();

        adapter.Fill(results, command);
    }

    // You can work with the rows in the DataTable here
%>

Would work if you're trying to do everything in the page code.

I would suggest using the Code-Behind file and working with code that way. It makes things easier to understand when your code is in a seperate file leaving markup in one spot and code in the other.

Justin Niessner
A: 

There are lots of approaches to this and what is "best" depends on your scenario. This might be a good starting point for you to try doing this with a gridview: http://www.c-sharpcorner.com/UploadFile/paulabraham/populatingdatagrid02092007022124AM/populatingdatagrid.aspx

AaronLS
+1  A: 

Presuming web forms you would have a data control, such as a gridview or repeater, bound to a datasource (such as SqlDataSource or ObjectDataSource etc). You would then bind the parameters of your filter controls as control parameters of your datasource. You then need a button to fire a postback and that's basically it.

See http://msdn.microsoft.com/en-us/library/ms227680.aspx for more detail.

Dan Diplo