asp.net C# 中网格视图使用问题
我想用 HyperLink
和其他两个字段填充 GridView
。
我半心半意地尝试了以下方法。它给出了错误。
SqlCommand comm = new SqlCommand(
@"Select
FileUpload.FileName AS FileName,
FileUpload.FilePath AS PATH,
SubjectMaster.SubjectName AS Subject,
MemberPersonalInformation.FirstName As SharedBy
from FileUpload
INNER JOIN ContentManagement
ON ContentManagement.FileId=FileUpload.FileId
INNER JOIN MemberPersonalInformation
ON MemberPersonalInformation.MemberId=ContentManagement.CreatedBy
INNER JOIN SubjectMaster
ON ContentManagement.SubjectName=SubjectMaster.SubjectId
where
FileUpload.FileId in
(
Select FileId
from ContentManagement
where
ContentId in
(
Select ContentId
from ContentToIndividual
where ShowToMemberId=@MemberId
) and
ContentManagement.ContentTypeId=@TPD and
ContentManagement.SessionId=@SessionId
)", conn);
conn.Open();
SqlDataReader rdr = comm.ExecuteReader();
int i = 0;
while (rdr.Read())
{
string fip = rdr["PATH"].ToString();
GridViewRow di = GridView1.Rows[i];
HyperLink h1 = (HyperLink)di.FindControl("Hyperlink1");
h1.Text = rdr["FileName"].ToString();
h1.NavigateUrl = "download.aspx?filepath=" + fip;
di.Cells[1].Text = rdr["SharedBy"].ToString();
di.Cells[2].Text = rdr["Subject"].ToString();
i++;
}
rdr.Close();
获取错误消息
索引超出范围。必须是 非负且小于 集合。
参数 名称:索引
如果有人建议我更好的方法来执行此操作或纠正此错误。
I want to fill the GridView
with a HyperLink
and other two other fields.
I tried with the below approach half heartedly. It's giving errors.
SqlCommand comm = new SqlCommand(
@"Select
FileUpload.FileName AS FileName,
FileUpload.FilePath AS PATH,
SubjectMaster.SubjectName AS Subject,
MemberPersonalInformation.FirstName As SharedBy
from FileUpload
INNER JOIN ContentManagement
ON ContentManagement.FileId=FileUpload.FileId
INNER JOIN MemberPersonalInformation
ON MemberPersonalInformation.MemberId=ContentManagement.CreatedBy
INNER JOIN SubjectMaster
ON ContentManagement.SubjectName=SubjectMaster.SubjectId
where
FileUpload.FileId in
(
Select FileId
from ContentManagement
where
ContentId in
(
Select ContentId
from ContentToIndividual
where ShowToMemberId=@MemberId
) and
ContentManagement.ContentTypeId=@TPD and
ContentManagement.SessionId=@SessionId
)", conn);
conn.Open();
SqlDataReader rdr = comm.ExecuteReader();
int i = 0;
while (rdr.Read())
{
string fip = rdr["PATH"].ToString();
GridViewRow di = GridView1.Rows[i];
HyperLink h1 = (HyperLink)di.FindControl("Hyperlink1");
h1.Text = rdr["FileName"].ToString();
h1.NavigateUrl = "download.aspx?filepath=" + fip;
di.Cells[1].Text = rdr["SharedBy"].ToString();
di.Cells[2].Text = rdr["Subject"].ToString();
i++;
}
rdr.Close();
Getting Error message
Index was out of range. Must be
non-negative and less than the size of
the collection.
Parameter
name: index
If anybody suggest me a better way to do this or correct this error please.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
确保您的 gridview 至少有三个列,否则这一行将抛出您描述的异常:
在不同的注释中,您需要小心这一行:
将路径传递给客户端,然后返回到服务器是引入安全问题的好方法,请确保 download.aspx 检查 filepath 参数是否为真正应该可下载的文件! :)
Make sure your gridview has at least three columns, otherwise this line will throw the exception you describe:
On a different note, you need to be careful with this line:
passing paths to the client and then back to the server is a good way to introduce security problems, be sure that download.aspx checks the filepath parameter is to a file that really should be downloadable!! :)
GridView.Rows 集合索引是从零开始的。您需要用零初始化
i
。当
i
变量的值超出GridView.Rows
集合中的可用索引时,会发生错误。因此,如果用零初始化i
变量不能解决问题,那么返回的结果集(由读取器读取)比GridView具有更多的项目。行集合。
要确保
i
变量的值不超过GridView.Rows
集合的可用索引,请使用以下命令:如果您需要将行添加到 < code>GridView 然后使用以下内容:
另一个原因可能是您用来引用 GridViewRow.Cells 中的项目的索引。
GridView.Rows
collection indexing is zero-based. You need to initialize thei
with a zero.The error occurs when the value of the
i
variable exceedds the available indexes in theGridView.Rows
collection. So, if initializing thei
variable with zero does not solve the problem, then the returned result set - readed by the reader - has more items than theGridView.Rows
collection.To make sure that the value of the
i
variable does not exceed the available indexes for theGridView.Rows
collection use the following:If what you need is adding rows to your
GridView
then use the following:Another cause might be the indexes you are using to references the items in the
GridViewRow.Cells
.设置 int i = 0;并检查,索引从 0 开始,而不是 1
Set int i = 0; and check, index start from 0 not 1
我认为您的读者集合计数与网格行数的大小不匹配,这就是为什么它给出“索引超出范围”检查这个
执行此操作后进行检查
,然后执行您想要的操作
I think your reader collection count is not matching the size of grid row count that's why it is giving "Index was out of range" check this
Make a check
then after this perform which you want