咨询各位高手一个小问题
想采用三层架构思路来构建一个页面,要实现读取记录并显示出来的功能其中:
WEB_MSDI_NEWS 是数据表
包含 Id,Title,Content字段
1.数据访问层
public class NewsEdit
{
SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionsqlserver"].ConnectionString);
public DataSet GetNews(string Id)
{
try
{
DataSet thisDataSet = new DataSet();
thisConnection.Open();
SqlDataAdapter thisAdapter = new SqlDataAdapter("SELECT Title,Content FROM WEB_MSDI_NEWS WHERE (Id = '"+Id+"')", thisConnection);
SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
thisAdapter.Fill(thisDataSet, "WEB_MSDI_NEWS");
thisConnection.Dispose();
return thisDataSet;
}
catch
{
throw;
}
finally
{
thisConnection.Close();
}
}
}
2.业务层
public class NewsEdit
{
IndexDAL.NewsEdit newsEdit = new IndexDAL.NewsEdit();
//string message = string.Empty;
//string Fid1 = string.Empty;
public DataSet GetNews(string Id)
{
DataSet getnews = newsEdit.GetNews(Id);
return getnews;
}
}
3.表示层后台
this.TextBox1.Text = newsEdit.GetNews(id).Tables[0].Rows[0]["Content"].ToString();
但是在运行是总提示这个错误
跟代码也不晓得是什么问题,恳请各位高手解答下 --------------------编程问答-------------------- 断点跟踪,没有查找到数据 --------------------编程问答-------------------- Tables[0]中没有数据 --------------------编程问答-------------------- newsEdit.GetNews(id).Tables[0].Rows[0]["Content"].ToString();
错误太明显了 不想多说
自己跟踪代码 到这行 仔细看看 就知道原因了
--------------------编程问答-------------------- 调试下吧。返回的值为空。 --------------------编程问答-------------------- 是的,已经跟踪了
newsEdit.GetNews(id).Tables[0].Rows[0]["Content"].ToString();
没有数据,应该怎样修改呢? --------------------编程问答-------------------- 判断一下如果为空的话就让他显示无,不为空显示那个值 --------------------编程问答-------------------- newsEdit.GetNews(id).Tables[0].Rows[0]["Content"].ToString();
这句里面的东东都是能点出来的吗?
还是自己强行写出来的?? --------------------编程问答-------------------- public class NewsEdit
{
SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["connectionsqlserver"].ConnectionString);
public DataSet GetNews(string Id)
{
try
{
DataSet thisDataSet = new DataSet();
thisConnection.Open();
SqlDataAdapter thisAdapter = new SqlDataAdapter("SELECT Title,Content FROM WEB_MSDI_NEWS WHERE (Id = '"+Id+"')", thisConnection);
SqlCommandBuilder thisBuilder = new SqlCommandBuilder(thisAdapter);
thisAdapter.Fill(thisDataSet, "WEB_MSDI_NEWS");
thisConnection.Dispose();
//在这里进行判断
if(thisDataSet.Tables[0].Rows.Count>0) return thisDataSet;
}
catch
{
throw;
}
finally
{
thisConnection.Close();
}
}
}
补充:.NET技术 , ASP.NET