Click here to Skip to main content
15,881,709 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i correctly added the Imageid... But it passing null values...
can anyone help me!!!!

Here is the code:

C#
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
Updated 28-Apr-13 5:11am
v3
Comments
v surya dev 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">
<columns>
<asp:BoundField DataField="ImageId" HeaderText="ImageId" />
<asp:BoundField DataField="ImageName" HeaderText="ImageName" />
<asp:BoundField DataField="AddedOn" HeaderText="AddedOn" />
<asp:TemplateField HeaderText="FilePath">
<itemtemplate>
<asp:LinkButton ID="lnkDownload" runat="server" Text="Download" OnClick="lnkDownload_Click">



Kenneth Haugland 28-Apr-13 11:09am    
Use the improve question to update it with your code :-)
tumbledDown2earth 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 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
[no name] 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

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
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900