вторник, 28 сентября 2010 г.

SqlDataSource Optional Parameters

I want to use it to populate a GridView, but using an optional parameter to filter the results.


One way to do this is to use Optional Parameters: http://steve.emxsoftware.com/Optional+Parameters+in+SQL+Server+Search+Queries.


If you adopt this approach, make sure to set the CancelSelectOnNullParameter property of your DataSource control to false.

суббота, 18 сентября 2010 г.

How To Set the Image dpi to 300 in PHP



<?php
//Get jpeg image contents
ob_start();
imagejpeg($image""100);
$image ob_get_contents();
ob_end_clean();
//Or read jpeg image
$image file_get_contents($file);
//Replace header image info to set DPI as units and the density for X and Y
//First parameter are units for X and Y densities (0x00 No units, 0x01 DPI, 0x02 DPC)
//Second parameter is the X density
//Third parameter is the Y density

$image substr_replace($imagepack("Cnn"0x01300300), 135);
header("Content-type: image/jpeg");
header('Content-Disposition: attachment; filename="'.basename($file).'"'); 

echo 
$image;?>

пятница, 17 сентября 2010 г.

Using SELECT to INSERT records



Edwin writes "Ok, this may be simple to some, but it sure is a puzzle to me. I want to move a set of data from one table to another table with a similar structure. What I figure was a shady solution: Selecting the data from the source table, then opening the other table and using a loop to populate the destination table. Question: Is there a way I can use just ONE insert statement to do all this? Sort of like incorporating the select statement into the insert statement? If there is one, then please give me some example code." I certainly can do this in one statement (but looks like two). (This article has been updated through SQL Server 2005.)


I'll use the SELECT statement in conjunction with the INSERT statement to make this as easy as possible. Normally, I would code an INSERT statement something like this (using the pubs database):
INSERT authors (au_id, au_lname, au_fname, contract)
VALUES ('123-45-6789', 'Gates', 'Bill', 1)
This will insert one row into the authors table. I could write a program to loop through a set of records and insert them one at a time into another table. SQL Server is designed for set processing. It is optimized to handle groups or sets of records. I can actually replace the VALUES clause with a SELECT statement that will return a set of records. Suppose I have a table called CALIFORNIA_AUTHORS and we want to populate it with the ID and names of the authors from California. The statement would look something like this:
INSERT california_authors (au_id, au_lname, au_fname)
SELECT au_id, au_lname, au_fname
FROM authors
WHERE State = 'CA'
This will take the 15 records with State='CA' and load them into the table CALIFORNIA_AUTHORS. I can use any type of SELECT statement here. It just has to return a record set that matches the columns in the INSERT statement. The number of columns and their data types must match (or be implicitly convertible). I can also execute a stored procedure that returns a record set using the EXEC command in place of the SELECT statement.

понедельник, 13 сентября 2010 г.

WPF Open Directory Dialog




Unfortunately, WPF doesn't have a folder browser dialog. You should use the System.Windows.Forms.FolderBrowserDialog class for this. Don't mind that it's in theSystem.Windows.Forms namespace.


var dialog = new System.Windows.Forms.FolderBrowserDialog();
System.Windows.Forms.DialogResult result = dialog.ShowDialog();

Installing Fonts for Photoshop Only



Here is a great tip from Matthew Feist, originally posted in the graphics software discussion forum:
I read your article on fonts Sue and was very impressed, but after 3 hours of tearing my hair out, I have finally solved my question asked earlier. This might even be worthy of a TIP of the day for this forum. Anyway, I asked earlier how to install the fonts so Windows would not see them but Photoshop 5.5 would. After searching through that wonderful document we all take for granted but never follow instructions from or bother to read, (yes, thats right, the README.txt file), I was overjoyed to find that you can install your fonts that you don't want windows to see over to this location:
C:\Program Files\Common Files\Adobe\Fonts


The good thing about it is that you can keep your font collection to above 1000 like I am, and not have to sacrifice preformance by installing them into the Windows FONTS directory. The only bad thing is that Photoshop takes longer to load. Oh well.

воскресенье, 12 сентября 2010 г.

How to Convert image to string in PHP



I have been looking to send the output from GD to a text string without proxying via a file or to a browser.

I have come up with a solution.

This code buffers the output between the ob_start() and ob_end() functions into ob_get_contents()



See the example below

<?php
// Create a test source image for this example
$im imagecreatetruecolor(30050);
$text_color imagecolorallocate($im2331491);
imagestring($im155,  'A Simple Text String'$text_color);

// start buffering
ob_start();
// output jpeg (or any other chosen) format & quality
imagejpeg($imNULL85);
// capture output to string
$contents ob_get_contents();
// end capture
ob_end_clean();

// be tidy; free up memory
imagedestroy($im);

// lastly (for the example) we are writing the string to a file
$fh fopen("./temp/img.jpg""a+" );
    fwrite$fh$contents );
fclose$fh );
?> 

PHP: Sending Email (Text/HTML/Attachments)



Email is the most popular Internet service today. A plenty of emails are sent and delivered each day. The goal of this tutorial is to demonstrate how to generate and send emails in PHP.
So, you want to send automated email messages from your PHP application. This can be in direct response to a user's action, such as signing up for your site, or a recurring event at a set time, such as a monthly newsletter. Sometimes email contains file attachments, both plain text and HTML portions, and so on. To understand how to send each variation that may exist on an email, we will start with the simple example and move to the more complicated.
Note that to send email with PHP you need a working email server that you have permission to use: for Unix machines, this is often Sendmail; for Windows machines, you must set the SMTP directive in your php.ini file to point to your email server.


Sending a Simple Text Email
At first let's consider how to send a simple text email messages. PHP includes the mail() function for sending email, which takes three basic and two optional parameters. These parameters are, in order, the email address to send to, the subject of the email, the message to be sent, additional headers you want to include and finally an additional parameter to the Sendmail program. Themail() function returns True if the message is sent successfully and False otherwise. Have a look at the example:
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email';
//define the message to be sent. Each line should be separated with \n
$message = "Hello World!\n\nThis is my first mail.";
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//send the email$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
As you can see, it very easy to send an email. You can add more receivers by either adding their addresses, comma separated, to the $to variable, or by adding cc: or bcc: headers. If you don't receive the test mail, you have probably installed PHP incorrectly, or may not have permission to send emails.
Sending HTML Email
The next step is to examine how to send HTML email. However, some mail clients cannot understand HTML emails. Therefore it is best to send any HTML email using a multipart construction, where one part contains a plain-text version of the email and the other part is HTML. If your customers have HTML email turned off, they will still get a nice email, even if they don't get all of the HTML markup. Have a look at the example:
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test HTML email';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash
$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/alternative; boundary=\"PHP-alt-".$random_hash."\"";
//define the body of the message.ob_start(); //Turn on output buffering
?>
--PHP-alt-
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--PHP-alt-
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!


This is something with HTML formatting.


--PHP-alt---
//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
In the preceding example we add one additional header of Content-type:multipart/alternative and boundary string that marks the different areas of the email. Note that the content type of the message itself is sent as a mail header, while the content types of the individual parts of the message are embedded in the message itself. This way, mail clients can decide which part of the message they want to display.
Sending Email with Attachment
The last variation that we will consider is email with attachments. To send an email with attachment we need to use the multipart/mixed MIME type that specifies that mixed types will be included in the email. Moreover, we want to use multipart/alternative MIME type to send both plain-text and HTML version of the email. Have a look at the example:
//define the receiver of the email
$to = 'youraddress@example.com';
//define the subject of the email
$subject = 'Test email with attachment';
//create a boundary string. It must be unique
//so we use the MD5 algorithm to generate a random hash

$random_hash = md5(date('r', time()));
//define the headers we want passed. Note that they are separated with \r\n
$headers = "From: webmaster@example.com\r\nReply-To: webmaster@example.com";
//add boundary string and mime type specification
$headers .= "\r\nContent-Type: multipart/mixed; boundary=\"PHP-mixed-".$random_hash."\"";
//read the atachment file contents into a string,
//encode it with MIME base64,
//and split it into smaller chunks

$attachment = chunk_split(base64_encode(file_get_contents('attachment.zip')));
//define the body of the message.
ob_start(); //Turn on output buffering
?>
--PHP-mixed-
Content-Type: multipart/alternative; boundary="PHP-alt-"

--PHP-alt-
Content-Type: text/plain; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!!!
This is simple text email message.

--PHP-alt-
Content-Type: text/html; charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Hello World!


This is something with HTML formatting.


--PHP-alt---

--PHP-mixed-
Content-Type: application/zip; name="attachment.zip"
Content-Transfer-Encoding: base64
Content-Disposition: attachment


--PHP-mixed---

//copy current buffer contents into $message variable and delete current output buffer
$message = ob_get_clean();
//send the email
$mail_sent = @mail( $to, $subject, $message, $headers );
//if the message is sent successfully print "Mail sent". Otherwise print "Mail failed" 
echo $mail_sent ? "Mail sent" : "Mail failed";
?>
As you can see, sending an email with attachment is easy to accomplish. In the preceding example we have multipart/mixed MIME type, and inside it we have multipart/alternative MIME type that specifies two versions of the email. To include an attachment to our message, we read the data from the specified file into a string, encode it with base64,  split it in smaller chunks to make sure that it matches the MIME specifications and then include it as an attachment.

вторник, 7 сентября 2010 г.

WPF OpenFileDialog() locks the folder




I use OpenFileDialog() in my Silverlight application. When I select a file using ShowDialog() it simply locks the file until I close my application.
I am not able to rename or delete the folder when the application is running (silverlight application in browser)
If I try to select any other file in any another folder, I am able to rename the previous folder. It seems it is releasing the handle.
My goal: I want to rename/delete the folder in filesystem (manually) once I finished uploading.




By default the dialog changes the process' current directory (as in System.Environment.CurrentDirectory) to the one the selected file is in. This is so it can default to the same directory again if you open the dialog a second time.It's not the dialog itself that keeps the directory "locked", it's the fact that it's the current directory for the process. Anyway, you can suppress this behavior by setting the dialog's RestoreDirectory property to true.