|
I'm sorry I have never done this before.
What I have there is what I've tried to get to work from what I've seen on the internet.
myReader["Saved_File"].ToString(); is reading the varbinary from the SQL database (Saved_File) column.
So i'm taking the string FileData and storing the information in the SQL database in the string.
So what I'm trying to do is take the data from the database and attach it in an email without actually saving a file to the harddrive then deleting it when I'm done. I'm wanting to store the file in memory, then attach it to the email and send it. Is this possible?
|
|
|
|
|
You have to loop on the read in order to get all of the data from the stream. The read method returns number of bytes read, loop until number of bytes read == number of bytes in file.
Also, to string will only cause things to break. It is the most overused and misunderstood feature of .NET apparently.
Need software developed? Offering C# development all over the United States, ERL GLOBAL, Inc is the only call you will have to make.
If you don't ask questions the answers won't stand in your way.
Most of this sig is for Google, not ego.
|
|
|
|
|
Jacob D Dixon wrote: FileData = myReader["Saved_File"].ToString();
You are getting the data as a byte array, but then you call the ToString method on the data. The Array class does not override the ToString method, so you are using the default implementation, which returns the name of the class. So, that line gives the same result as:
FileData = "System.Byte[]";
Jacob D Dixon wrote: byte[] buffer = new byte[FileData.Length];
Here you are creating a new byte array with the size 13.
Jacob D Dixon wrote: ms.Read(buffer, 0, FileData.Length);
Here you attemt to read the first 13 bytes from the memorystream. However, as you ignore the return value of the Read method, you don't know if you got one byte or 13 bytes. The Read method doesn't have to return the number of bytes that you requested, even if there are more bytes left in the stream, so you need to call Read in a loop until you have got all the data that you want, or until the method returns zero which means that it reached the end of the stream.
However, I have no idea why you are trying to read the data from the memory stream into a byte array, when you are using the memory stream in the attachment. Also I don't know where you think that you get the data from the data reader into either the memory stream or the buffer...
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Im not trying to read from a memorystream into a byte array. I'm trying to take the bytes from the database and put them into a Stream to attach to an email.
Attachment of (System.Net.Mail) needs a file or a Stream
|
|
|
|
|
Figured it out. This is what I did:
...(SqlConnection, SqlCommand stuff up here)...
MailMessage mm = new MailMessage(...);
SqlDataReader myReader = null;
conn.Open
myReader = cmd.ExecuteReader();
while (myReader.Read())
{
object buffer = myReader["Saved_File"];
mm.Attachments.Add(new Attachment(new MemoryStream((byte[])buffer), myReader["Filename"].ToString()));
}
conn.Close();
It works.. but is that a good way to do it. Basically tehre can be multiple files per ticket. So it goes through each file within the ticket (that has the ID of whatever) and puts it in the object buffer, then attaches it to an email.
Is there a better way? I tested this and it works but I would love to learn a better or more correct way.
|
|
|
|
|
I think that overall the solution is good. It's definitely better to process the data in memory rather than writing it to disk only to delete it right afterwards. Especially since the only reason for writing to disk was to use the file as an Attachment. Writing to disk is slower and more 'risky' than working with the data in memory. One small improvement would be to put a try/catch/finally around the while loop and use the finally clause to close the database resources. Another would be to declare buffer as the correct type just one time outside the loop and use safe type conversion followed by testing it for null . Another thing to improve might be accessing the 'Filename' field without testing for DBNull or anything; but that may not be an issue for your data (you might have the field declared not null in the database, or might be defaulting it to '' if it's null in your query, etc.).
try
{
byte[] buffer = null; <----- declare it here as null and of type byte[]
while(myReader.Read())
{
buffer = myReader["Saved_File"] as byte[]; <----- safely convert the return value here
if(buffer == null) <----- conversion failed, skip this item
continue;
mm.Attachments.Add(new Attachment(new MemoryStream(buffer), myReader["Filename"].ToString()));
}
}
catch
{
}
finally <----- close out database resources here
{
if(myReader != null)
myReader.Close();
if(conn != null)
conn.Close();
}</handle>
Keep It Simple Stupid! (KISS)
|
|
|
|
|
If I have a .dll project with a bunch of common classes, can I add that project to multiple solutions? Is this a safe thing to do or is there a better solution?
|
|
|
|
|
mobius111001 wrote: .dll project
Why not just reference the DLL in whatever project you want to use it in instead of adding the project all the time?
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
|
|
|
|
|
If I wanted to add new features to the DLL or something of that nature.
|
|
|
|
|
mobius111001 wrote: add new features to the DLL
That does not preclude you adding new features.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
|
|
|
|
|
Yet, .Net is crap where code re-use is concerned. In C++, all you had to do was add a given file to your project, regardless of what folder it was in. In .net, you can't do that. I don't care to have a million little DLL's distributed with my app. Sometimes, I don't want ANY DlL's. Code re-use is limited to making a COPY of a file in the new project's folder. It's just crap, and a huge step AWAY from code re-use.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
Isn't using a Source Control product the modern version of pointing your C/C++ project to a file on your hard drive?
led mike
|
|
|
|
|
Nope, not when you're dealing with .Net.
"Why don't you tie a kerosene-soaked rag around your ankles so the ants won't climb up and eat your candy ass..." - Dale Earnhardt, 1997 ----- "...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
You can add source code as a link, I do that a lot when I don't want to include my whole library.
I agree (and have since my first experience with VS ten years ago) that VS should not copy files by default.
|
|
|
|
|
Ok...I now have many follow up questions to this dilemma.
If you can't just link in a DLLs source tree, what is the purpose of having a solution and not just opening up multiple IDEs with multiple projects?
If I just reference the DLL, when I step into a function debugging, will it know where to find the source for that DLL?
This DLL I already have has common data access routines in it that is used by a service I created. I would like to access those same routines with a new service. I may also need to add more functions, but like I said, I would like to be able to step into those routines and debug.
|
|
|
|
|
mobius111001 wrote: If you can't just link in a DLLs source tree
You can, no one said you could not.
mobius111001 wrote: I step into a function debugging
If it were me, I would not just reference a DLL that I did not trust had been tested and debugged. If you cannot trust the functionality of the DLL you would probably be way better off adding the project to your solution.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
|
|
|
|
|
Ok, so what is the proper way to add a project to a solution so that a second copy of the code isn't created?
|
|
|
|
|
mobius111001 wrote: proper way to add a project to a solution
I suppose that depends on the version of VS you are using. In VS 2008, right click on the solution, select Add, then select Existing project.
Why is common sense not common?
Never argue with an idiot. They will drag you down to their level where they are an expert.
Sometimes it takes a lot of work to be lazy
Individuality is fine, as long as we do it together - F. Burns
|
|
|
|
|
mobius111001 wrote: is there a better solution?
I have always found using a source control product very productive.
led mike
|
|
|
|
|
Yes you can. I do. Otherwise, I can't step into that code while debugging.
|
|
|
|
|
Can anyone give me a detailed step by step procedure to deploy a C# application in visual studio 2005 with sql server 2005 databse ? I had developed an application but I can't deploy it because I don't know the procedure to deploy it. When I deploy it is working on my machine but on client's pc it throws an exception that cannto Login to the database.
|
|
|
|
|
|
i need to change mouse cursor outside form.
how can i do it?, can i use user32.dll?
please give some tip or hint or example to me.
thanks a lot....^^
|
|
|
|
|
ping_jacob wrote: change mouse cursor outside form
What do you mean?
only two letters away from being an asset
|
|
|
|
|