I want to show Components in a tabs , so first of all create few components. In this project we have three components, First View Component public class AllViewComponent : ViewComponent { private readonly UserManager<ApplicationUser> _userManager; public AllViewComponent(UserManager<ApplicationUser> userManager) { _userManager = userManager; } public async Task<IViewComponentResult> InvokeAsync() { List<StudentViewModel> allUsers = new List<StudentViewModel>(); var items = await _userManager.Users.ToListAsync(); foreach (var item in items) { allUsers.Add(new StudentViewModel {Id=item.Id, EnrollmentNo = item.EnrollmentNo, FatherName = item.FatherName, Name = item.Name, Age = item.Age, Birthdate = item.Birthdate, Address = item.Address, Gender = item.Gender, Email = item.Email }); }
Introduction
In this article i will show you, how to retrieve data from database table using DataReader. I will give a example of data fetching from database table using SqlDataReader class, also bind the GridView with the DataReader.
SqlDataReader
Provides a way of reading a forward-only stream of rows from a SQL Server database ( source from msdn.microsoft.com)
Example How to bind gridview using SqlDataReader class
Step-1 : Take a Gridview onto design window.
Step-2 : Develop code for binding gridview using SqlDataReader.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class datareadercl : System.Web.UI.Page
{
SqlDataReader rd;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString =ConfigurationManager.ConnectionStrings ["ConnectionString"].ToString ();
con.Open ();
using(SqlCommand cmd=new SqlCommand ())
{
cmd.CommandText ="select * from [Table]";
cmd.Connection =con;
rd=cmd.ExecuteReader();
GridView1 .DataSource =rd;
GridView1 .DataBind ();
}
}
}
}
}
OutPut of the program
Read() method of SqlDataReader class : read next record
In this article i will show you, how to retrieve data from database table using DataReader. I will give a example of data fetching from database table using SqlDataReader class, also bind the GridView with the DataReader.
SqlDataReader
Provides a way of reading a forward-only stream of rows from a SQL Server database ( source from msdn.microsoft.com)
Example How to bind gridview using SqlDataReader class
Step-1 : Take a Gridview onto design window.
Step-2 : Develop code for binding gridview using SqlDataReader.
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class datareadercl : System.Web.UI.Page
{
SqlDataReader rd;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
using (SqlConnection con = new SqlConnection())
{
con.ConnectionString =ConfigurationManager.ConnectionStrings ["ConnectionString"].ToString ();
con.Open ();
using(SqlCommand cmd=new SqlCommand ())
{
cmd.CommandText ="select * from [Table]";
cmd.Connection =con;
rd=cmd.ExecuteReader();
GridView1 .DataSource =rd;
GridView1 .DataBind ();
}
}
}
}
}
OutPut of the program
Read() method of SqlDataReader class : read next record
- Here Example of Read method which is read table row one by one.
Comments
Post a Comment