问题如题,在页面数据可以查看删除都没问题,就是添加修改就出现问题,高手帮我解决一下!代码如下:
Model层:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.ComponentModel;
/// <summary>
/// config 网站配置的摘要说明
/// </summary>
namespace Model
{
    public class Config
    {
        #region Public Properties        [DataObjectFieldAttribute(true, true, false)]
        public int ID { get; set; }        public string Title { get; set; }        public string Company { get; set; }        public string Tel { get; set; }        public string Fax { get; set; }        public string Website { get; set; }        public string Email { get; set; }        public string Address { get; set; }        public string Logopic { get; set; }        public string Copyright { get; set; }        public string Record { get; set; }        #endregion 
    }
}DAL层:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data.SqlClient;
using SQLHelper;
using Model;
/// <summary>
/// ConfigSQL 的摘要说明
/// </summary>
namespace DAL
{
    public class ConfigSQL
    {
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="config"></param>
        public void Update_Config(Config config)
        {
            SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();
            SqlParameter[] ParamList ={ 
                sqlHelper.CreateInParam("@Title",SqlDbType.NVarChar,50,config.Title),
                sqlHelper.CreateInParam("@Company",SqlDbType.NVarChar,50,config.Company),
                sqlHelper.CreateInParam("@Tel",SqlDbType.NVarChar,50,config.Tel),
                sqlHelper.CreateInParam("@Fax",SqlDbType.NVarChar,50,config.Fax),
                sqlHelper.CreateInParam("@Website",SqlDbType.NVarChar,50,config.Website),
                sqlHelper.CreateInParam("@Email",SqlDbType.NVarChar,50,config.Email),
                sqlHelper.CreateInParam("@Address",SqlDbType.NVarChar,255,config.Address),
                sqlHelper.CreateInParam("@Logopic",SqlDbType.NVarChar,255,config.Logopic),
                sqlHelper.CreateInParam("@Copyright",SqlDbType.NVarChar,50,config.Copyright),
                sqlHelper.CreateInParam("@Record",SqlDbType.NVarChar,50,config.Record)
            };
            try
            {
                sqlHelper.RunProc("Update_Config", ParamList);
            }
            catch (Exception ex)
            {
                SystemError.CreateErrorLog(ex.Message);
                throw new Exception(ex.Message, ex);
            }
        }
        /// <summary>
        /// 查看
        /// </summary>
        /// <returns></returns>
        public Config Get_Config()
        {
            SQLHelper.SQLHelper sqlHelper = new SQLHelper.SQLHelper();
            SqlDataReader rec=null;
            try
            {
                sqlHelper.RunProc("Get_Config", out rec);
            }
            catch (Exception ex)
            {
                SystemError.CreateErrorLog(ex.Message);
                throw new Exception(ex.Message, ex);
            }
            Config config = new Config();
            while (rec.Read())
            {
                config.Title = rec["Title"].ToString();
                config.Company = rec["Company"].ToString();
                config.Tel = rec["Tel"].ToString();
                config.Fax = rec["Fax"].ToString();
                config.Website = rec["Website"].ToString();
                config.Email = rec["Email"].ToString();
                config.Address = rec["Address"].ToString();
                config.Logopic = rec["Logopic"].ToString();
                config.Copyright = rec["Copyright"].ToString();
                config.Record = rec["Record"].ToString();
            }
            return config;
        }
    }
}BLL层:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.ComponentModel;
using DAL;
using Model;
/// <summary>
/// ConfigSystem 的摘要说明
/// </summary>
namespace BLL
{
    public class ConfigSystem
    {
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="config"></param>
        public void Update_Config(Config config)
        {
            ConfigSQL configSQL = new ConfigSQL();
            configSQL.Update_Config(config);
        }
        /// <summary>
        /// 查看
        /// </summary>
        /// <returns></returns>
        public Config Get_Config()
        {
            ConfigSQL configSQL = new ConfigSQL();
            return (configSQL.Get_Config());
        }
    }
}web提交数据代码:
protected void btnUpdate_Click(object sender, EventArgs e)
    {
        string sPic = Pic.Value.ToString();
        if (FileUpload1.HasFile)
        {
            string savePath = @"../UploadFiles/Image/";
           
            string savePath1 = @"../UploadFiles/Image/";
            
            string fileName = Server.HtmlEncode(FileUpload1.FileName);
            string extension = System.IO.Path.GetExtension(fileName);
            string ImageName = null;
            if ((extension == ".jpg") | (extension == ".gif"))
            {
                ImageName = DateTime.Now.ToString("yyyyMMddhhmmssfff") + extension;
                savePath += ImageName;
                savePath1 += sPic;
                savePath = Server.MapPath(savePath);
                savePath1 = Server.MapPath(savePath1);
                FileUpload1.SaveAs(savePath);
                //生成小图
                ImageClass.ImageClass imageClass = new ImageClass.ImageClass();
                imageClass.ShowThumbnail(savePath, savePath1, 200, 153);
                sPic = ImageName;
                //删除旧图片
                Lonely.Lonely.FilePicDelete(savePath1);
               
            }
            else
            {
                Response.Write("<script>alert('文件上传格式错误');history.back(1);</script>");
                Response.End();
            }
        }
        Config config = new Config();
        ConfigSystem configSystem = new ConfigSystem();
        config.Title = Title.Text.ToString();
        config.Company = Company.Text.ToString();
        config.Tel = Tel.Text.ToString();
        config.Fax = Fax.Text.ToString();
        config.Website = Website.Text.ToString();
        config.Email = Email.Text.ToString();
        config.Address = Address.Text.ToString();
        config.Logopic = sPic;
        config.Copyright = Copyright.Text.ToString();
        config.Record = Record.Text.ToString();
        configSystem.Update_Config(config);
        Response.Write("<script>alert('修改成功');location.href='Update_config.aspx';</script>");
    }SQLHelper 存储过程部分代码:
                   /// <summary>
/// 执行存储过程
/// </summary>
/// <param name="procName">存储过程名称</param>
/// <param name="prams">存储过程所需参数</param>
/// <returns>返回存储过程返回值</returns>
public int RunProc(string procName, SqlParameter[] prams) 
{
SqlCommand cmd = CreateProcCommand(procName, prams);
try
{
///执行存储过程
cmd.ExecuteNonQuery();
}
catch(Exception ex)
{
///记录错误日志
SystemError.CreateErrorLog(ex.Message);
}
finally
{
///关闭数据库的连接
Close();
}

///返回存储过程的参数值
return (int)cmd.Parameters[RETURNVALUE].Value;
}

解决方案 »

  1.   

    我跟进去了,发现有两个问题,
    一个错误是上面的存储过程中代码中:}
    catch(Exception ex)   为过程或函数 Update_Config 指定了过多的参数。
    {一个错误是上面的DAL代码中:
    catch (Exception ex)
    {
    SystemError.CreateErrorLog(ex.Message);
    throw new Exception(ex.Message, ex);    [color=#FF0000] 用户代码未处理Exception,未将对象引用设置到对象的实例.color]
    }可这样我也看不出原因,请帮忙看看,多谢先!
      

  2.   

    再帮我看看是差什么参数么!多谢!
    存储过程中CreateProcCommand的代码:
    private SqlCommand CreateProcCommand(string procName, SqlParameter[] prams) 
    {
    ///打开数据库连接
    Open();

    ///设置Command
    SqlCommand cmd = new SqlCommand(procName, myConnection);
    cmd.CommandType = CommandType.StoredProcedure; ///添加把存储过程的参数
    if (prams != null) 
    {
    foreach (SqlParameter parameter in prams)
    {
    cmd.Parameters.Add(parameter);
    }
    }

    ///添加返回参数ReturnValue
    cmd.Parameters.Add(
    new SqlParameter(RETURNVALUE, SqlDbType.Int,4,ParameterDirection.ReturnValue,
    false,0,0,string.Empty, DataRowVersion.Default,null)); ///返回创建的SqlCommand对象
    return cmd;
    }

      

  3.   

    如题目上的异常我跟进去了,发现有两个问题,
    一个错误是上面的存储过程中代码中:}
    catch(Exception ex) 为过程或函数 Update_Config 指定了过多的参数。
    {一个错误是上面的DAL代码中:
    catch (Exception ex)
    {
    SystemError.CreateErrorLog(ex.Message);
    throw new Exception(ex.Message, ex); [color=#FF0000] 用户代码未处理Exception,未将对象引用设置到对象的实例.[color]
    }
      

  4.   

    我再发一次异常!
    我跟进去了,发现有两个问题,
    一个错误是上面的存储过程中代码中:}
    catch(Exception ex) 为过程或函数 Update_Config 指定了过多的参数。
    {一个错误是上面的DAL代码中:
    catch (Exception ex)
    {
    SystemError.CreateErrorLog(ex.Message);
    throw new Exception(ex.Message, ex); 用户代码未处理Exception,未将对象引用设置到对象的实例.
    }可这样我也看不出原因,请帮忙看看,多谢先!
      

  5.   

    把你的catch代码段注释掉,再设断点。看看错误到底在哪。catch有的时候会隐藏一些错误的,虽然它捕获的异常,但是你就不知道,这个异常是从哪抛出的。
      

  6.   

    针对这个错误“为过程或函数 Update_Config 指定了过多的参数。”你要核对一下数据库中的存储过程,看看参数是否一致。
      

  7.   


    注释掉后就出现(在上面的执行存储过程代码中):
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
    源错误: 
    行 127:
    行 128: ///返回存储过程的参数值
    行 129: return (int)cmd.Parameters[RETURNVALUE].Value;
    行 130: }
    行 131:
     可我还是不知道怎么解决,也不知道问题在哪!同样的存储过程代码我在做Admin页面的时候,增删改查都可以。但做到这个设置网站配置页面执行时出现问题,这个我很困惑!请帮忙找到我哪里搞错了!
      

  8.   

    你的存储过程中肯定有Return语句,是吧?
     return (int)cmd.Parameters[RETURNVALUE].Value;
    这句就是用来承接这个Return语句返回值的。
    那你的参数里面应该定义一个这样的参数才对呀。
    要设置这样的一个属性ParameterDirection.ReturnValue 
      

  9.   

    不要看异常里面,建议你打断点,调试报异常的那句,是哪句?1.sqlHelper.RunProc("Update_Config", ParamList); 
    Update_Config这个存储过程的表结构打过来看下。
    2.……用户代码未处理Exception,未将对象引用设置到对象的实例.
    是不是定义的变量等为空了,没有做空值判断??请检查
      

  10.   


    代码定义有这么一句:private readonly string RETURNVALUE = "RETURNVALUE";
    这个应该没问题吧!这个只是公用的SQLHelper类封装对SQL Server数据库的添加、删除、修改和选择等操作!我想问题应该不是这里,因为同样的存储过程代码我在做Admin页面的时候,增删改查都可以。
      

  11.   

    我见过这种程序。应该就是这个错误,我说的是存储过程里是不是有Return语句。
    建议你看下http://hi.baidu.com/redxman/blog/item/ee34ee81bbafcfdcbd3e1e51.html至于那个“未将对象引用设置到对象的实例”的错误,就是因为你没有定义ParameterDirection.ReturnValue
    这种参数。
      

  12.   

    这种错一般是自己不细心错的,应该是没赋值,或是等于null了。要么是哪个地方写错了一个字母,导致值获得地不对。楼主再自己检查吧,不是语法错误,也不是逻辑错误,仅仅是单纯的自己的问题罢了。检查各个实体的值是否赋对了,或者绑定的值是否正确。
      

  13.   

    要么就是你的"Update_Config"存储过程中没有return语句,那就应该对public int RunProc(string procName, SqlParameter[] prams)这个方法做重载,返回值为void,去掉最后一行。