Hi,
I am testing inputin data in my database. The first I noticed is that most times when I run the website, I get a blank page that say Application_Offline.
Now the site loads fine, but when I try to insert data in the database using the DetailsView control, I keep getting the message: Internet Explorer cannot display te webpage.
Can anyone help me out please?
This is mostly happening with the Image.aspx page. Here is the code-behind for it.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class Admin_Images : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//if the page is not a post back, ensure that the GridView is
//displaying all images...
//if (!Page.IsPostBack)
//{
int id = Convert.ToInt16(serviceDropDownList1.SelectedItem.Value);
if (id == 0)
{
//! The value of the first item in the dropdown list has been
//!set to 0; therefore, once this value is 0, it is certain that
//!the selected item is not a valid Service. At this point, simply
//!display all images.
GridView1.DataSourceID = "ServiceSqlDataSource";
GridView1.DataBind();
}
//}
errorLabel.Visible = false;
}
protected void serviceDropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
int id = Convert.ToInt16(serviceDropDownList1.SelectedItem.Value);
if (id == 0)
{
//GridView1.DataSourceID = "";
GridView1.DataSourceID = "ServiceSqlDataSource";
GridView1.DataBind();
}
else
{
GridView1.DataSourceID = "SqlDataSource2";
GridView1.DataBind();
}
}
protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
//Get the currently selected service
int id = Convert.ToInt32(serviceDropDownList1.SelectedItem.Value);
//if the selected item in the dropdown list is not a valid service
if (id == 0)
{
e.Cancel = true;
errorLabel.Text = @"You cannot add an image when there is no service
selected. Please select the service from the dropdown
and then try again.";
errorLabel.Visible = true;
}
else
{
e.Values["ServiceID"] = id; //Set the ServiceID value in the database
e.Values["DateAdded"] = DateTime.Now; //Set the DateAdded value in the database
//Find the FileUpload control in the DetailsView
FileUpload imageUpload = (FileUpload)DetailsView1.FindControl("FileUpload1");
string virtualFolder = "~/ServiceImages/"; //set the virtual path of the folder
string physicalFolder = Server.MapPath(virtualFolder); //set the physical server location
string filename = Guid.NewGuid().ToString(); //generate a unique name for the file
string extension = Path.GetExtension(imageUpload.FileName); //get the extension of the file
//save the file to a physical server location
imageUpload.SaveAs(Path.Combine(physicalFolder, filename + extension));
//save the virtual url in the database
e.Values["ImageUrl"] = string.Format("{0}{1}{2}", virtualFolder, filename, extension);
}
}
protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)
{
string deletedUrl = Server.MapPath(Convert.ToString(e.Values["ImageUrl"]));
File.Delete(deletedUrl);
}
}
Thank you...