Click here to Skip to main content
15,893,401 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hiii to all,
This is my code behind file:
C#
public partial class Fileupload : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
    SqlCommand cmd1; 
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            Filldrop();
        }
    }
    private void Filldrop()
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
        SqlCommand cmd=new SqlCommand();
        cmd.CommandType = CommandType.StoredProcedure;       
        cmd.CommandText = "Select_File8";        
        cmd.Connection = conn;
        conn.Open();
        //cmd1.Parameters.Add("@FileName ", SqlDbType.VarChar).Value = FileUpload1.FileName; 
        // SqlDataAdapter da = new SqlDataAdapter(cmd);       
        SqlDataAdapter ad = new SqlDataAdapter(cmd);
        cmd.Parameters.Clear();   
        ad.SelectCommand = cmd;
        ad.SelectCommand.Parameters.Add("@FileName", SqlDbType.VarChar).Value = FileUpload1.FileName;    
        //cmd1.Parameters.Add("@FileID", SqlDbType.VarChar).Value = 1;
        DataSet ds = new DataSet();
        ad.Fill(ds,"Files");       
        //DropDownList1.DataTextField = "FileName ";
        //DropDownList1.DataValueField = "FileName ";
        DropDownList1.DataSource = ds;
        DropDownList1.DataBind();
        conn.Close();
    }
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            if (FileUpload1.HasFile)
            {
                int filsize = FileUpload1.PostedFile.ContentLength;
                byte[] filebyt = new byte[filsize];
                FileUpload1.PostedFile.InputStream.Read(filebyt, 0, filsize);
                SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                cmd1 = new SqlCommand();
                cmd1.CommandType = CommandType.StoredProcedure;
                cmd1.CommandText = "File_Upload";
                cmd1.Connection = conn;
                // SqlDataAdapter da = new SqlDataAdapter(cmd);
                conn.Open();  
                cmd1.Parameters.AddWithValue("@FileName ", Path.GetFileName(FileUpload1.PostedFile.FileName));
                cmd1.Parameters.AddWithValue("@Files ", filebyt);               
                int result = cmd1.ExecuteNonQuery();
                conn.Close(); 
                cmd1.Dispose(); 
                conn.Dispose(); 
                filebyt = null;
                Response.Write("File uploaded");
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        try
        {
            string strname = string.Empty;
            string name = DropDownList1.SelectedItem.Value;
            SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            cmd1 = new SqlCommand();
            cmd1.CommandType = CommandType.StoredProcedure;
            cmd1.CommandText = "Select_File2";
            cmd1.Connection = conn;
            // SqlDataAdapter da = new SqlDataAdapter(cmd);
            conn.Open();   
            SqlDataReader dr = cmd1.ExecuteReader();
            while (dr.Read())
            {
                strname = dr["FileName "].ToString();
                Byte[] filbyt = (Byte[])dr["Files "];
                FileStream filstr = new FileStream(Server.MapPath("Documents")+ @"\" + strname, FileMode.OpenOrCreate);
                filstr.Write(filbyt, 0, filbyt.Length);
                filstr.Close(); 
                filstr.Dispose();
                filbyt = null;
            }
            conn.Close(); 
            cmd1.Dispose(); 
            conn.Dispose();
            if (strname != string.Empty)
            {
                Response.Redirect(@"Documents/" + strname);
            }
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
    }

    
}

This is my stored procedure:
SQL
ALTER PROCEDURE [dbo].[Select_File8]
AS
declare @FileName varchar(50)
BEGIN
    Select  FileName from Files
END

Here file upload works, but I want to view the uploaded file by selecting the filename in dropdownlist by means of stored procedure and try to open the selected document, its not working....
Posted
Updated 21-Apr-11 23:41pm
v3
Comments
Sandeep Mewara 22-Apr-11 4:06am    
its not working....
Do you get amy error?
dhanyasri 22-Apr-11 8:28am    
check it and tell me
Gandalf_TheWhite 22-Apr-11 8:38am    
He's asking you about the errors that you got while you are running this code, if you tell some error it will be easy to track your problem. also people over here are not meant to do your work and check errors for you, so mind your attitude and co-operate to 'SOLVE YOUR OWN' problem.
Sandeep Mewara 22-Apr-11 11:25am    
:thumbsup:
Sandeep Mewara 22-Apr-11 11:24am    
Right! :)

On the SelectedIndexChange event of the DropDownList1, write the following code

 bytDocTemp = (byte[])YOUROBJECT.DOCUMENTPROPERTY;
File_Name = YOUROBJECT.DOCUMENT_FileName;
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + File_Name + "\"");
Response.ContentType = "application/octet-stream";
Response.BinaryWrite(bytDocTemp);
Response.Flush();
Response.End();
 
Share this answer
 
maybe youe code can not work because SP has problem.
SQL
ALTER PROCEDURE [dbo].[Select_File8]
AS
declare @FileName varchar(50)
BEGIN
    Select  FileName from Files
    Where @FileName=???
END


please check it again.

good lucky!
 
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