Gridview使用ObjectDataSource - 不显示信息

时间:2011-12-08 22:49:56

标签: c# web-services objectdatasource

我尝试通过Web服务绑定gridview但未成功,但没有任何显示。我的网络方法有问题吗?

以下是我在App_Code文件夹中的内容:membershipService.cs,DataAccessService.cs,Post.cs membershipService.cs:

    [WebMethod(Description = "Display all users' post")]
    public Post[] DisplayAllPosts()
    {
        List<Post> usersPosts = new List<Post>();
        DataSet ds;
        Post p;

        try
        {
            ds = DataAccessService.RunSimpleQuery("SELECT username, post, postDateTime FROM UserPosts ORDER BY postID DESC");

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                p = new Post();
                p.username = dr["username"].ToString();
                p.post = dr["post"].ToString();
                p.postDateTime = dr["postDateTime"].ToString();
                usersPosts.Add(p);
            }        
            return usersPosts.ToArray();
        }
        catch (Exception ex)
        {
            return null;
        }
    }

DataAccessService.cs:

public static DataSet RunSimpleQuery(string query)
{
    DataSet result = new DataSet();
    OleDb.OleDbConnection connection = new System.Data.OleDb.OleDbConnection(ConnectionString);
    OleDb.OleDbCommand command;
    OleDb.OleDbDataAdapter dataAdapter;

    try
    {
        //open database connection
        connection.Open();

        //create database command for query
        command = new OleDb.OleDbCommand();
        command.CommandText = query;
        command.Connection = connection;
        command.CommandType = CommandType.Text;

        //create data adapter and use it to fill the data set using the command
        dataAdapter = new OleDb.OleDbDataAdapter();
        dataAdapter.SelectCommand = command;
        dataAdapter.Fill(result);
    }
    catch
    {
        result = null;
    }
    finally
    {
        //close connection
        connection.Close();
        connection.Dispose();
    }
    //return dataset
    return result;
}

Post.cs:

public class Post
{
    public string username;
    public string post;
    public string postDateTime;
}

我会感激我能得到的任何帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

您是否尝试在DisplayAllPosts中的异常处理程序上放置一个断点,以查看是否在那里抛出异常?

除非您的所有数据库列都不可为空,否则您的某个列中的DbNull.Value可能会抛出异常。