谁能帮我看下这段代码。。=w=不知道问题出在哪里
我前台有一个repeater控件,可是不知道为什么绑定后的数据会出现问题,数据都显示的相同一条,但我把sql语句放到数据库里面查的数据缺失不同的
我dal层这么写的
public static DataTable GetOvertimeSheet(int utype, int uid, int did)
{
object a = utype;
List<tb_OvertimeSheetTable> list = new List<tb_OvertimeSheetTable>();
using (SqlConnection con = new SqlConnection(DBhelper.connectionString))
{
DataSet ds = new DataSet();
string sql="select B.ID ,EmployeeID,B.SerialNumber,ExpectStartTime,ExpectEndTime,StartTime,EndTime,LeaveType,LeaveReason,ApproveStatus,ApplyLevel,LeaveStatus,B.UserID,LastUpdateTime,CreateTime,IsDel, T.Name,T.SerialNumber,T.PositionID,T.DepartmentID,T.UserID,T1.G_CName as DepartmentNa,T1.GroupID,T2.GroupID,T2.G_CName as Position,T3.U_Type from tb_OvertimeSheet as B INNER JOIN tb_EmployeesInfo as T On B.UserID = T.UserID INNER JOIN sys_Group as T1 On T.DepartmentID = T1.GroupID INNER Join sys_Group as T2 on T.PositionID =T2.GroupID INNER Join sys_User as T3 on B.UserID =T3.UserID where LeaveStatus = 0 and T3.U_Type between @Utype and @tys and b.UserID = @uid and T.DepartmentID = @did order by B.CreateTime DESC";
SqlCommand cmd = new SqlCommand(sql.ToString(), con);
con.Open();
cmd.Parameters.Add("@Utype", SqlDbType.TinyInt).Value = utype - 1;
cmd.Parameters.Add("@tys", SqlDbType.TinyInt).Value = utype;
cmd.Parameters.Add("@uid", SqlDbType.Int).Value = uid;
cmd.Parameters.Add("@did", SqlDbType.Int).Value = did;
SqlDataAdapter adp = new SqlDataAdapter(cmd);
adp.Fill(ds);
return ds.Tables[0];
}
}
public static List<tb_OvertimeSheetTable> getLists(int utype, int uid, int did)
{
DataTable dt = GetOvertimeSheet(utype, uid, did);
return getList(dt);
}
public static List<tb_OvertimeSheetTable> getList(DataTable dt) {
List<tb_OvertimeSheetTable> lists = new List<tb_OvertimeSheetTable>();
int counts = dt.Rows.Count;
if (counts > 0) {
tb_OvertimeSheetTable b = new tb_OvertimeSheetTable();
for (int i = 0; i < counts; i++)
{
if (dt.Rows[i]["ExpectStartTime"].ToString() != "") {
b.ExpectStartTime = Convert.ToDateTime(dt.Rows[i]["ExpectStartTime"].ToString());
}
if (dt.Rows[i]["ExpectEndTime"].ToString() != "")
{
b.ExpectEndTime = Convert.ToDateTime(dt.Rows[i]["ExpectEndTime"].ToString());
}
if (dt.Rows[i]["CreateTime"].ToString() != "")
{
b.CreateTime = Convert.ToDateTime(dt.Rows[i]["CreateTime"].ToString());
}
if (dt.Rows[i]["ApproveStatus"].ToString() != "") {
b.ApproveStatus = Convert.ToInt32(dt.Rows[i]["ApproveStatus"].ToString());
}
if (dt.Rows[i]["ApplyLevel"].ToString() != "")
{
b.ApplyLevel = Convert.ToInt32(dt.Rows[i]["ApplyLevel"].ToString());
}
b.Name =dt.Rows[i]["Name"].ToString();
b.DepartmentName = dt.Rows[i]["DepartmentNa"].ToString();
b.PositionName = dt.Rows[i]["Position"].ToString();
b.ID = Convert.ToInt32(dt.Rows[i]["ID"]);
lists.Add(b);
}
}
return lists;
}
谁能帮我看下哦。。=w=是不是for循环的赋值问题
[解决办法]
这代码真是。。。
[解决办法]
设置断点,跟踪下最简单。
[解决办法]
看到错误了
public static List<tb_OvertimeSheetTable> getList(DataTable dt) {中
....
if (counts > 0) {
//tb_OvertimeSheetTable b = new tb_OvertimeSheetTable(); 去掉挪到for中
for (int i = 0; i < counts; i++)
{
tb_OvertimeSheetTable b = new tb_OvertimeSheetTable();
[解决办法]
你在for中每次都给用一个tb_OvertimeSheetTable赋值了。