C# 未将对象引用设置到对象的实例(看代码)

来源:百度知道 编辑:UC知道 时间:2024/06/23 08:36:40
<%@ Application Language="C#" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script runat="server">

void Application_Start(object sender, EventArgs e)
{
// 在应用程序启动时运行的代码
SqlConnection conn = ConnDB.GetConn();
conn.Open();
SqlCommand cmd = new SqlCommand("select num from adminconfig",conn);
Application["total"] = Convert.ToInt32(cmd.ExecuteScalar());
Application["online"] = 0;
}

void Application_End(object sender, EventArgs e)
{
// 在应用程序关闭时运行的代码
SqlConnection conn = ConnDB.GetConn();
conn.Open();
SqlCommand cmd = new SqlCommand("update adminconfig set num='"+Application["total"]+"'",conn);
cmd.ExecuteNonQuery();
conn.Close();
}

是不是cmd.ExecuteScalar()对象返回为null那?
建议:
Object ob=cmd.ExecuteScalar();
if(ob!=null)//你调试看看。
{
Application["total"] = Convert.ToInt32(ob);
}

//祝你好运

在winform中这样的问题是这样解决的
就是给对象实例化一个实例
也就是new一个实例
total tt = new total()
如果可以这样的话!

try
{Application["total"] = Convert.ToInt32(cmd.ExecuteScalar());
}
catch
{}

adminconfig表有数据吗?