Click here to Skip to main content
15,895,370 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Actually whenever the submit the decrpt code then file was downloaded.it is theme.

C#
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class downloadfile : System.Web.UI.Page
{
    string Id;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["fuzzyconnection"]);
            con.Open();
            Id = (string)Session["filenam"];
            SqlCommand cmd = new SqlCommand("select * from ownerfiles where filenam = '" + Id + "'", con);
            //cmd.Parameters.Add("@FileId",SqlDbType.Int).Value=1;
            DataTable dt = GetData(cmd);
            if (dt != null)
            {
                download(dt);
            }


            
            
            
        }
    }
    private DataTable GetData(SqlCommand cmd)
    {

        DataTable dt = new DataTable();
        SqlConnection con = new SqlConnection(ConfigurationManager.AppSettings["fuzzyconnection"]);
        SqlDataAdapter sda = new SqlDataAdapter();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        try
        {
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);
            return dt;
        }
        catch
        {
            return null;
        }

        finally
        {
            con.Close();
            sda.Dispose();
            con.Dispose();
        }
    }
    private void download(DataTable dt)
    {
        if (dt.Rows.Count > 0)
        {
            Byte[] bytes = (Byte[])dt.Rows[0]["files"];
            Response.Buffer = true;
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            //Response.ContentType = dt.Rows[0]["Ftype"].ToString();
            Response.AddHeader("content-disposition", "attachment;filename=" + dt.Rows[0]["filenam"].ToString());
            //Response.BinaryWrite("<script type='text/javascript'> <embed src='bytes' style=width:300px; height:200px;> </embed> </script> ");
            Response.ContentType = "application/msword";
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
        }
    } 




}


in this one string read null value and dt not return values.it's empty.

that's why file was not downloading.
string is like as

C#
Id = (string)Session["filenam"];


dt like as

C#
DataTable dt = GetData(cmd);



my problem is file was not downloading

please help me.

thank u.
Posted
Updated 21-Jun-15 4:29am
v2
Comments
Are you sure the session exists? Because there is a spell mistake. It might be "filename". But I am not sure, you need to debug and check.
Krishna Veni 21-Jun-15 10:51am    
"filenam" is mentioned in database.not spell mistake
Suvendu Shekhar Giri 21-Jun-15 11:12am    
You are trying to retrieve data from a session variable but I could not find the line of code where you have assigned value to the session variable. Unless you have assigned any value , it is supposed to return null.

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