asp.net C# 中网格视图使用问题

发布于 2024-11-15 10:52:19 字数 1871 浏览 3 评论 0原文

我想用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

未蓝澄海的烟 2024-11-22 10:52:19

确保您的 gridview 至少有三个,否则这一行将抛出您描述的异常:

di.Cells[2].Text = rdr["SharedBy"].ToString();

在不同的注释中,您需要小心这一行:

 h1.NavigateUrl = "download.aspx?filepath=" + fip;

将路径传递给客户端,然后返回到服务器是引入安全问题的好方法,请确保 download.aspx 检查 filepath 参数是否为真正应该可下载的文件! :)

Make sure your gridview has at least three columns, otherwise this line will throw the exception you describe:

di.Cells[2].Text = rdr["SharedBy"].ToString();

On a different note, you need to be careful with this line:

 h1.NavigateUrl = "download.aspx?filepath=" + fip;

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!! :)

烏雲後面有陽光 2024-11-22 10:52:19

GridView.Rows 集合索引是从零开始的。您需要用初始化i

i 变量的值超出 GridView.Rows 集合中的可用索引时,会发生错误。因此,如果用初始化i变量不能解决问题,那么返回的结果集(由读取器读取)比GridView具有更多的项目。行集合。

要确保 i 变量的值不超过 GridView.Rows 集合的可用索引,请使用以下命令:

while (rdr.Read() && i < gridView.Rows.Count)

如果您需要将行添加到 < code>GridView 然后使用以下内容:

while (rdr.Read() && i < 3)
{
    string fip = rdr["PATH"].ToString();
    GridViewRow di = new GridViewRow();

    ....

    gridView.Rows.Add(di);        
    i++;        
}

另一个原因可能是您用来引用 GridViewRow.Cells 中的项目的索引。

GridView.Rows collection indexing is zero-based. You need to initialize the i with a zero.

The error occurs when the value of the i variable exceedds the available indexes in the GridView.Rows collection. So, if initializing the i variable with zero does not solve the problem, then the returned result set - readed by the reader - has more items than the GridView.Rows collection.

To make sure that the value of the i variable does not exceed the available indexes for the GridView.Rows collection use the following:

while (rdr.Read() && i < gridView.Rows.Count)

If what you need is adding rows to your GridView then use the following:

while (rdr.Read() && i < 3)
{
    string fip = rdr["PATH"].ToString();
    GridViewRow di = new GridViewRow();

    ....

    gridView.Rows.Add(di);        
    i++;        
}

Another cause might be the indexes you are using to references the items in the GridViewRow.Cells.

爱她像谁 2024-11-22 10:52:19

设置 int i = 0;并检查,索引从 0 开始,而不是 1

Set int i = 0; and check, index start from 0 not 1

为人所爱 2024-11-22 10:52:19

我认为您的读者集合计数与网格行数的大小不匹配,这就是为什么它给出“索引超出范围”检查这个
执行此操作后进行检查

if(GridView1.Rows.Count  > i)

,然后执行您想要的操作

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

if(GridView1.Rows.Count  > i)

then after this perform which you want

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文