我写了一个新闻读取的类,但是出现标题所出的问题。
这是我的详细代码:
前台:
<table border="1">
<tr><td style="width: 293px" runat="server" id="tt1">&nbsp;</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();
        }
    }
谢谢各位了
    

解决方案 »

  1.   

    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 里面没有执行,又没有出异常,就出问题了!
      

  2.   

    这一句的位置不对!
    return tt.InnerText; 
      

  3.   


    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(); 
            } 
        } 你最后再返回 return "";可以了,
    一般你可以在最外面声明个变量把数据赋给这个变量,最后只返回这个变量就可以了。
      

  4.   

    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();
            }
        } 
      

  5.   

    改: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); 
                               } 
            } 
            catch 
            { 
                //return tt.InnerText; 
            } 
            finally 
            { 
                conn.Close(); 
            } 
     return tt.InnerText;     }
      

  6.   

    你的代码写错了:
       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 (dea.Read()) 
                { 
                    tt.InnerText +=" <a href=\"news_look.aspx?id=" + dea["id"] + "\" target=_blank>"   +access.Substr(dea["title"].ToString(), 0, 11); 
                }
       return tt.InnerText; 
      

  7.   

    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;
                }
    return string.empty;
            }
            catch
            {
                return tt.InnerText;
            }
            finally
            {
                conn.Close();
            }
        } 
      

  8.   


    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造成了这个错误,所以在编译的时候报这个错误。6楼正解。
    哎来晚了!!