开发者

Problem with Grid view usage in asp.net C#

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.

Param开发者_C百科eter name: index

If anybody suggest me a better way to do this or correct this error please.


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


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.


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

if(GridView1.Rows.Count  > i)

then after this perform which you want

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜