62,046
社区成员
发帖
与我相关
我的任务
分享
<ul id="menu" runat="server">
</ul>
protected string sConnString = "..."; // 连接数据库字符串
protected void Page_Load(object sender, EventArgs e)
{
menu.InnerHtml = CreateMenuHtml();
}
public string CreateMenuHtml()
{
StringBuilder sMenuHtml = new StringBuilder();
if(isPurview("itemA")) { sMenuHtml.append(...) ......}
if(isPurview("itemB")) { sMenuHtml.append(...) ......}
if(isPurview("itemC")) { sMenuHtml.append(...) ......}
if(isPurview("itemD")) { sMenuHtml.append(...) ......}
if(GetOther("@ABlock", "ABlock")==1) { sMenuHtml.append(...) ......} // 添上了这句就出现如题的错误。
return sMenuHtml.ToString();
}
//
public bool isPurview(string item)
{
SqlConnection connection = new SqlConnection(this.sConnString);
SqlCommand command = new SqlCommand("IS_Purview", connection);
command.CommandType = CommandType.StoredProcedure;
SqlParameter username = command.Parameters.Add("@sUserName", SqlDbType.Char, 50);
SqlParameter purview = command.Parameters.Add("@sPurview", SqlDbType.Char, 50);
SqlParameter intReturn = command.Parameters.Add("rv", SqlDbType.Int);
username.Direction = ParameterDirection.Input;
purview.Direction = ParameterDirection.Input;
intReturn.Direction = ParameterDirection.ReturnValue;
username.Value = Session["username"].ToString();
purview.Value = item;
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
SqlDataReader reader = command.ExecuteReader();
int returnValue = int.Parse(command.Parameters["rv"].Value.ToString());
reader.Close();
connection.Close();
if (returnValue == 1)
return true;
else
return false;
}
public int GetOther(string item, string itemvalue)
{
int iOther=0;
SqlConnection connection = new SqlConnection(this.sConnString);
SqlCommand command = new SqlCommand("IS_Other", connection);
command.CommandType = CommandType.StoredProcedure;
SqlParameter OtherList = command.Parameters.Add(item, SqlDbType.Char, 50);
OtherList.Direction = ParameterDirection.Input;
OtherList.Value = itemvalue;
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
SqlDataReader reader = command.ExecuteReader();
reader = command.ExecuteReader();
if (reader.Read())
{
iOther = Convert.ToInt32(reader[0]);
}
reader.Close();
connection.Close();
return iOther;
}
加上try..catch..finally,比如:
public bool isPurview(string item)
{
int returnValue=0;
SqlConnection connection = new SqlConnection(this.sConnString);
SqlDataReader reader =null;
SqlCommand command = new SqlCommand("IS_Purview", connection);
command.CommandType = CommandType.StoredProcedure;
SqlParameter username = command.Parameters.Add("@sUserName", SqlDbType.Char, 50);
SqlParameter purview = command.Parameters.Add("@sPurview", SqlDbType.Char, 50);
SqlParameter intReturn = command.Parameters.Add("rv", SqlDbType.Int);
username.Direction = ParameterDirection.Input;
purview.Direction = ParameterDirection.Input;
intReturn.Direction = ParameterDirection.ReturnValue;
purview.Value = item;
try
{
username.Value = Session["username"].ToString();
if (connection.State == ConnectionState.Closed)
{
connection.Open();
}
reader = command.ExecuteReader();
returnValue = int.Parse(command.Parameters["rv"].Value.ToString());
reader.Close();
connection.Close();
}
catch(Exception ex)
{
throw ex;
}
finally
{
if(reader!=null) reader.Close();
connection.Close();
}
return returnValue == 1;
}
后面的GetOther也做类似的修改