Download Word File from Sql Database,Need Help

Posted by Raja_89 under ASP.NET on 10/15/2013 | Points: 10 | Views : 727 | Status : [Member] | Replies : 1
Hai

I am having a file[stored in the form of varbinary(MAX) ]

Concept:
Click Download button to save the Corresponding Word file from the SQL Database 2005

while downloading that file through gridview i got an error
"Object reference not set to an instance of an object."
pointing to
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;



Code:

LinkButton lnkbtn = sender as LinkButton;
GridViewRow gvrow = lnkbtn.NamingContainer as GridViewRow;
int fileid = Convert.ToInt32(dwnstGridView.DataKeys[gvrow.RowIndex].Value.ToString());

SqlCommand objgv = new SqlCommand("select dt_docfile from userDownload where dt_userid=@dt_userid", objsqlcon);
objgv.Parameters.AddWithValue("@dt_userid", fileid);


SqlDataReader dr = objgv.ExecuteReader();
if (dr.Read())
{
Response.ContentType = dr["FileType"].ToString();
Response.AddHeader("Content-Disposition", "attachment;filename=\"" + dr["FileName"] + "\"");
Response.BinaryWrite((byte[])dr["FileData"]);
Response.End();

}
}



why this error arises ?
Tell any other code


Help me as short as possible

Regards




Responses

Posted by: Bandi on: 10/18/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
Refer this link
http://bytes.com/topic/asp-net/answers/864478-object-reference-not-set-instance-object

Try to find out the linkButton reference using FindControl() method for accessing LinkButton data
http://stackoverflow.com/questions/6501479/error-object-reference-not-set-to-an-instance-of-an-object-during-grid-findcon

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Raja_89, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response