|
Hello All, I am testing three tier approach to display some records on asp.net page. For that i have created typed data set in visual studio and then i've created a .cs file in App_Code folder. Then am displaying the records on the .aspx page. SQL server is the database for the backend. Follwing is the code in app_code folder i.e. using System;using System.Data;using System.Configuration;using System.Linq;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.HtmlControls;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Xml.Linq;using System.Data.SqlClient;using System.Collections.Generic;using System.IO;public class Country{ public Country() { } private int _countryID; private string _countryName; public int CountryID{get{return _countryID;}set{_countryID = value;}} public string CountryName{get{return _countryName;}set{_countryName = value;}} public List<Country> AllCountries(){ Country objCountry = new Country(); SqlConnection objConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["empConnectionString"].ConnectionString); SqlCommand objCommand = new SqlCommand("select * from Country", objConnection);objCommand.CommandType = CommandType.Text;objConnection.Open(); List<Country> list = new List<Country>(); SqlDataReader objReader = objCommand.ExecuteReader(); while (objReader.Read()){ objCountry.CountryID = ( int)objReader["CountryID"];objCountry.CountryName = ( string)objReader["CountryName"];list.Add(objCountry); } return list;} } Now i am displaying thre records on an object data source at .aspx page. The problem is that it only displays the last row in the database repeatedly on the gridview control. Could any buddy sort out the problem Regards, Ghaffar |
|
| |
Bechir BejaouiPosted Dec 2, 2008, 4:01 AM
Ghaffar AbdulPosted Dec 2, 2008, 1:27 AM
using
System;using
System.Data;using
System.Configuration;using
System.Linq;using
System.Web;using
System.Web.Security;using
System.Web.UI;using
System.Web.UI.HtmlControls;using
System.Web.UI.WebControls;using
System.Web.UI.WebControls.WebParts;using
System.Xml.Linq;public
class AddCountry{
private int _countryID; private string _countryName; public int CountryID { get { return _countryID; } } public string CountryName { get { return _countryName; } } public AddCountry(int id, string name){
_countryID = id;
_countryName = name;
}
}
Then modifying the following code. I used this sample from a code written by Bill Jevens of microsoft on Personal Web Starter Kit.
using
(SqlDataReader reader = objCommand.ExecuteReader(CommandBehavior.CloseConnection)){ while (reader.Read()){ AddCountry temp = new AddCountry((int)reader["CountryID"], (string)reader["CountryName"]);list.Add(temp);}}
return list;Bechir BejaouiPosted Dec 1, 2008, 9:53 AM
while (objReader.Read())
{
objCountry.CountryID = (int)objReader["CountryID"];
objCountry.CountryName = (string)objReader["CountryName"];
list.Add(objCountry);
}
by
do
{
objCountry.CountryID = (int)objReader["CountryID"];
objCountry.CountryName = (string)objReader["CountryName"];
list.Add(objCountry);
objReader.NextResult();
}
while (objReader.Read())
Ghaffar AbdulPosted Dec 1, 2008, 6:30 AM
Bechir BejaouiPosted Dec 1, 2008, 5:19 AM
It is worth is your case to use a data adapter that fills a data set within the data and then you can use the cached data to leaverage what ever you want, if any changes are done then use adapter.update();