0
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.

2
  • 3
    How will you fit many rows in only one label? Commented Jan 3, 2014 at 5:49
  • It will overwrite the values and you will get the last row only. Commented Jan 3, 2014 at 5:51

4 Answers 4

5

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();
Sign up to request clarification or add additional context in comments.

Comments

1
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();

Comments

0
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);
       }
    }

1 Comment

Could you add some comments as to what your code does and how it helps to answer the question.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.