C# 获取列表web方法并在列表框中显示数据

C# 获取列表web方法并在列表框中显示数据,c#,asp.net,sql,web-services,webmethod,C#,Asp.net,Sql,Web Services,Webmethod,我正在尝试在asp.net的列表框中显示日期。我有下面的web方法,它在输入学生ID时起作用。然后如何在我的页面中调用此web方法,以便在列表框中仅显示日期 [WebMethod] public List<Attendance> StudentAttendance(int SID) { List<Attendance> listofAttendance = new

我正在尝试在asp.net的列表框中显示日期。我有下面的web方法,它在输入学生ID时起作用。然后如何在我的页面中调用此web方法,以便在列表框中仅显示日期

 [WebMethod]
                public List<Attendance> StudentAttendance(int SID)
                {

                    List<Attendance> listofAttendance = new List<Attendance>();
                    cn.Open();
                    SqlCommand com = new SqlCommand("SELECT * FROM tblAttendance WHERE StudentID = " + SID +"", cn);
                    SqlDataReader sr = com.ExecuteReader();
                    while (sr.Read())
                    {
                        Attendance getdattendance = new Attendance();
                        getdattendance.ID = sr.GetInt32(0);
                        getdattendance.StudentID = sr.GetInt32(1);
                        getdattendance.RegistrationDate = sr.GetDateTime(2);

                        listofAttendance.Add(getdattendance);
                    }
                    sr.Close();
                    cn.Close();
                    return listofAttendance;

                }

您需要像这样修改web方法中的注册日期格式

 getdattendance.RegistrationDate =Convert.ToDateTime(sr.GetDateTime(2).ToString("dd MM yyyy"));
然后在页面加载中,您需要将列表框绑定到此考勤列表

List<Attendance> attendances = attend.StudentAttendance(s.StudentID); ;
listbox1.DataSource = attendances;
listbox1.DataValueField = "Id";
listbox1.DataTextField = "RegistrationDate";
listbox1.DataBind();
列出出席人数=出席。学生出席人数(s.StudentID);
listbox1.DataSource=出席人数;
listbox1.DataValueField=“Id”;
listbox1.DataTextField=“注册日期”;
listbox1.DataBind();

我收到错误“无法将类型“ServiceReference1.Attention[]”隐式转换为“Studention.Attention[]”您需要使用列表中的ServiceReference考勤类
List<Attendance> attendances = attend.StudentAttendance(s.StudentID); ;
listbox1.DataSource = attendances;
listbox1.DataValueField = "Id";
listbox1.DataTextField = "RegistrationDate";
listbox1.DataBind();