我在下面收到Index out bounds error
一个FullName property
。我有我的persondetails
班级和data
班级,我正在SqlDataReader
尝试调用这个属性。firstname
和值是使用存储过程返回的lastname
,然后我想创建一个属性来连接这两个,并且能够调用FullName
我的存储过程。
persondetails
班级
private string firstName;
private string lastName;
private string fullName;
public string FirstName
{
get { return firstName;}
set { set firstName = value;}
}
public string LastName
get { return lastName; }
set { set lastName = value; }
public string FullName
get { return lastName + "," + firstName;
}
public persondetails(string lastName, string firstName)
{
this.lastName = lastName;
this.firstName = firstName;
}
data
班级
public List<Persondetails> getFullName()
{
// Do my sql connection stuff
List<Persondetails> persondetails = new List<Persondetails>();
Persondetails person;
try
{
// open connection
// specify "stored Procedure" (which returns firstname and lastname)
using (SqlDataReader reader = cmd.ExecuteReader())
{
while (reader.Read())
{
person = new Persondetails((
reader.GetString(reader.GetOrdinal("LASTNAME")));
reader.GetString(reader.GetOrdinal("FIRSTNAME")));
persondetails.Add(person);
}
reader.Close();
return persondetails;
}
// the rest of my method
存储过程只是从我的表中返回lastname
和返回,firstname
该表有 2 个单独的字段。我不想在这里进行连接,我想在我的属性中进行连接。
已编辑***工作代码