|
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
|
|
|
|
|
|
Hello,
how can I link a hscrollbar to a picturebox ? (I need to get the offset from hscrollbar.Value, so that I can calculate the horizontal coordinates of a point in the image) I believe I must use an OnPaint event, but do not know how to use it. A portion of code would be appreciated.
thanks
pcjd
|
|
|
|
|
Are you asking how to determine the clicked point on an image, even if the image is larger than the area it's being displayed in and the image is scrolled to one side??
If so, you don't need the scroll offset.
Create a UserControl, put a Panel on the UserControl and set it's AutoScroll property to true. Then put another Panel inside the first one. Expose a property in the UserControl to take an Image. In the setter for the Image, set the second Panel's size to match the image size. Now, when you click on the second Panel (the one showing the image), you handle the Click property of that Panel control. You'll get the coordinates of the click, relative to the upper left corner of the image Panel.
|
|
|
|
|
Hello,
yes I am trying to get the coordinates of a clicked image - the absolute
coordinates - not those relative to the top corner of the panel, but
relative to the top corner of the image when I've scrolled way past it. I've already tried your method, which is why I want the offset.
thanks
pcjd
|
|
|
|
|
Offset relative to what?? I don't understand your explanation.
|
|
|
|