先将代码贴上,诸位多帮忙:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public class Album_SQLHelper
{
private static string connStr =@"Data Source=DBASE;Initial Catalog=JiaoYou_DB;Integrated Security=True";
private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataAdapter adapter = null;
private DataSet dataset = null; public Album_SQLHelper()
{
conn = new SqlConnection(connStr);
cmd = new SqlCommand();
cmd.Connection = conn;
} private void openConn()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
} private void closeConn()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
} // 验证节点是否为目录
public bool checkdir(string strID)
{
bool dt;
string tempCmd = "SELECT IsDir FROM Album WHERE ID=" + strID;
try
{
this.openConn();
cmd.CommandText = tempCmd;
dt = Boolean.Parse(cmd.ExecuteScalar().ToString());
}
catch (Exception ex)
{
throw ex; --------------------这一行有错!!!!!!!!!! }
finally
{
this.closeConn();
}
return dt;
} //提取数据库内所有照片
public DataTable getpicinfo()
{
DataTable dt;
string tempCmd = "SELECT * FROM Album";
try
{
this.openConn();
cmd.CommandText = tempCmd;
adapter = new SqlDataAdapter(cmd);
dataset = new DataSet();
adapter.Fill(dataset);
dt = dataset.Tables[0];
}
catch (Exception ex)
{
throw ex;
}
finally
{
this.closeConn();
}
return dt;
} //验证是照片节点还是目录节点
public bool checkchildnode(string strID)
{
bool dt;
string tempCmd = "SELECT COUNT(*) FROM Album WHERE ParentID=" + strID;
try
{
this.openConn();
cmd.CommandText = tempCmd;
dt = Int32.Parse(cmd.ExecuteScalar().ToString()) > 0 ? true : false;
}
catch (Exception ex)
{
throw ex;
}
finally
{
this.closeConn();
}
return dt;
} //删除节点
public void deleteNode(string strID)
{
string tempCmd = "DELETE Album WHERE ImgID=" + strID;
try
{
this.openConn();
cmd.CommandText = tempCmd;
cmd.ExecuteNonQuery(); }
catch (Exception ex)
{
throw ex;
}
finally
{
this.closeConn();
}
} //添加节点
public void addNode(string name, string url, string isDir, string ParentID)
{
string tempCmd = "INSERT INTO Album (ImgName,ImgUrl,IsDir,ParentID,CreateDate)VALUES" + "('" + name + "','" + url + "','" + isDir + "'," + ParentID + ",GetDate())";
try
{
this.openConn();
cmd.CommandText = tempCmd;
cmd.ExecuteNonQuery(); }
catch (Exception ex)
{
throw ex; }
finally
{
this.closeConn();
}
}
//更新节点
public void updateNode(string strID, string name)
{
string tempCmd = "UPDATE Album SET ImgName='" + name + "'WHERE ID=" + strID;
try
{
this.openConn();
cmd.CommandText = tempCmd;
cmd.ExecuteNonQuery(); }
catch (Exception ex)
{
throw ex;
}
finally
{
this.closeConn();
}
}}
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
Album_SQLHelper.checkdir(String strID) in e:\Web Service文件夹--Netbox\交友网站\App_Code\Album_SQLHelper.cs:60
Album.btnAddPicture_Click(Object sender, EventArgs e) in e:\Web Service文件夹--Netbox\交友网站\AlbumList.aspx.cs:95
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +98
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4921
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public class Album_SQLHelper
{
private static string connStr =@"Data Source=DBASE;Initial Catalog=JiaoYou_DB;Integrated Security=True";
private SqlConnection conn = null;
private SqlCommand cmd = null;
private SqlDataAdapter adapter = null;
private DataSet dataset = null; public Album_SQLHelper()
{
conn = new SqlConnection(connStr);
cmd = new SqlCommand();
cmd.Connection = conn;
} private void openConn()
{
if (conn.State == ConnectionState.Closed)
{
conn.Open();
}
} private void closeConn()
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
} // 验证节点是否为目录
public bool checkdir(string strID)
{
bool dt;
string tempCmd = "SELECT IsDir FROM Album WHERE ID=" + strID;
try
{
this.openConn();
cmd.CommandText = tempCmd;
dt = Boolean.Parse(cmd.ExecuteScalar().ToString());
}
catch (Exception ex)
{
throw ex; --------------------这一行有错!!!!!!!!!! }
finally
{
this.closeConn();
}
return dt;
} //提取数据库内所有照片
public DataTable getpicinfo()
{
DataTable dt;
string tempCmd = "SELECT * FROM Album";
try
{
this.openConn();
cmd.CommandText = tempCmd;
adapter = new SqlDataAdapter(cmd);
dataset = new DataSet();
adapter.Fill(dataset);
dt = dataset.Tables[0];
}
catch (Exception ex)
{
throw ex;
}
finally
{
this.closeConn();
}
return dt;
} //验证是照片节点还是目录节点
public bool checkchildnode(string strID)
{
bool dt;
string tempCmd = "SELECT COUNT(*) FROM Album WHERE ParentID=" + strID;
try
{
this.openConn();
cmd.CommandText = tempCmd;
dt = Int32.Parse(cmd.ExecuteScalar().ToString()) > 0 ? true : false;
}
catch (Exception ex)
{
throw ex;
}
finally
{
this.closeConn();
}
return dt;
} //删除节点
public void deleteNode(string strID)
{
string tempCmd = "DELETE Album WHERE ImgID=" + strID;
try
{
this.openConn();
cmd.CommandText = tempCmd;
cmd.ExecuteNonQuery(); }
catch (Exception ex)
{
throw ex;
}
finally
{
this.closeConn();
}
} //添加节点
public void addNode(string name, string url, string isDir, string ParentID)
{
string tempCmd = "INSERT INTO Album (ImgName,ImgUrl,IsDir,ParentID,CreateDate)VALUES" + "('" + name + "','" + url + "','" + isDir + "'," + ParentID + ",GetDate())";
try
{
this.openConn();
cmd.CommandText = tempCmd;
cmd.ExecuteNonQuery(); }
catch (Exception ex)
{
throw ex; }
finally
{
this.closeConn();
}
}
//更新节点
public void updateNode(string strID, string name)
{
string tempCmd = "UPDATE Album SET ImgName='" + name + "'WHERE ID=" + strID;
try
{
this.openConn();
cmd.CommandText = tempCmd;
cmd.ExecuteNonQuery(); }
catch (Exception ex)
{
throw ex;
}
finally
{
this.closeConn();
}
}}
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。堆栈跟踪:
[NullReferenceException: 未将对象引用设置到对象的实例。]
Album_SQLHelper.checkdir(String strID) in e:\Web Service文件夹--Netbox\交友网站\App_Code\Album_SQLHelper.cs:60
Album.btnAddPicture_Click(Object sender, EventArgs e) in e:\Web Service文件夹--Netbox\交友网站\AlbumList.aspx.cs:95
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +75
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +98
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +7
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +11
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +33
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +4921
如果结果集为空,则为null
所以在dt = Boolean.Parse(cmd.ExecuteScalar().ToString());
要先判断cmd.ExecuteScalar()是否为null
这样可以吗?
可以这样改试试:
// 验证节点是否为目录
public bool checkdir(string strID)
{
bool dt=true;
string tempCmd = "SELECT IsDir FROM Album WHERE ID=" + strID;
try
{
this.openConn();
cmd.CommandText = tempCmd;
dt = Boolean.Parse(cmd.ExecuteScalar().ToString());
}
catch (System.NullReferenceException NrefEx)
{
dt=false;
}
catch (Exception ex)
{
dt=false;
throw ex; --------------------这一行有错!!!!!!!!!! }
finally
{
this.closeConn();
}
return dt;
}