如何在asp.net中处理null listview内容

时间:2015-11-11 13:57:11

标签: asp.net

我使用listview显示数据库表内容。但我的问题是当listview为空时抛出异常。我怎么能解决这个问题?

aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            var tbl = GetAlldealdet();
            ListView1.DataSource = tbl;
            ListView1.DataBind();

        }
     }

  public static DataTable GetAlldealdet()
    {
        try
        {
          string connString = "Server=localhost;database=mytable;uid=root;";
          string query = "SELECT * FROM 'mytable'.'dealmaster' ";
          MySqlDataAdapter ma = new MySqlDataAdapter(query, connString);
          DataSet DS = new DataSet();
          ma.Fill(DS);
          return DS.Tables[0];
        }
        catch (MySqlException e)
        {
            throw new Exception(e.Message);
        }
    }

1 个答案:

答案 0 :(得分:0)

你能试试吗

   var tbl = GetAlldealdet();
   if (tbl != null) {
       ListView1.DataSource = tbl;
       ListView1.DataBind();
   }
   else {
       //do the logic if empty
   }