Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to download a file from database using gridview

Here is my code...

C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!IsPostBack)
           {
               BindGrid();
           }
       }
       private void BindGrid()
       {
           string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
           using (SqlConnection con = new SqlConnection(constr))
           {
               using (SqlCommand cmd = new SqlCommand())
               {
                   cmd.CommandText = "select Id, FileName from Career";
                   cmd.Connection = con;
                   con.Open();
                   cmd.ExecuteNonQuery();
                   GridView1.DataBind();
                   con.Close();
               }
           }

       }




       protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
       {
           {
               int id = int.Parse((sender as LinkButton).CommandArgument);
               byte[] bytes;
               string fileName, contentType;
               string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
               using (SqlConnection con = new SqlConnection(constr))
               {
                   using (SqlCommand cmd = new SqlCommand())
                   {
                       cmd.CommandText = "select FileName, Data, contentType from Career where Id=@Id";
                       cmd.Parameters.AddWithValue("@Id", id);
                       cmd.Connection = con;
                       con.Open();
                       using (SqlDataReader sdr = cmd.ExecuteReader())
                       {
                           sdr.Read();
                           bytes = (byte[])sdr["Data"];
                           contentType = sdr["contentType"].ToString();
                           fileName = sdr["FileName"].ToString();
                       }
                       con.Close();
                   }
               }
               Response.Clear();
               Response.Buffer = true;
               Response.Charset = "";
               Response.Cache.SetCacheability(HttpCacheability.NoCache);
               Response.ContentType = contentType;
               Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
               Response.BinaryWrite(bytes);
               Response.Flush();
               Response.End();
           }
       }

   }




Please let me know how to solve this error??

Update: The error occurs on this line

C#
int id = int.Parse((sender as LinkButton).CommandArgument);
Posted
Updated 12-Aug-15 1:14am
v2
Comments
RAJKUMAR M G 12-Aug-15 6:37am    
Specify the error line
F-ES Sitecore 12-Aug-15 6:38am    
You solve the error but not referencing null variables. How you fix your *specific* error depends on what it is that is null that you're referencing but as you didn't say what line the error occurred on it's impossible to give any specific help, you're asking people to guess.
MohamedEliyas 12-Aug-15 6:38am    
specify the error line in your code. mention pls
RMSinha 12-Aug-15 6:47am    
ohh sorry its my fault...
Sanket Saxena 12-Aug-15 6:47am    
Debug it first and let us know where you are facing the issue.

Hello ,
You have not used GridView DataSource property .Modify your code like this way
using (SqlCommand cmd = new SqlCommand())
               {
                   cmd.CommandText = "select Id, FileName from Career";
                   cmd.Connection = con;
                   con.Open();

                //Remove this line
	           //  cmd.ExecuteNonQuery();

	               //Add these lines
         	       DataTable dt = new DataTable();
                   dt.Load(cmd.ExecuteReader());
                
	             GridView1.DataSource=dt;
              
	             GridView1.DataBind();
                 con.Close();
               }
 
Share this answer
 
Could you please check and change your code as below:

C#
 protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  {    
    if (e.CommandName == "YourButtonCommandName")

       {
           int id = Convert.ToInt32(e.CommandArgument.ToString());

           //rest code here

       }
}


Hope it Helps :)
 
Share this answer
 

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