public partial class admin_series : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string path="";
        if (this.FileUpload1.HasFile)//检查是否有文件
        {
            string fullFileName = this.FileUpload1.PostedFile.FileName;                     //文件路径名
            string fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);   //图片名称
            string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);        //图片格式            if (type == "jpg" || type == "JPG" || type == "gif" || type == "GIF" || type == "BMP" || type == "bmp") //判断是否为图片类型
            {
                if (this.FileUpload1.PostedFile.ContentLength > 500 * 1024)
                {
                    Response.Write("<script>alert('上传图片必须小于500k!');</script>");
                }
                else
                {
                    path = HttpContext.Current.Request.MapPath("~/pic/");//获取上传文件的网站目录路径
                    this.FileUpload1.SaveAs(path + fileName);//存储文件到磁盘
                    Response.Write("<script>alert('图片上传成功!');</script>");//提示
                    this.Image1.ImageUrl = "~/pic/" + fileName;//显示图片
                }
            }
            else
            {
                Response.Write("<script>alert('非图片类型,不允许上传!');</script>");
            }
            string sqlConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
            SqlConnection con = new SqlConnection(sqlConnString);
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "insert into series(series,typeid,pic,note) values(@series,@typeid,@pic,@note)";
            string series = TextBox1.Text;
            string typeid = "";
            if (DropDownList1.SelectedItem != null)
            {
                typeid = DropDownList1.SelectedValue;
            }
            string note = txtTitle.Text;            cmd.Parameters.Add(new SqlParameter("@pic", path + fileName));
            cmd.Parameters.Add(new SqlParameter("@series", series));
            cmd.Parameters.Add(new SqlParameter("@typeid", typeid));
            cmd.Parameters.Add(new SqlParameter("@note", note));
            cmd.Connection = con;
            int sdr = cmd.ExecuteNonQuery();
            if (sdr == 0)
            {
                Response.Write("<script>alert(文件上传不成功!);</script>");///
            }
            else
            {
            }
            
            con.Close();
        }
        else
        {
            Response.Write("<script>alert('必须指定文件!');</script>");
        }    }
}哪位高手帮我看一下 页面显示没有问题 但是点确定录入的时候出现:
出现了运行时间错误
是否进行调试
行604
错误:例外被抛出且未被接住

解决方案 »

  1.   

    Response.Write("<script>alert(文件上传不成功!);</script>");///
    改为Response.Write("<script>alert('文件上传不成功!');</script>");///试一下
      

  2.   

    public partial class admin_series : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {    }
        string path = "";
        string fileName = "";
        protected void Button2_Click(object sender, EventArgs e)
        {
            
            if (this.FileUpload1.HasFile)//检查是否有文件
            {
                string fullFileName = this.FileUpload1.PostedFile.FileName;                     //文件路径名
                fileName = fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);   //图片名称
                string type = fullFileName.Substring(fullFileName.LastIndexOf(".") + 1);        //图片格式            if (type == "jpg" || type == "JPG" || type == "gif" || type == "GIF" || type == "BMP" || type == "bmp") //判断是否为图片类型
                {
                    if (this.FileUpload1.PostedFile.ContentLength > 500 * 1024)
                    {
                        Response.Write("<script>alert('上传图片必须小于500k!');</script>");
                    }
                    else
                    {
                        path = HttpContext.Current.Request.MapPath("~/pic/");//获取上传文件的网站目录路径
                        this.FileUpload1.SaveAs(path + fileName);//存储文件到磁盘
                        Response.Write("<script>alert('图片上传成功!');</script>");//提示
                        this.Image1.ImageUrl = "~/pic/" + fileName;//显示图片
                    }
                }
            }
            else
            {
                Response.Write("<script>alert('非图片类型,不允许上传!');</script>");
            }    }
        protected void Button1_Click(object sender, EventArgs e)
        {
                
                string sqlConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
                SqlConnection con = new SqlConnection(sqlConnString);
                con.Open();
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "insert into series(series,typeid,pic,note) values(@series,@typeid,@pic,@note)";
                string series = TextBox1.Text;
                string typeid = "";
                if (DropDownList1.SelectedItem != null)
                {
                    typeid = DropDownList1.SelectedValue;
                }
                string note = txtTitle.Text;            cmd.Parameters.Add(new SqlParameter("@pic", path + fileName));
                cmd.Parameters.Add(new SqlParameter("@series", series));
                cmd.Parameters.Add(new SqlParameter("@typeid", typeid));
                cmd.Parameters.Add(new SqlParameter("@note", note));
                cmd.Connection = con;
                int sdr = cmd.ExecuteNonQuery();
                if (sdr == 0)
                {
                    Response.Write("<script>alert('文件上传不成功!');</script>");///
                }
                else
                {
                    Response.Write("<script>alert('文件上传成功!');</script>");
                }
                con.Close();
        }}
    代码更新了一下 还是同样的情况,麻烦各位高手帮忙找找错!不管是按图片上传还是录入信息按钮 都弹出意外被抛出未接住。。
      

  3.   

    我说小哥儿,你就那么懒么?你把Button1_Click里的所有东西用try catch括一下断个点不就知道哪儿出问题了嘛?如果你Button1_Click力调用某个方法出现异常了,方法本身肯定会抛出来,但是你自己没有接。。错误信息已经说的很清楚了:例外被抛出且未被接住,而且告诉你行号了,他说你未被接住,你自己接一下不就可以了????