I'm learning asp.net web forms, and I got stuck on a problem.
Instead of providing what I've tried, I'll tell you what I have that is working, and what I am trying to do that is not working! This is what I have and is working:
SqlConnection sqlConn = new SqlConnection(connstr);
sqlConn.Open();
string sqlComm = "SELECT * FROM bookShop";
SqlCommand comm = new SqlCommand(sqlComm, sqlConn);
SqlDataReader reader = comm.ExecuteReader();
while (reader.Read())
So after this, I just dynamically create some div's and other elements to populate my website, and this works fine, it reads the entire database and generates everything.
However, this was in my Page_Load function and now I am trying to modify it so that this becomes a method of a dataclass which will allow me to just call a method instead of typing all of this in page_load. So the idea here is to call a method that will retrieve all of my database records and put them in a List<T> from which I plan on creating the needed elements, and then the user should work with the string. After he/she finishes I will use the list to update the database. This is for making as little as possible reference to the actual base.
This is what I have in the method:
SqlDataReader reader;
Book temp = new Book();
connection.Open();
SqlCommand loading = new SqlCommand("SELECT * FROM bookShop", connection);
reader = loading.ExecuteReader();
while(reader.Read())
{
temp.ID = int.Parse(reader[0].ToString());
temp.bookName = reader[1].ToString();
temp.bookAuthor = reader[2].ToString();
temp.bookPrice = float.Parse(reader[3].ToString());
temp.coverPath = reader[4].ToString();
books.Add(temp);
}
connection.Close();
books is a List that I take as a parameter, but for some reason this code only populates the list with the last record of my database and I can't figure out why since its exactly the same as the above.