In this article we will discuss, how to use ExecuteReader() method in ASP.Net c#. When we want to return more than a single value by using ExecuteReader().
Example:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//Create the connection object
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["ShoppingZone"].ConnectionString); ;
try
{
// Pass theconnection to the command object, so the command object knows on which
// connection toexecute the command
SqlCommand cmd = new SqlCommand("Select * from employee", connection);
// Open theconnection. Otherwise you get a runtime error. An open connection is
// required toexecute the command
connection.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
}
catch (Exception ex)
{
// HandleExceptions, if any
}
finally
{
// The finallyblock is guarenteed to execute even if there is an exception.
// This ensures connections are always properlyclosed.
connection.Close();
}
}
}
Post your comments / questions
Recent Article
- Requested setting INSTALLED_APPS, but settings are not configured. You must either define..
- ValueError:All arrays must be of the same length - Python
- Check hostname requires server hostname - SOLVED
- How to restrict access to the page Access only for logged user in Django
- Migration admin.0001_initial is applied before its dependency admin.0001_initial on database default
- Add or change a related_name argument to the definition for 'auth.User.groups' or 'DriverUser.groups'. -Django ERROR
- Addition of two numbers in django python
- The request was aborted: Could not create SSL/TLS secure channel -Error in Asp.net
Related Article