Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Updated 14-Jun-13 5:03am
v3
Comments
[no name] 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 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!!
[no name] 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 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!
Richard C Bishop 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900