wodeqq584958184
wodeqq584958184
2018-05-15 01:30
采纳率: 77.8%
浏览 688

关于VS2010中asp.net后台代码中一个报错,不知道什么意思

报的错是这个:
图片说明
图片说明

然后我的后台代码是如下,起初没有任何错误提示,但是按下F11调试后就出现上面的错误了

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Web.UI.DataVisualization.Charting;

namespace zhu
{
public partial class WebForm1 : System.Web.UI.Page
{
private SqlConnection SqlConnString;

    string[] Count1 = new string[9];
    double[] Count2 = new double[9];
    //string[] Count3 = new string[11];
    protected void Page_Load(object sender, EventArgs e)
    {

        DataSet ds = new DataSet();
        string vOutParam = "";

        ds = ExeProcDT3("Searchtable5", "", ref vOutParam);
        if (ds.Tables.Count >0)
        {
            DataTable dt = new DataTable();
            dt = ds.Tables[0];
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < 9; i++)
                {

                    Count2[i] = Convert.ToDouble(dt.Rows[i]["AM"]);
                    Count1[i] = dt.Rows[i]["c"].ToString() + dt.Rows[i]["a"].ToString();

                }
            }
        }
        zhuzhaugntu(Count1, Count2);



    }

    //柱状图图形的设置
    private void zhuzhaugntu(string[] a, double[] b)
    {
        Chart1.ChartAreas[0].BackColor = Color.Black;//设置背景为黑色
        Chart1.ChartAreas[0].Area3DStyle.PointGapDepth = 10;//x轴行间距
        Chart1.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;//需线
        Chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Yellow;//需线颜色
        Chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;//关掉竖线1
        //根据水尺最高值设置Y轴间距

        Chart1.ChartAreas[0].AxisY.Maximum = 5000;
        Chart1.Series[0].Label = "#VAL";//设置标签文本 (在设计期通过属性窗口编辑更直观)
        Chart1.Series[0].IsValueShownAsLabel = true;//显示标签
        Chart1.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.Blue;
        Chart1.ChartAreas[0].AxisX.LabelAutoFitMaxFontSize = 12;
        Chart1.ChartAreas[0].AxisY.LabelAutoFitMaxFontSize = 15;
        Chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.Red;

        Chart1.Series[0].Points.DataBindXY(a, b);//初始化数据


        /*Chart1.Series[1].Label = "#VAL";
        Chart1.Series[1].IsValueShownAsLabel = true;
        Chart1.ChartAreas[0].AxisX2.LabelStyle.ForeColor = Color.Blue;
        Chart1.ChartAreas[0].AxisX2.LabelAutoFitMaxFontSize = 12;
        Chart1.Series[0].Points.DataBindXY(c);*/
    }
    //连接数据库
    private SqlConnection GetConnect()
    {
        string strSqlSqlConn = "Data Source=.;Initial Catalog=plc;" +
            "Persist Security Info=True;User ID=Jerome;Password=123456";
        SqlConnection sqlConnString = new SqlConnection(strSqlSqlConn);
        return sqlConnString;
    }
    /// <summary>
    /// 执行单入存过带多个表
    /// </summary>
    /// <param name="procName">存储过程名称</param>
    /// <param name="vInParam">输入参数</param>
    /// <param name="vOutParam">输出参数</param>
    /// <returns></returns>
    public DataSet ExeProcDT3(string procName, string vInParam, ref string vOutParam)
    {
        try
        {
            SqlConnString = GetConnect();
            SqlCommand SqlCmd = new SqlCommand(procName, SqlConnString);
            SqlCmd.CommandType = CommandType.StoredProcedure;
            SqlCmd.Parameters.Add("@inparam", SqlDbType.VarChar, 1000).Value = vInParam;
            SqlCmd.Parameters.Add("@outparam", SqlDbType.VarChar, 1024).Direction = ParameterDirection.Output;
            SqlDataAdapter SqlDa = new SqlDataAdapter(SqlCmd);

            DataSet DS = new DataSet();
            SqlDa.Fill(DS);
            vOutParam = SqlCmd.Parameters["@outparam"].Value.ToString();
            return DS;
        }
        catch (Exception)
        {
            return null;
        }
        finally
        {
            SqlConnString.Close();
        }
    }
}

}

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

10条回答 默认 最新

  • qq_23126581
    已采纳

    首先if (ds.Tables.Count >0)报未将对象引用......说明你ds根本没有获取到,和楼上说的一样判断改为if(ds.Tables != null &&ds.Tables.Count >0)
    还有就是我并没有看到你有Open数据库。

    SqlConnection sqlConnString = new SqlConnection(strSqlSqlConn);
    sqlConnString.Open();

    点赞 评论
  • wodeqq584958184
    wodeqq584958184 2018-05-15 01:30

    本人在线等 很急~!!!!!

    点赞 评论
  • zheshiweihe
    zheshiweihe 2018-05-15 01:40

    有可能是数据库没有正确关闭导致的,调试过程中开启后,直接关闭了调试
    下次运行时ExeProcDT3返回null,然后直接引用会出错
    1.全部关闭,clean后重新编译运行
    2.if (ds.Tables.Count >0)
    改为
    if(ds!=null && ds.Tables != null &ds.Tables.Count >0)试一下,这样可能会有用

    点赞 评论
  • scguest
    scguest 2018-05-15 01:58

    ExeProcDT3函数返回空啊,要进这个函数里去调试,查找返回空的具体原因。

    点赞 评论
  • showbo
     执行到catch (Exception)
            {
                return null;
            }这块了
    
                    去掉try..catch看具体报什么错误
    
    点赞 评论
  • HANYONGHUI
    HANYONGHUI 2018-05-15 02:56

    dataset是个空对象,检查在if之前,dataset中是否存在table

    点赞 评论
  • THN1021
    THN1021 2018-05-15 03:03

    去掉try..catch,然后一步步检查。

    点赞 评论
  • showbo

    改这样,看具体错误是什么,肯定是有问题了存储过程

     public DataSet ExeProcDT3(string procName, string vInParam, ref string vOutParam)
        {
                SqlConnString = GetConnect();
                SqlCommand SqlCmd = new SqlCommand(procName, SqlConnString);
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlCmd.Parameters.Add("@inparam", SqlDbType.VarChar, 1000).Value = vInParam;
                SqlCmd.Parameters.Add("@outparam", SqlDbType.VarChar, 1024).Direction = ParameterDirection.Output;
                SqlDataAdapter SqlDa = new SqlDataAdapter(SqlCmd);
    
                DataSet DS = new DataSet();
                SqlDa.Fill(DS);
                vOutParam = SqlCmd.Parameters["@outparam"].Value.ToString();
                return DS;
        }
    
    点赞 评论
  • wodeqq584958184
    wodeqq584958184 2018-05-15 05:46

    目前代码是这样,没有报错,但按下F11调试时候,在SqlConnString .Close()也提示同样的异常信息,提示用户代码未处理NullReferenceException

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.SqlClient;
    using System.Configuration;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Web.UI.DataVisualization.Charting;

    namespace zhu
    {
    public partial class WebForm1 : System.Web.UI.Page
    {
    private SqlConnection SqlConnString;

        string[] Count1 = new string[9];
        double[] Count2 = new double[9];
        //string[] Count3 = new string[11];
        protected void Page_Load(object sender, EventArgs e)
        {
    
            DataSet ds = new DataSet();
            string vOutParam = "";
    
            ds = ExeProcDT3("Searchtable5", "", ref vOutParam);
            if (ds.Tables.Count >0)
            {
                DataTable dt = new DataTable();
                dt = ds.Tables[0];
                if (dt.Rows.Count > 0)
                {
                    for (int i = 0; i < 9; i++)
                    {
    
                        Count2[i] = Convert.ToDouble(dt.Rows[i]["AM"]);
                        Count1[i] = dt.Rows[i]["c"].ToString() + dt.Rows[i]["a"].ToString();
    
                    }
                }
            }
            zhuzhaugntu(Count1, Count2);
    
    
    
        }
    
        //柱状图图形的设置
        private void zhuzhaugntu(string[] a, double[] b)
        {
            Chart1.ChartAreas[0].BackColor = Color.Black;//设置背景为黑色
            Chart1.ChartAreas[0].Area3DStyle.PointGapDepth = 10;//x轴行间距
            Chart1.ChartAreas[0].AxisY.MajorGrid.LineDashStyle = ChartDashStyle.Dash;//需线
            Chart1.ChartAreas[0].AxisY.MajorGrid.LineColor = Color.Yellow;//需线颜色
            Chart1.ChartAreas[0].AxisX.MajorGrid.Enabled = false;//关掉竖线1
            //根据水尺最高值设置Y轴间距
    
            Chart1.ChartAreas[0].AxisY.Maximum = 5000;
            Chart1.Series[0].Label = "#VAL";//设置标签文本 (在设计期通过属性窗口编辑更直观)
            Chart1.Series[0].IsValueShownAsLabel = true;//显示标签
            Chart1.ChartAreas[0].AxisX.LabelStyle.ForeColor = Color.Blue;
            Chart1.ChartAreas[0].AxisX.LabelAutoFitMaxFontSize = 12;
            Chart1.ChartAreas[0].AxisY.LabelAutoFitMaxFontSize = 15;
            Chart1.ChartAreas[0].AxisY.LabelStyle.ForeColor = Color.Red;
    
            Chart1.Series[0].Points.DataBindXY(a, b);//初始化数据
    
    
            /*Chart1.Series[1].Label = "#VAL";
            Chart1.Series[1].IsValueShownAsLabel = true;
            Chart1.ChartAreas[0].AxisX2.LabelStyle.ForeColor = Color.Blue;
            Chart1.ChartAreas[0].AxisX2.LabelAutoFitMaxFontSize = 12;
            Chart1.Series[0].Points.DataBindXY(c);*/
        }
        //连接数据库
        private SqlConnection GetConnect()
        {
            string strSqlSqlConn = "Data Source=.;Initial Catalog=plc;" +
                "Persist Security Info=True;User ID=Jerome;Password=123456";
            SqlConnection sqlConnString = new SqlConnection(strSqlSqlConn);
        SqlConnString.Close();
        SqlConnString.Open();
            return sqlConnString;
        }
        /// <summary>
        /// 执行单入存过带多个表
        /// </summary>
        /// <param name="procName">存储过程名称</param>
        /// <param name="vInParam">输入参数</param>
        /// <param name="vOutParam">输出参数</param>
        /// <returns></returns>
        public DataSet ExeProcDT3(string procName, string vInParam, ref string vOutParam)
        {
    
                SqlConnString = GetConnect();
                SqlCommand SqlCmd = new SqlCommand(procName, SqlConnString);
                SqlCmd.CommandType = CommandType.StoredProcedure;
                SqlCmd.Parameters.Add("@inparam", SqlDbType.VarChar, 1000).Value = vInParam;
                SqlCmd.Parameters.Add("@outparam", SqlDbType.VarChar, 1024).Direction = ParameterDirection.Output;
                SqlDataAdapter SqlDa = new SqlDataAdapter(SqlCmd);
    
                DataSet DS = new DataSet();
                SqlDa.Fill(DS);
                vOutParam = SqlCmd.Parameters["@outparam"].Value.ToString();
                return DS;
        }
    }
    

    }

    点赞 评论
  • wodeqq584958184
    wodeqq584958184 2018-05-15 05:58

    问题已解决了 谢谢大家的帮忙 非常感谢~!!

    点赞 评论

相关推荐