Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
hi, my name is modeste
am having a problem concerning how to download a file from database using datalist view control on asp.net this is the code am having but it is not giving result
 SqlCommand cmd = new SqlCommand("select * FROM WordFiles where id=@id ", con);
                cmd.Parameters.AddWithValue("id",DataList1.SelectedItem.ToString());
 
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                dt.TableName = "WordFiles";
                da.Fill(dt);
                DataSet dS = new DataSet();
                dS.DataSetName = "WordFiles";
                dS.Tables.Add(dt);
               // StringWriter sw = new StringWriter();
              //  dS.WriteXml(sw, XmlWriteMode.IgnoreSchema);
               // string s = sw.ToString();
 
                SqlDataReader drr = cmd.ExecuteReader();
 

                if (drr.Read())
                {
                // string attachment = "attachment; filename=test.xml";
                Response.ClearContent();
                Response.ContentType = drr["type"].ToString();
                Response.AddHeader("content-disposition", "attachment;filename=" + drr["name"].ToString());
                Response.BinaryWrite((byte[])drr["data"]);
                Response.End();
Posted 14-Jun-13 0:07am
Edited 14-Jun-13 5:03am
33.9K
v3
Comments
ThePhantomUpvoter at 14-Jun-13 6:18am
   
Debug your code. Define what "not giving result" means? Did you check your SQL? Why are you adding a parameter to your SQL when the parameter does not exist?
carpediemmodeste at 14-Jun-13 7:38am
   
ThePhantomUpvoter can you plz help me: those are the information about my sql; i have a table called wordFiles with id as primary key(int), name(the name of the file which is in table), data(which is having the content of the file) and the type (wich is storing the exenstion),if you have the code of downloading a file using a button in a datalist view plz help me .Thank you so much!!
ThePhantomUpvoter at 14-Jun-13 9:11am
   
Well I am not sure how to help you.
1. You have not debugged your code.
2. You have not specified what error message you are getting.
3. You specify a parameter named "@id" but try to assign a value to a parameter named "id".
4. You have not detailed what result you are getting with your code.
carpediemmodeste at 14-Jun-13 10:32am
   
Thank you so much for your time you are giving to me for help i have degugged the code, then runned the code then when i click on the button i get nothing ! no error, nothing happen
may table in database is having 3 colomns which are : id(as primary key), data(where the file will be) and typ(which will hold the extension of any file uploaded in a table), then on the datalist i want that if a user click on button he/she can download that file. thank you!
richcb at 14-Jun-13 11:04am
   
As ThePhantomUpvoter has stated, you have not provided enough information. But I can tell you your query will not return anything.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Schatak 394
1 OriginalGriff 365
2 _Amy 115
3 Sergey Alexandrovich Kryukov 109
4 Andreas Gieriet 100
0 OriginalGriff 7,097
1 Sergey Alexandrovich Kryukov 5,623
2 Maciej Los 3,504
3 Peter Leow 3,373
4 DamithSL 2,505


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 14 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100