Wednesday, June 22, 2016

File Upload in ASP.Net

 protected void Button1_Click(object sender, EventArgs e)
    {
        //code to Insert into database
        string nm = TextBox1.Text.Trim();
        string usernm = TextBox2.Text.Trim();
        string pass = TextBox3.Text.Trim();
        string phone = TextBox4.Text.Trim();
        if (FileUpload1.HasFile) {
            string name=FileUpload1.PostedFile.FileName;
            string ext=System.IO.Path.GetExtension(name);

            if(!(ext==".jpg" || ext==".png" || ext == ".bmp")){
            Label8.Text="Invalid File Type!! Only Images(jpg,bmp or png) allowed!";
                return;
            }
            else if(FileUpload1.PostedFile.ContentLength > 100000){
            Label8.Text="Image too large!! Only Upto 100Kb Allowed!!";
                return;
            }
            string filename = DateTime.Now.ToFileTime() + usernm+ ext;
            string ins = "insert into register(username,name,password,phone,image) values('"+usernm+"','"+name+"','"+pass+"','"+phone+"','"+filename+"')";
            FileUpload1.SaveAs(Server.MapPath("Images") + "/"+filename);
            int i = dbase.insert(ins);
            if (i == 1) {
                Label8.Text = "Registered successfully!!";
             }
            else
                Label8.Text = "Error while saving!!!!";
           
        }
    }

Wednesday, June 15, 2016

DBASE Class

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

///
/// Summary description for dbase
///
public class dbase
{
public dbase()
{
//
// TODO: Add constructor logic here
//
}
    public static DataSet select(string qu) {
        try
        {

            DataSet ds = new DataSet();
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
            SqlCommand cmd = new SqlCommand(qu, con);
            con.Open();
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            adp.Fill(ds);
            con.Close();
            return ds;
         
        }
        catch (Exception exp)
        {

            return null;
        }
    }
    public static int insert(string qu) {
        try
        {
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
            SqlCommand cmd = new SqlCommand(qu, con);
            con.Open();
            int ans = cmd.ExecuteNonQuery();
            con.Close();
            return ans;
        }
        catch (Exception exp) {
            return -1;
        }
    }

}

ASP.Net Edit Course Code

Edit Course Cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
public partial class admin_EditCourse : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            if (Request.QueryString["cid"] == null)
            {
                Response.Redirect("~/admin/ViewCourse.aspx", true);
            }
            string cid = Request.QueryString["cid"].ToString();
            string qu = "select coursename from course where courseid=" + cid;
            DataSet ds = dbase.select(qu);
            if (ds != null && ds.Tables.Count == 1)
            {
                TextBox1.Text = ds.Tables[0].Rows[0]["coursename"].ToString();
                RequiredFieldValidator1.InitialValue = TextBox1.Text;
            }
            lblCourseId.Text = cid;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string up = TextBox1.Text;
        string qu = "update course set coursename='"+up+"' where courseid="+lblCourseId.Text;
        int i = dbase.insert(qu);
        if (i == 1) {
        //Successful
            ScriptManager.RegisterStartupScript(this, this.GetType(), "js1",
                "alert('Saved Successfully!!');window.location='ViewCourse.aspx'", true);
          }
        else {
        //Error unsuccesful
            ScriptManager.RegisterStartupScript(this, this.GetType(), "js2",
      "alert('Error While Saving. Try Again later');window.location='ViewCourse.aspx'", true);
        }
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/admin/ViewCourse.aspx");
    }
}


Thursday, June 9, 2016

Sending Attachment through PHP mail function

Sending Attachment using mail() function :
   // request variables // important
   $from = $_REQUEST["from"];
   $emaila = $_REQUEST["emaila"];
   $filea = $_REQUEST["filea"];
 
   if ($filea) {
      function mail_attachment ($from , $to, $subject, $message, $attachment){
         $fileatt = $attachment; // Path to the file
         $fileatt_type = "application/octet-stream"; // File Type
       
         $start = strrpos($attachment, '/') == -1 ?
            strrpos($attachment, '//') : strrpos($attachment, '/')+1;

         $fileatt_name = substr($attachment, $start,
            strlen($attachment)); // Filename that will be used for the
            file as the attachment
       
         $email_from = $from; // Who the email is from
         $subject = "New Attachment Message";
       
         $email_subject =  $subject; // The Subject of the email
         $email_txt = $message; // Message that the email has in it
         $email_to = $to; // Who the email is to
       
         $headers = "From: ".$email_from;
         $file = fopen($fileatt,'rb');
         $data = fread($file,filesize($fileatt));
         fclose($file);
       
         $msg_txt="\n\n You have recieved a new attachment message from $from";
         $semi_rand = md5(time());
         $mime_boundary = "==Multipart_Boundary_x{$semi_rand}x";
         $headers .= "\nMIME-Version: 1.0\n" . "Content-Type: multipart/mixed;\n" . "
            boundary=\"{$mime_boundary}\"";
       
         $email_txt .= $msg_txt;

         $email_message .= "This is a multi-part message in MIME format.\n\n" .
            "--{$mime_boundary}\n" . "Content-Type:text/html;
            charset = \"iso-8859-1\"\n" . "Content-Transfer-Encoding: 7bit\n\n" .
            $email_txt . "\n\n";

         $data = chunk_split(base64_encode($data));
       
         $email_message .= "--{$mime_boundary}\n" . "Content-Type: {$fileatt_type};\n" .
            " name = \"{$fileatt_name}\"\n" . //"Content-Disposition: attachment;\n" .
            //" filename = \"{$fileatt_name}\"\n" . "Content-Transfer-Encoding:
            base64\n\n" . $data . "\n\n" . "--{$mime_boundary}--\n";

         $ok = mail($email_to, $email_subject, $email_message, $headers);
       
         if($ok) {
            echo "File Sent Successfully.";
            unlink($attachment); // delete a file after attachment sent.
         }else {
            die("Sorry but the email could not be sent. Please go back and try again!");
         }
      }
      move_uploaded_file($_FILES["filea"]["tmp_name"],
         'temp/'.basename($_FILES['filea']['name']));

      mail_attachment("$from", "youremailaddress@gmail.com",
         "subject", "message", ("temp/".$_FILES["filea"]["name"]));
   }
?>