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 :)
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)
{
Imageupload();
}
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();
}
}
}
public void imagebindGrid()
{
connection();
query = "SELECT id,ImageName,Image from ImageToDB where user_id= (@PerID)";
SqlCommand com = new SqlCommand(query, con);
com.Parameters.Add(new SqlParameter("PerID", PID));
SqlDataReader dr = com.ExecuteReader();
dr.Read();
Image1.ImageUrl = "Handler1.ashx?id_Image=" + PID;
}
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");
}
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)
{
}
}
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);
}
public void PostSelection()
{
connection();
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();
}
protected void Button2_Click(object sender, EventArgs e)
{
Txt();
}
public string USER_PID { get; set; }
public DateTime _myid { get; set; }
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)
{
}
}
}