Simple ASP.NET Database Query

10,018

Solution 1

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 Filtering Data Using Data Source Controls for more detail.

Solution 2

<%
    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.

Share:
10,018
Steven
Author by

Steven

...

Updated on June 04, 2022

Comments

  • Steven
    Steven almost 2 years

    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.