Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
i correctly added the Imageid... But it passing null values...
can anyone help me!!!!
 
Here is the code:
 
  
protected void lnkDownload_Click(object sender, EventArgs e)
    {
 
        LinkButton lnkbtn = sender as LinkButton;
        GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
        int ImageId = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());
        string name, type;
        using (SqlConnection con = new SqlConnection(strCon))
        {
            using (SqlCommand cmd = new SqlCommand())
            {
                cmd.CommandText = "select ImageName, FileName, AddedOn from ImageInfo where ImageId=@ImageId";
                cmd.Parameters.AddWithValue("@ImageId", ImageId);
                cmd.Connection = con;
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr.Read())
                {
                    Response.ContentType = dr["FileType"].ToString();
                    Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["ImageName"] + "\"");
                    Response.BinaryWrite((byte[])dr["FileName"]);
                    Response.End();
                }
            }
        }
    }
 

Server Error in '/WebSite5' Application.
 
Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
 
Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index
 
Source Error: 
 

Line 149:        LinkButton lnkbtn = sender as LinkButton;
Line 150:        GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
Line 151:        int ImageId = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());
Line 152:        string name, type;
Line 153:        using (SqlConnection con = new SqlConnection(strCon))
 
Source File: c:\Users\SURYA-BOSS\Documents\Visual Studio 2010\WebSites\WebSite5\Default.aspx.cs    Line: 151 
 
Stack Trace: 
 

[ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.
Parameter name: index]
   System.Collections.ArrayList.get_Item(Int32 index) +9368776
   System.Web.UI.WebControls.DataKeyArray.get_Item(Int32 index) +42
   _Default.lnkDownload_Click(Object sender, EventArgs e) in c:\Users\SURYA-BOSS\Documents\Visual Studio 2010\WebSites\WebSite5\Default.aspx.cs:151
   System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +141
   System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +150
   System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +38
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +37
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +289
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4225
 
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
[Edit]Code block added[/Edit]
Posted 28-Apr-13 5:39am
Edited 28-Apr-13 6:11am
v3
Comments
v surya dev at 28-Apr-13 10:49am
   
My .CS Code .....
 

protected void lnkDownload_Click(object sender, EventArgs e)
{
 
LinkButton lnkbtn = sender as LinkButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
int ImageId = Convert.ToInt32(GridView1.DataKeys[gvrow.RowIndex].Value.ToString());
string name, type;
using (SqlConnection con = new SqlConnection(strCon))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select ImageName, FileName, AddedOn from ImageInfo where ImageId=@ImageId";
cmd.Parameters.AddWithValue("@ImageId", ImageId);
cmd.Connection = con;
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
Response.ContentType = dr["FileType"].ToString();
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["ImageName"] + "\"");
Response.BinaryWrite((byte[])dr["FileName"]);
Response.End();
}
}
}
}
 

 
aspx.code is:
 

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">

<asp:BoundField DataField="ImageId" HeaderText="ImageId" />
<asp:BoundField DataField="ImageName" HeaderText="ImageName" />
<asp:BoundField DataField="AddedOn" HeaderText="AddedOn" />
<asp:TemplateField HeaderText="FilePath">

<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click">



Kenneth Haugland at 28-Apr-13 11:09am
   
Use the improve question to update it with your code :-)
tumbledDown2earth at 28-Apr-13 11:09am
   
Check gvrow.RowIndex. Its value looks like is less than GridView1.DataKeys.Count. This is causing the problem
v surya dev at 28-Apr-13 11:23am
   
ya i cleared this... but i have another problem.....
 
the problem is:
 
i am using varbinary(MAX) data type... last time i used the same code for the same data type... but now it shows this error...
can you help me...!!!!
 

Server Error in '/WebSite5' Application.
 
Unable to cast object of type 'System.String' to type 'System.Byte[]'.
 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
 
Exception Details: System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Byte[]'.
 
Source Error:
 

Line 164: // Response.ContentType = dr["AddedOn "].ToString();
Line 165: Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["ImageName"] + "\"");
Line 166: Response.BinaryWrite((byte[])dr["FileName"]);
Line 167: Response.End();
Line 168: }
 
Source File: c:\Users\SURYA-BOSS\Documents\Visual Studio 2010\WebSites\WebSite5\Default.aspx.cs Line: 166
 
Stack Trace:
 

[InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Byte[]'.]
_Default.lnkDownload_Click(Object sender, EventArgs e) in c:\Users\SURYA-BOSS\Documents\Visual Studio 2010\WebSites\WebSite5\Default.aspx.cs:166
System.Web.UI.WebControls.LinkButton.OnClick(EventArgs e) +141
System.Web.UI.WebControls.LinkButton.RaisePostBackEvent(String eventArgument) +150
System.Web.UI.WebControls.LinkButton.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +38
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +37
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +289
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4225
 
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272
ThePhantomUpvoter at 28-Apr-13 11:27am
   
That is a completely different question/problem and does not belong here.
 
Besides that, the error message tells you exactly what the problem is.

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

It means that the value of gvrow.RowIndex is less than 0 or more than GridView1.DataKeys.Count - 1. Use the debugger: set a break point on this like and check those values during runtime, figure out why, then fix the code.
 
—SA
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 562
1 Sergey Alexandrovich Kryukov 484
2 Maciej Los 325
3 DamithSL 233
4 Mathew Soji 195
0 OriginalGriff 7,168
1 Sergey Alexandrovich Kryukov 6,377
2 DamithSL 5,461
3 Manas Bhardwaj 4,876
4 Maciej Los 4,450


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 28 Apr 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100