如何解决索引超出了我的sqlsitemapprovider中数组的范围? [英] How to solve Index was outside the bounds of the array in my sqlsitemapprovider?
问题描述
大家好,我是c#的新手。我有sqlsitemapprovider和存储过程。我在sqlsitemapprovider的这一行有这个问题:
DataRow rowRoot = dtSiteMap.Select([parentId] IS NULL)[0]; // problemhere //
每次我在我的存储过程中过滤我的idUserRole时会出现此问题,但如果我没有过滤我的idUserRole,我的动态菜单栏将会起作用。我已经检查了我的数据表,当我通过idUserRole过滤它时它是空的,如果我没有通过idUserRole过滤它,它就会填满。我坚持这个问题两个星期,我已经通过每个论坛,但仍然没有找到任何解决方案。
我的代码sqlsitemapprovider:
Hi everyone, i am newbie in c#. i have sqlsitemapprovider and stored procedure. i having this problem at this row in sqlsitemapprovider:
DataRow rowRoot = dtSiteMap.Select("[parentId] IS NULL")[0]; //problemhere //
this problem occur everytime i filter my idUserRole in my stored procedure but my dynamic menu bar will work if i did not filter my idUserRole. i already check my datatable, it is empty when i did filter it by idUserRole and it filled if i did not filter it by idUserRole. i stuck in this problem for two week, i have go through every forum but still did not find any solution.
my code sqlsitemapprovider:
DataSet ds = new DataSet();
adapter.Fill(ds, "modulemain");
DataTable dt = new DataTable();
DataTable dtSiteMap = ds.Tables["modulemain"];
DataRow rowRoot = dtSiteMap.Select("[parentId] IS NULL")[0]; //problemhere //
rootNode = new SiteMapNode(this,
rowRoot["url"].ToString(), rowRoot["url"].ToString(),
rowRoot["description"].ToString(), rowRoot["description"].ToString());
string rootID = rowRoot["moduleId"].ToString();
我的存储过程:
my stored procedure:
CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_SiteMap`()
BEGIN
SELECT A.moduleId, A.Code,A.description,A.url,A.parent AS parentId,A.sequence
, C.`description` AS RoleDesc,D.idUser
FROM modulemain A
LEFT JOIN `moduleitem` B ON A.`moduleId` = B.`ModuleId` AND B.`active` = 1
LEFT JOIN `usermodule` C ON B.`UserModuleId` = C.`idUserModule`
LEFT JOIN `user` D ON C.`idUserModule` = D.`idUserRole`
WHERE A.active = 1 AND D.idUserRole = @idUserRole
ORDER BY A.parent, A.Code, A.sequence;
END$$
有什么建议吗?非常感谢!愿上帝保佑你们!
any suggestion? many thanks! May god bless you guys!
推荐答案
有什么建议吗?非常感谢!愿上帝保佑你们!
any suggestion? many thanks! May god bless you guys!
改变自:
DataRow rowRoot = dtSiteMap.Select([parentId] IS NULL )[0]; //问题//
要:
var res =来自dt.AsEnumerable()中的行
其中row [parentID] == null
选择行;
if(res.Any ()){
DataRow rowRoot = res.First();
} else {
// TODO:实施适当的cas处理有一个parentId
}
指出不要假设有一个数字0(第一个),直到你知道有,这是你的问题
Change from:
DataRow rowRoot = dtSiteMap.Select("[parentId] IS NULL")[0]; //problemhere //
To:
var res = from row in dt.AsEnumerable()
where row["parentID"] == null
select row;
if(res.Any()){
DataRow rowRoot = res.First();
} else {
//TODO: Implement appropriate handling of cas where there is a parentId
}
Point being that don't presume there is a number 0 (first) until you know there is, this is your problem
这篇关于如何解决索引超出了我的sqlsitemapprovider中数组的范围?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!