Hi nauna,
Refer the below code.
public List<Country> GetCountry()
{
string sqlStatment = "select countryid, countryname from country";
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(constr))
{
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlStatment, con))
{
cmd.Connection.Open();
System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
List<Country> emp = new List<Country>();
while (reader.Read())
{
Country country = new Country();
country.countryid = Convert.ToInt32(reader.GetValue(0));
country.countryname = reader.GetValue(1).ToString();
emp.Add(country);
}
reader.Close();
cmd.Connection.Close();
return emp;
}
}
}
public class Country
{
public int countryid { get; set; }
public string countryname { get; set; }
}
OR
public List<Country> GetCountry()
{
string sqlStatment = "select countryid, countryname from country";
string constr = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
using (System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(constr))
{
using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(sqlStatment, con))
{
cmd.Connection.Open();
System.Data.SqlClient.SqlDataReader reader = cmd.ExecuteReader();
List<Country> emp = new List<Country>();
while (reader.Read())
{
Country country = new Country
{
countryid = Convert.ToInt32(reader.GetValue(0)),
countryname = reader.GetValue(1).ToString()
};
emp.Add(country);
}
reader.Close();
cmd.Connection.Close();
return emp;
}
}
}
public class Country
{
public int countryid { get; set; }
public string countryname { get; set; }
}
You are returning List<Country> So your method should returns List<Country>.