The following will just print out 1 row from the database
public partial class Default : System.Web.UI.Page
{
SqlConnection connection;
SqlCommand command;
SqlDataReader reader;
protected void Page_Load(object sender, EventArgs e)
{
using (connection = new SqlConnection(ConfigurationManager.AppSettings["connString"]))
{
using (command = new SqlCommand("select col1, col2 from table1 where id = @id", connection))
{
command.Parameters.Add("@id", SqlDbType.Int, 3).Value = 1;
connection.Open();
using (reader = command.ExecuteReader())
{
reader.Read();
Div1.InnerHtml = reader["col1"].ToString();
}
}
}
}
}
What needs to be done here so it prints out all rows returned?
Here:
Also since you are instantiating your connection objects inside the method you don’t really need to make them as private fields: