Reputation: 47
con.Open();
string query = "select Calf_ID,Plant,date1,Event from Holiday_Master ";
cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.Text;
dr = cmd.ExecuteReader();
while (dr.Read())
{
Label1.Text = dr["Calf_ID"].ToString();
Label2.Text = dr["Plant"].ToString();
Label3.Text = dr["date1"].ToString();
Label4.Text = dr["Event"].ToString();
}
con.Close();
I am using this code but it retrieves only one row from table I want all data from Table.
Upvotes: 0
Views: 68173
Reputation: 3050
On MVC5 you can use:
List<EntityName> varName = db.EntityName.ToList(); //This selects all rows from the table
Then you can iterate your list to display the info string/label
Upvotes: 0
Reputation: 1
Public class Employee
{
public int EmployeeId{get;set;}
public string Name{get;set;}
}
//ADO.NET CODES
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
string cs = ConfigurationManager.ConnectionString["DBCS"].ConnectionStrings;
using(SqlConnection con = ne SqlConnection(cs))
{
List<Employee> employee = new List<Employee>();
SqlCommand cmd = new SqlCommand("select * from Employee",con);
cmd.commandType = CommandType.Text;
con.Open();
SqlReader rdr = cmd.ExecuteReader();
while(rdr.Read())
{
Employee emp = new Employee();
emp.EmployeeId = Convert.ToInt32(rdr["EmployeeId"]);
emp.Name = rdr["Name"].ToString();
employee.Add(emp);
}
}
Upvotes: 0
Reputation: 2293
You can try a grid view
con.Open();
string query = "select Calf_ID,Plant,date1,Event from Holiday_Master ";
cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.Text;
using (SqlDataReader dr = cmd.ExecuteReader())
{
GridView1.DataSource = dr;
GridView1.DataBind();
}
con.Close();
Upvotes: 5
Reputation: 597
con.Open();
string query = "select Calf_ID,Plant,date1,Event from Holiday_Master ";
cmd = new SqlCommand(query, con);
cmd.CommandType = CommandType.Text;
dr = cmd.ExecuteReader();
while (dr.Read())
{
Label1.Text += dr["Calf_ID"].ToString();
Label2.Text += dr["Plant"].ToString();
Label3.Text += dr["date1"].ToString();
Label4.Text += dr["Event"].ToString();
}
con.Close();
Upvotes: 1