Click here to Skip to main content
15,867,968 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hey guys, im making a video handler and i want to play videos on my webpage from DB.

but this site is user based so i have sessions to keep my info running between pages.

The problem occurs when after i've chosen the video file i press upload.After the Response.Redirect(Request.Url.AbsoluteUri);
So i get null exception..
I tried to use Response.Redirect("~/Default.aspx?Email=" + PID);

the page loaded ok after the upload but i didn't have the video playing, so im thinking if absoluteURI is needed for the video to work...

Any ideas? here's the code , stuff happens at BindGrid(); and bttnUpload.

THANKS :)

C#
namespace DisplayingImages
{
    public partial class Default : System.Web.UI.Page
    {

        public string query, constr, query1, query2, query3;
        public SqlConnection con, conn;
        public void connection()
        {
            constr = ConfigurationManager.ConnectionStrings["Myconnection"].ToString();
            con = new SqlConnection(constr);
            con.Open();
        }




        protected void Page_Load(object sender, EventArgs e)
        {
            Label1.Visible = false;
            PID = (int)(Session["id"]);
            if (!IsPostBack)
            {



                HttpContext context = HttpContext.Current;
                UserProfile objUserProfile = Session["UserData_" + PID] as UserProfile;
                lblemail.Text = objUserProfile.First_Name;
                lblname.Text = objUserProfile.Email_Account;



                if (PID == null)
                {
                    Response.Redirect("Login.aspx");
                }

                imagebindGrid();
                PostSelection();

            }
        }

        private void InitializeComponent()
        {
            throw new NotImplementedException();
        }


        protected void upload(object sender, EventArgs e) // button για το upload της φωτο. Καλειται η κλαση Imageupload()
        {

            Imageupload();
        }
        /* Κλασση για upload εικονας . Ελεγχος και περασμα εικονας */
        private void Imageupload()
        {
            if (FileUpload1.HasFile)
            {
                PID = (int)(Session["id"]);
                if (PID != null)
                {
                    int imagefilelenth = FileUpload1.PostedFile.ContentLength;
                    byte[] imgarray = new byte[imagefilelenth];
                    HttpPostedFile image = FileUpload1.PostedFile;
                    image.InputStream.Read(imgarray, 0, imagefilelenth);
                    connection();
                    query = "Insert into  ImageToDB (user_id,ImageName,Image) values (@user_id,@Name,@Image)";
                    SqlCommand com = new SqlCommand(query, con);
                    com.Parameters.AddWithValue("@Name", SqlDbType.VarChar).Value = TextBox1.Text;
                    com.Parameters.AddWithValue("@Image", SqlDbType.Image).Value = imgarray;
                    com.Parameters.AddWithValue("@user_id", PID);
                    com.ExecuteNonQuery();
                    Label1.Visible = true;
                    Label1.Text = "Image Is Uploaded successfully";
                    imagebindGrid();
                    BindGrid();

                }
            }
        }



        /* Gridview για εικονες */
        public void imagebindGrid()
        {
            connection();

            //SQL Injection Prevention!
            query = "SELECT id,ImageName,Image from ImageToDB where user_id= (@PerID)";
            SqlCommand com = new SqlCommand(query, con);
            com.Parameters.Add(new SqlParameter("PerID", PID));
            // com.ExecuteReader();
            SqlDataReader dr = com.ExecuteReader();
            dr.Read();


            Image1.ImageUrl = "Handler1.ashx?id_Image=" + PID;

        }

        /*Logout Button */
        protected void Button1_Click(object sender, EventArgs e)
        {

            System.Web.Security.FormsAuthentication.SignOut();
            Session.Clear();
            Session.RemoveAll();
            Session.Abandon();
            Response.Cache.SetExpires(DateTime.UtcNow.AddMinutes(-1));
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.Cache.SetNoStore();
            HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache, no-store, must-revalidate");
            HttpContext.Current.Response.AddHeader("Pragma", "no-cache");
            HttpContext.Current.Response.AddHeader("Expires", "0");
            FormsAuthentication.SignOut();
            HttpContext.Current.User = new GenericPrincipal(new GenericIdentity(string.Empty), null);
            Response.Redirect("~/Logout.aspx");
        }
        /* Κλασση για το Post */
        private void Txt()
        {



            try
            {
                PID = (int)(Session["id"]);
                if (PID != null)
                {
                    connection();
                    query1 = "Insert into  Posttext (user_id,Posttext) values (@user_id,@Your_Post)";
                    SqlCommand com2 = new SqlCommand(query1, con);
                    com2.Parameters.AddWithValue("@Your_Post", SqlDbType.VarChar).Value = PostBox.Text;
                    com2.Parameters.AddWithValue("@user_id", PID);
                    com2.ExecuteNonQuery();
                    lbluser.Text = PostBox.Text;
                    PostSelection();
                }

            }



            catch (Exception ex)
            {
                //con.Close();
            }

        }
        /* Κανει select τα κειμενα και τα ανεβαζει απο την βαση στο grid */
        private void BindGrid()
        {
            string strConnString = ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                using (SqlCommand cmd = new SqlCommand())
                {
                    cmd.CommandText = "select * tblFiles";
                    cmd.Connection = con;
                    con.Open();
                    DataList1.DataSource = cmd.ExecuteReader();
                    DataList1.DataBind();
                    con.Close();
                }
            }
        }

        protected void btnUpload_Click(object sender, EventArgs e)
        {
            using (BinaryReader br = new BinaryReader(FileUpload1.PostedFile.InputStream))
            {
                byte[] bytes = br.ReadBytes((int)FileUpload1.PostedFile.InputStream.Length);
                string strConnString = ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString;
                using (SqlConnection con = new SqlConnection(strConnString))
                {
                    using (SqlCommand cmd = new SqlCommand())
                    {
                        cmd.CommandText = "insert into tblFiles(Name, ContentType, Data) values (@Name, @ContentType, @Data)";
                        cmd.Parameters.AddWithValue("@Name", Path.GetFileName(FileUpload1.PostedFile.FileName));
                        cmd.Parameters.AddWithValue("@ContentType", "video/mp4");
                        cmd.Parameters.AddWithValue("@Data", bytes);
                        cmd.Connection = con;
                        con.Open();
                        cmd.ExecuteNonQuery();
                        con.Close();
                    }
                }
            }
            Response.Redirect(Request.Url.AbsoluteUri);
          //  Response.Redirect("~/Default.aspx?Email=" + PID);
        }


        public void PostSelection()
        {
            connection();

            //SQL Injection Prevention!!!!
            query2 = "Select Posttext from Posttext where user_id= (@PerID)";
            SqlCommand com1 = new SqlCommand(query2, con);
            com1.Parameters.Add(new SqlParameter("PerID", PID));


            SqlDataReader Read = com1.ExecuteReader();
            grdemployee7.DataSource = Read;
            grdemployee7.DataBind();
            Read.Close();

        }
        /* Καλειται η Txt() για να γινει το Post */
        protected void Button2_Click(object sender, EventArgs e)
        {
            Txt();
        }

        public string USER_PID { get; set; }
        public DateTime _myid { get; set; }

        /* --------------------Κουμπι για search PROFILE -----------------------------------*/
        public void Button3_Click1(object sender, EventArgs e)
        {
            Response.Redirect("~/WebForm7.aspx");
        }


        public int PID { get; set; }

        protected void PostBox_TextChanged(object sender, EventArgs e)
        {

        }

        public string sqlquer { get; set; }

        public object SqlQueryIn { get; set; }

        protected void Button4_Click(object sender, EventArgs e)
        {

        }
    }
}
Posted

1 solution

Look at MSDN documentation:

http://msdn.microsoft.com/en-us/library/a8wa7sdt(v=vs.110).aspx[^]

Specify the endResponse as false parameter:

Response.Redirect(url, false);
 
Share this answer
 
Comments
Maciej Los 5-Nov-14 15:01pm    
+5
Manas Bhardwaj 5-Nov-14 15:05pm    
thx Maciej :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900