Click here to Skip to main content
15,886,638 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack == false)
            {

                SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MinisanteDb;Integrated Security=True");
                SqlCommand com = new SqlCommand("select id,name,type,data from  uploadd where name=@name", con);
                com.Parameters.AddWithValue("name", GridView1.SelectedRow.Cells[1].Text);
                con.Open();
                //con.Close();
                SqlDataReader dr = com.ExecuteReader();
                if (dr.Read())
                {
                    Response.Clear();
                    Response.Buffer = true;
                    Response.ContentType = dr["type"].ToString();
                    Response.AddHeader("content-disposition", "attachment;filename=" + dr["name"].ToString());     // to open file prompt Box open or Save file         
                    Response.Charset = "";
                    Response.Cache.SetCacheability(HttpCacheability.NoCache);
                    Response.BinaryWrite((byte[])dr["data"]);
                    Response.End();
                    Page_Load(sender, e);
                }
            }


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 6-Mar-13 22:44pm
v2
Comments
Orcun Iyigun 7-Mar-13 4:49am    
You have a null value. Which line you are getting the exception? I think here: Response.ContentType = dr["type"].ToString();
Gilbertinino 7-Mar-13 7:41am    
I have the exception on this line of code com.Parameters.AddWithValue("name", GridView1.SelectedRow.Cells[1].Text);

1 solution

First off, you need an extra "@" in there:
C#
SqlCommand com = new SqlCommand("select id,name,type,data from  uploadd where name=@name", con);
com.Parameters.AddWithValue("name", GridView1.SelectedRow.Cells[1].Text);
Becomes:
C#
SqlCommand com = new SqlCommand("select id,name,type,data from  uploadd where name=@name", con);
com.Parameters.AddWithValue("@name", GridView1.SelectedRow.Cells[1].Text);
Otherwise the parameter is not linked up to the query properly.

You don't say which line gives you the error, but the fact that you don't get an SQL exception indiates that that it is the Parameters.AddWithValue line that is probably throwing the null reference: So check your SelectedRow paramater and make sure there is one - probably it is null, but it could also be that the Cells collection does not have sufficient cells - check the Count as well.

Two other things: Don't use constructs like:
if (!IsPostBack == false)
It is slow any awkward to read:
if (IsPostBack)
Is a lot easier.
And don't use "magic numbers" like "1" - use a constant instead so if it moves later it only has to be changed in one place.
 
Share this answer
 
Comments
Gilbertinino 7-Mar-13 7:36am    
com.Parameters.AddWithValue("@name", GridView1.SelectedRow.Cells[0].Text);
while am passing zero instead of one am still getting the same error so I don't understand how I can use constant.
OriginalGriff 7-Mar-13 8:30am    
Instead of using "0" or "1", set up a named constant:
const int nameColumn = 1;
It won't change the error if the problem is your SelectedRow property - if no row is selected it will return a null, and you will get the error when you try to use it.
So check first - and do something sensible if no row is selected!

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