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 gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Attach a file location to a form 1

Status
Not open for further replies.

MJV57

Programmer
Apr 18, 2009
87
CA
I have a form (quote) that I would like the user to be able to attach a file (tiff,pdf etc...)to and when it is attached the file is stored in a specific location on the network. I also want to be able to recall that file when this form is loaded. So if a quote is created and a file is attached and that form is saved I would like to be able to open that form and also find and open the file that was stored in a specific directory. I would also like to automatically label the file the same as the quotenumber field on the form.
 
so what's your question?

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Sorry I guess I was not clear my mistake. I'm looking for code that can be attached to a button that will allow 1.the selection of a specific document 2.the renaming of the document based on the estimateno field on the specific record it is to be associated with 3. the storage of the document in a specific location on the server and finally 4. the ability to open this file from the form if it is reaccessed by another user that wants to see the document.
 
this is all code you will write. exactly how is up to you. if you are having trouble envisioning how this will work use each sentence as a bit of functionality and solve one piece at a time.

the parts I see are:
1. a user interface to save the data
2. a user interface to view the data
3. some logic to keep the database and file IO in sync
4. some logic to standardize naming conventions for files.

writing to the database and opening a file should be straightforward code, nothing fancy. For interface #1 (saving data) use a File Dialog box to select the file. For interface #2 use a link or button to allow the user to open the file.

I would treat the file directory similar to a database (hierachal, not relational). In that the identifier for the document shouldn't have business meaning, it should only be an identifier to relate the document to meta data in the database. (in other words, from a data POV, the document has no meaning by itself. it only has meaning when associated to the data record.) To that extent I would identify the file using a GUID, no the quote number.

hint: you should store the path to the file in the database. You quote table(s) will have a column named FileLocation, or something like that. Also by using an non-business defined naming scheme for the document you are free to refactor how the documents are stored, both IO and within the DB. (for example, the requirement may change to store revisions of all changes to the quote document. by using GUID you can change the database without needing a new naming schema for files.)

the "difficult" part is keeping the IO and database in sync. I would use the following code for that.
Code:
using(var connection = new SqlConnection(...))
using(var transaction = connection.BeginTransaction())
using(var command = connection.CreateCommand())
{
   command.CommandText = "insert/update statement";
   command.Paramaters.Add(...);
   try
   {
      connection.Open();
      command.ExecuteNonQuery();

      File.Save(fullPathToFile);

      transaction.Commit();
   }
   catch
   {
      if(File.Exists(fullPathToFile))
      {
         File.Delete(fullPathToFile);
      }
      transaction.Rollback();
      throw; // this is critical to keep the stacktrace intact.
   }
}

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top