0

谢谢,我需要一些帮助,并且DBOperation dbo = new DBOperation();他们说找不到类型或命名空间的错误。

 public partial class Survey : System.Web.UI.Page
    {

     public DataTable fillmydropdownlist()
  {
     DataTable drpdt = new DataTable();
     SqlConnection con= new SqlConnection();
     try
     {

     con.ConnectionString = @"SurveyFdBk_DB";
     con.Open();
     string q = "SELECT * FROM [Survey]";
     SqlCommand cmd = new SqlCommand(q,con);
     SqlDataAdapter da2 = new SqlDataAdapter(cmd);
     return drpdt;
     }
     catch { }
     finally{ con.Close(); }
  }
  protected void Page_Load(object sender, EventArgs e)
  {
    DBOperation dbo = new DBOperation();
    DataTable dt = new DataTable();
    dt = dbo.fillmydropdownlist();
    DataTable drpdt= new DataTable();
    if (dt.Rows.Count > 0)
    {
        DropDownList1.DataSource = drpdt;
        DropDownList1.DataTextField="SurveyName";
        DropDownList1.DataValueField="SurveyID";
        DropDownList1.DataBind();
    }
  }


}
4

1 回答 1

0

所有的执行路径都必须返回一些东西。在您上面的方法中,有 2 条路径:

public DataTable fillmydropdownlist()
{
   try
   {
       //path 1
       return drpdt;
   }
   catch 
   {
       //path 2
       return null; //need return value here
   }
}

如果抛出异常,您可能需要返回某种值null

话虽这么说,在没有记录或处理的情况下捕获所有错误并不是可取的做法。你应该考虑添加一些错误处理,你也应该DataAdapter在你的 finally 块中处理你的。

于 2013-10-23T02:44:25.493 回答