并非所有的代码路径都返回值???
我写了一个新闻读取的类,但是出现标题所出的问题。
这是我的详细代码:
前台:
<table border="1">
<tr><td style="width: 293px" runat="server" id="tt1"> </td></tr>
</table>
后台
protected void Page_Load(object sender, EventArgs e)
{
string str = "select top 5 * from news where type_ids=51 order by id desc";
access.yj(str,tt1);
}
调用的类:
public static string yj(string str,HtmlTableCell tt)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["GirlsysConnectionString"].ConnectionString);//连接数据库
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(str, conn);
SqlDataReader dea = cmd.ExecuteReader();
while (dea.Read())
{
tt.InnerText +="<a href=\"news_look.aspx?id=" + dea["id"] + "\" target=_blank>" + access.Substr(dea["title"].ToString(), 0, 11);
return tt.InnerText;
}
}
catch
{
return tt.InnerText;
}
finally
{
conn.Close();
}
}
谢谢各位了
------解决方案--------------------while (dea.Read())
{
tt.InnerText +=" <a href=\"news_look.aspx?id=" + dea["id"] + "\" target=_blank>" + access.Substr(dea["title"].ToString(), 0, 11);
return tt.InnerText;
}
如果while 里面没有执行,又没有出异常,就出问题了!
------解决方案--------------------这一句的位置不对!
return tt.InnerText;
------解决方案--------------------C# code
public static string yj(string str,HtmlTableCell tt)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["GirlsysConnectionString"].ConnectionString);//连接数据库
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(str, conn);
SqlDataReader dea = cmd.ExecuteReader();
while (dea.Read())
{
tt.InnerText +=" <a href=\"news_look.aspx?id=" + dea["id"] + "\" target=_blank>" + access.Substr(dea["title"].ToString(), 0, 11);
return tt.InnerText;
}
}
catch
{
return tt.InnerText;
}
finally
{
conn.Close();
}
}
------解决方案--------------------
public static string yj(string str,HtmlTableCell tt)
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["GirlsysConnectionString"].ConnectionString);// 连接数据库
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(str, conn);
SqlDataReader dea = cmd.ExecuteReader();
while (dea.Read())
{
tt.InnerText +=" <a href=\"news_look.aspx?id=" + dea["id"] + "\" target=_blank>" + access.Substr(dea["title"].ToString(), 0, 11);
}
return tt.InnerText;
}
catch
{
return tt.InnerText;
}