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
- ImportError: cannot import name 'url' from 'django.conf.urls' - Django Error
- How to Enable Virtualization in BIOS Security Settings in Intel Processors For Android Studio?
- Dependency 'androidx.activity:activity:1.8.0' requires libraries and applications that depend on it.
- AttributeError: 'NoneType' object has no attribute 'get_text' - Python
- ModuleNotFoundError: No module named 'openpyxl' - Python
- How to get thumbnail from vimeo video URL in Python?
- Remove all special characters, punctuation except spaces from string - Python
- OSError: cannot write mode RGBA as JPEG- Python
Related Article