naidelih says: Hello World!!!

just my online notebook

Displaying picture from database using ASHX Handler

ShowImage.aspx code

  1. Imports System.Web
  2. Imports System.Web.Services
  3. Imports System
  4. Imports System.Configuration
  5. Imports System.IO
  6. Imports System.Data
  7. Imports System.Data.SqlClient
  8.  
  9. Public Class ShowImage
  10.     Implements System.Web.IHttpHandler
  11.  
  12.     Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
  13.         Dim mRefID As Int32
  14.         If Not context.Request.QueryString("id") Is Nothing Then
  15.             mRefID = Convert.ToInt32(context.Request.QueryString("id"))
  16.         Else
  17.             Throw New ArgumentException("No parameter specified")
  18.         End If
  19.  
  20.         Dim conn As String = ConfigurationManager.ConnectionStrings("ConnStr").ConnectionString
  21.         Dim connection As SqlConnection = New SqlConnection(conn)
  22.         Dim sql As String = "SELECT Photo FROM tblPhoto WHERE PhotoID = @ID"
  23.         Dim cmd As SqlCommand = New SqlCommand(sql, connection)
  24.         cmd.CommandType = CommandType.Text
  25.         cmd.Parameters.AddWithValue("@ID", mRefID)
  26.         connection.Open()
  27.         Dim img As Byte() = DirectCast(cmd.ExecuteScalar(), Byte())
  28.         Try
  29.             context.Response.ContentType = "image/jpeg"
  30.             context.Response.OutputStream.Write(img, 0, img.Length)
  31.  
  32.         Catch
  33.  
  34.         Finally
  35.             connection.Close()
  36.         End Try
  37.  
  38. End Sub

Usage

  1.  <asp:Image ID="imgViewPhoto" runat="server"  />
  2.  
  3. code behind
  4. imgViewPhoto.ImageUrl = "~/ShowImage.ashx?id=" & mPhotoID

Uploading file in php

<?php
//Сheck that we have a file
if((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
 //Check if the file is JPEG image and it's size is less than 350Kb
 //$filename = basename($_FILES['uploaded_file']['name']);
 $origfilename = basename($_FILES['uploaded_file']['name']);
 $filename=uniqid().".jpg";
 $ext = substr($origfilename, strrpos($origfilename, '.') + 1);
 if (($ext == "jpg") && ($_FILES["uploaded_file"]["type"] == "image/jpeg") &&
 ($_FILES["uploaded_file"]["size"] < 100000)) {
 //Determine the path to which we want to save this file
 $newname = dirname(__FILE__).'/pics/'.$filename;
 //Check if the file with the same name is already exists on the server
 if (!file_exists($newname)) {
 //Attempt to move the uploaded file to it's new place
 if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'],$newname))) {
 //if no errors then save record

 $msg="It's done! The file has been saved as: ".$newname;
 }

 } else {
 $msg=" Error: A problem occurred during file upload!";
 }
 } else {
 $msg="Error: File ".$_FILES["uploaded_file"]["name"]." already exists";
 }
 } else {
 $msg="Error: Only .jpg images under 100Kb are accepted for upload";
 }
} else {
 $msg="Error: No file uploaded";
}
?>

tutorial url rewriting in asp.net

A tutorial on url rewriting in asp.net

http://www.asp.net/general/videos/how-do-i-implement-url-rewriting

A nice php datepicker

A good datepicker/calendar for php web application.

http://www.triconsole.com/php/calendar_datepicker.php

Exporting Crystal Report to MS Word

 Private Sub ExportToWord(ByVal mDS As DataSet, ByVal mDocName As String, ByVal mReportPath As String, ByVal mSelectFormula As String, ByVal mSaveLoc As String)
 Me.Cursor = Cursors.WaitCursor
 Dim mDBLocation As String
 mDBLocation = mCurrentDirectory & "\" & "DB.mdb"
 Try
 Dim rptDocument As New CrystalDecisions.CrystalReports.Engine.ReportDocument
 rptDocument.Load(mReportPath)
 SetupReport(rptDocument, mDBLocation) ' define in previous post
 rptDocument.VerifyDatabase()
 rptDocument.SetDataSource(mDS)
 rptDocument.RecordSelectionFormula = mSelectFormula
 rptDocument.Refresh()
 Dim strExportFile As String = mSaveLoc & "\" & ReplaceCharFilename(mDocName) & ".doc"
 rptDocument.ExportOptions.ExportDestinationType = ExportDestinationType.DiskFile
 rptDocument.ExportOptions.ExportFormatType = ExportFormatType.WordForWindows
 Dim objOptions As DiskFileDestinationOptions = New DiskFileDestinationOptions()
 objOptions.DiskFileName = strExportFile
 rptDocument.ExportOptions.DestinationOptions = objOptions
 rptDocument.Export()
 objOptions = Nothing
 rptDocument = Nothing
 mDS = Nothing

 Catch ex As Exception
 MessageBox.Show("Report(Print Report) : " & ex.Message, "ProjectName", MessageBoxButtons.OK, MessageBoxIcon.Stop)

 Finally
 End Try
 Me.Cursor = Cursors.WaitCursor
 End Sub

Solving the Operation Must Use An Updateable Query error

I created a vb.net application and run flawlessly in a xp machine until a client/user installed it in his windows vista pc. The problem arises  when a user load the crystal report parent form and goes back to the manage window form, Adding, editing appears not to be working  and changes made did not reflect. You can only see the changes by reloading the application. And im using access as my database.

I turn on debug and i got an “Operation Must Use An Updateable Query” error. There seems a lot of reason why this error occur. My first suspect was the file restriction in windows vista as the application is installed by default in Program files and that the application runs smoothly on xp. I googled for help and was able to find the solution.  Adding write permission to the Users Account[ Users(Naidelih/Users) ] for the root folder of the application in the Program file folder solved the problem.

Though this problem came from different reasons, it is bes to start with the file permissions.

sending email using pop3 account in php

Example on sending email using pop3 account. This example uses the phpmailer class.

function pop3email ($msubject, $message){
  1.  $pop3server="mail.domain.com"; // pop3 server
  2.         $pop3user="info@domain.com";
  3.         $pop3pw="password";
  4.  require_once('class.phpmailer.php');
  5.  require_once('class.pop3.php'); // required for POP before SMTP
  6.  
  7.  $pop = new POP3();
  8.  $pop-&gt;Authorise($pop3server, 110, 30, $pop3user, $pop3pw, 1);
  9.  $mail = new PHPMailer();
  10.  $body             = $message;
  11.  $body             = eregi_replace("[\]",'',$body);
  12.  
  13.  $mail-&gt;IsSMTP();
  14.  $mail-&gt;SMTPDebug = 0;
  15.  $mail-&gt;Host     = $pop3server;
  16.  $mail-&gt;SetFrom($pop3user, 'sender name');
  17.  $mail-&gt;AddReplyTo($pop3user, 'reply to');
  18.  $mail-&gt;Subject    = $msubject;
  19.  $mail-&gt;AltBody    = "To view the message, please use an HTML compatible email viewer!"; // optional, comment out and test
  20.  $mail-&gt;MsgHTML($body);
  21.  $address = "recipient@domain.com";
  22.  $mail-&gt;AddAddress($address, "Test");
  23.  
  24.  if(!$mail-&gt;Send()) {
  25.   // echo "Mailer Error: " . $mail-&gt;ErrorInfo;
  26.    return false;
  27.  } else {
  28.   // echo "Message sent!";
  29.    return true;
  30.  }
  31. }

Download the required class here.