0
내 프로젝트의 주소 부분에 "패키지 프로그램"을 만들고 싶습니다. 거의 모든 프로젝트에서 필요하므로 쉽게 만들길 원했습니다.클래스로 드롭 다운 목록 채우기
그래서 도시 이름을 드롭 다운 목록에로드하는 클래스를 만들기로 결정했습니다. 여기에 코드 :
public class Address
{
string connStr = "Data Source...";
public int id { get; set; }
public string name { get; set; }
public Address(int ID, string Name)
{
this.id = ID;
this.name = Name;
}
public List<Address> LoadCities()
{
List<Address> cities = new List<Address>();
SqlConnection con = new SqlConnection(connStr);
SqlCommand cmd = new SqlCommand("select x,y from ...", con);
SqlDataReader rdr = cmd.ExecuteReader();
while (rdr.Read())
{
Address city = new Address(rdr.GetInt32(0), rdr.GetString(1));
cities.Add(city);
}
con.Close();
return cities;
}
}
이것은 내 패키지 프로그램입니다. 내 프로젝트에 대한 참조로 추가했습니다. 그리고 다음과 같이 내 드롭 다운 목록을 채우려고했습니다 :
List<Address> cities = ???
ddlCity.DataTextField = "x";
ddlCity.DataValueField = "y";
ddlCity.DataSource = cities;
ddlCity.DataBind();
?? 위치 난 그냥 이런 식으로 코드 싶어 : List cities = Address.Loadcities();
그래서 여기에 실수가 있습니다. 왜냐하면 나는 "수업"에서 새로운 일을하기 때문입니다.
미리 감사드립니다.
u를 감사 :
가 여기에 잠을 잘 수가 없어 그 시간에 대한 몇 가지 문서입니다. 그게 내가 필요한 전부 였어. 다시 (링크 너무) 주셔서 감사합니다 :) –