Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading Data from SQL DataBase Table to generic collection

Tags:

c#

I wanted to read all data from a table(containg 3 rows) and to add all the data into generic collection.From collection i wana bind to gridview.

The code displayed below works but only last row is displayed 3 times in gridview.Can You help me.Am a beginer

protected void Page_Load(object sender, EventArgs e)
{
    List<Student> listid = new List<Student>();
    Student stud = new Student();
    SqlConnection con = new SqlConnection("........");
    string sql = "select * from StudentInfo";
    con.Open();
    SqlCommand cmd = new SqlCommand(sql, con);
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        stud.Studid = Convert.ToInt32(dr["StudId"]);
        stud.StudName = dr["StudName"].ToString();
        stud.StudentDept = dr["StudentDept"].ToString();
        listid.Add(stud);               
    }
    GridView1.DataSource = listid;
    GridView1.DataBind();
}
public class Student
{
    private int studid;
    public int Studid
    {
       get { return studid; }
       set { studid = value; }
    }    
    private string studName;
    public string StudName
    {
       get { return studName; }
       set { studName = value; }
    }
    private string studentDept;
    public string StudentDept
    {
       get { return studentDept; }
       set { studentDept = value; }
    }

The output is like this:

enter image description here

like image 924
user2115618 Avatar asked Apr 01 '13 03:04

user2115618


2 Answers

In the DataReader while loop instantiate a new Student for each row in the database table:

while (dr.Read())
{
 var stud = new Student();
 stud.Studid = Convert.ToInt32(dr["StudId"]);
 stud.StudName = dr["StudName"].ToString();
 stud.StudentDept = dr["StudentDept"].ToString();
 listid.Add(stud);
}
like image 181
Jeremy Thompson Avatar answered Oct 29 '22 07:10

Jeremy Thompson


You need to instantiate your object inside while loop
Otherwise you will have same data in the collection
So the code should be

protected void Page_Load(object sender, EventArgs e)
{
    List<Student> listid = new List<Student>();
    SqlConnection con = new SqlConnection("........");
    string sql = "select * from StudentInfo";
    con.Open();
    SqlCommand cmd = new SqlCommand(sql, con);
    SqlDataReader dr = cmd.ExecuteReader();
    while (dr.Read())
    {
        Student stud = new Student();
        stud.Studid = Convert.ToInt32(dr["StudId"]);
        stud.StudName = dr["StudName"].ToString();
        stud.StudentDept = dr["StudentDept"].ToString();
        listid.Add(stud);               
    }
    GridView1.DataSource = listid;
    GridView1.DataBind();
}

Also it is not a good practice to use while to data reader or directly open connection
You should use using statement.

using(SqlConnection con = new SqlConnection("connection string"))
{

    con.Open();

    using(SqlCommand cmd = new SqlCommand("SELECT * FROM SomeTable", connection))
    {
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            if (reader != null)
            {
                while (reader.Read())
                {
                    //do something
                }
            }
        } // reader closed and disposed up here

    } // command disposed here

} //connection closed and disposed here
like image 25
शेखर Avatar answered Oct 29 '22 08:10

शेखर