Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object container for MSWord

Status
Not open for further replies.

quakeunity77

IS-IT--Management
Jun 3, 2010
36
US
I cannot find anything in toolbox of VS2010 that functions as an OLE container that will hold and show MSWord document or Excel spreadsheet that is contained in a database. I have MSWord and Excel running on the server and I have no problem just having them run in an ASP.net environment. However, MSword and Excel open as blank and I cannot get the contents of the field that contains the .doc or .xls to show.
 
because that type of control doesn't exist. a UI control will/should not have knowledge of a database, or the type of data it's receiving.

as for displaying a document. you need to open a stream and pass this to the http request. then you need to set the appropriate headers so the browser knows how to render the document.

remember this is a web environment, not a desktop environment. a lot of rules change and certain assumptions that may hold true in a stateful, Windows environment do not apply to the stateless, OS agnostic web environment.

for examples, search the web for "webforms load image from database" the same rules apply to word and excel, the content type changes, that's all. you could even search this forum for examples.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I found multiple references to load image or load file from a specific location, but my problem is that I need to load the contents of the database which has a field that contains an MSWord document. Can someone please help?
 
we discussed this already thread855-1665735

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I am interested in the solution. I have the same problem.
 
1. query the column from MS ACCESS and load it into either a stream or a byte array
2. write the stream/byte array to the response stream.
3. set the content headers correctly.

this can all be done using a generic handler.
Code:
class viewfile : IHttpHandler
{
   public void ProcessRequest(HttpContext context)
   {
       var pk = context.Request.QueryString["..."];
       var bytes = GetFileFromDbWith(pk);
       context.Response.WriteBinary(bytes);
       context.Response.Headers.Add(load as word document);
   }

   public bool IsReusable{get{return true;}}

   private byte[] GetFileFromDbWith(object pk)
   {
      using(connection...)
      using(command...)
      {
          command.CommandText = "select field from table where id = @id";
          add pk parameter with name ID
          return (byte[]) command.ExecuteScalar();
      }
   }
}

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
LOL - thanks jb... that made my morning :)

@quakeunity77
The beauty of the .net platform is that any language with a .net compiler can be used to write the code. VB and C# are the big 2, but there are compilers for other languages as well.
F#
C++
Ruby
Python

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Glad I can make you laugh. They say laughter heals all, so I try. LOL

So, quakeunity77 if you need help converting Jason's code, there are many online, free converters you can use to help. Jason has already written the code for you and helped you tremendously, please don't ask for the conversion as well. People here are here to help, not do all the work for you. Jason has already done more than enough.
Here is a converter I use at times to help me:
 
I tried to convert it to VB and got the error message: line 8 col 43: ")" expected. I tried to recode but it still does not work.
 
details would be helpful. like the compilation error, exception, the current code, etc. saying "it doesn't work" is too vague.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS1513: } expected

Source Error:



Line 13: }
Line 14: protected void Button1_Click(object sender, EventArgs e)
Line 15: {
Line 16: class viewfile : IHttpHandler{ public void ProcessRequest(HttpContext context) { var pk = context.Request.QueryString["..."];
Line 17: var bytes = GetFileFromDbWith(pk); context.Response.WriteBinary(bytes);


Source File: c:\Users\tony1\Documents\Visual Studio 2010\WebSites\WebSite47\Default.aspx.cs Line: 15


 
there is an open bracket than needs to be closed.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I am having the same problem. Where does it need to be closed?
 
I don't know, you didn't post your code. but, if you read your stacktrace it will tell you what line failed and why. this is where you can start to debug. in the case of a missing bracket, start matching them up.

going back over the looking at quake's last post you missed the point of my example. "viewfile" is it's own class, it's not meant to be copied into the method. it's generic http handler this code would execute when the url to the view files handler is made.

your button click event would simply redirect to that route.
Code:
void clickbutton(object sender, eventargs e)
{
  var url = string.format(~/viewfile.ashs?key={0}", value);
  response.redirect(url);
}
and please don't blindly copy and past this. it's meant to be an example. the relative path, url and querystring will most likely need to change. you'll also need to decide if you want the request to end right there, of if the current request should complete before making the new request.

if this isn't making sense, then I would stop what you are currently doing and take an hour or two to learn about the web/http and the asp.net pipeline. then learn the webforms page life cycle. then come back and try to solve this problem. it will be so much easier if you understand the technologies you are using.


Jason Meckley
Programmer

faq855-7190
faq732-7259
 
ashs should be ashx. is the generic handler suffix. key=value the query string. {0} is the string formatting.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
where should I put the ashx handler code? What to I need to put in the web.config file?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top