【问题标题】:How to get the Dataset if there are null values in some of the fields如果某些字段中有空值,如何获取数据集
【发布时间】:2014-06-06 07:05:30
【问题描述】:
DataSet ds = ast.GetUserLoginInfo(Param);

foreach (DataRow dr in ds.Tables[0].Rows)
{
    if (ds.Tables.Count > 0)
    {
        if (ds.Tables[0].Rows.Count > 0)
        {
            _userstate.ID = Convert.ToInt32(ds.Tables[0].Rows[0]["ID"]);
            _userstate.Name = Convert.ToString(ds.Tables[0].Rows[0]["Name"]).Trim();
            _userstate.Email = Convert.ToString(ds.Tables[0].Rows[0]["Email"]).Trim();
            _userstate.Username = Convert.ToString(ds.Tables[0].Rows[0]["Username"]).Trim();              
            _userstate.GroupId = Convert.ToInt32(ds.Tables[0].Rows[0]["GroupId"]);
            _userstate.BranchId = Convert.ToInt32(ds.Tables[0].Rows[0]["BranchId"]);
            _userstate.BranchId = Convert.ToInt32(ds.Tables[0].Rows[0]["DeptId"]);
            _userstate.RoleId = Convert.ToInt32(ds.Tables[0].Rows[0]["RoleId"]);
            _userstate.IsActive = Convert.ToBoolean(ds.Tables[0].Rows[0]["IsActive"]);

            _siteuser.UserStat = _userstate;
        }

对于某些用户,BranchId 为空,因为它们是头部,所有分支都在它们之下。因此,当用户登录时,数据库在数据集中返回非任何内容。

【问题讨论】:

  • 所以?有什么问题?
  • 您将DeptId 分配给BranchId 我很确定您不是故意的。
  • 标题没有意义。 如果某些字段中有空值,如何获取数据集是什么意思?获取数据集?你已经有数据集了!
  • 错误?错误是什么?发布它。
  • 引用变量 ds 没有指向任何东西。

标签: c# .net dataset


【解决方案1】:

将此代码用于所有字段,

 _userstate.ID = Convert.ToInt32(ds.Tables[0].Rows[0]["ID"] == null ? 0 : ds.Tables[0].Rows[0]["ID"]);
 _userstate.Name = Convert.ToString(ds.Tables[0].Rows[0]["Name"] == null ? "" ds.Tables[0].Rows[0]["Name"] ).Trim();

【讨论】:

    【解决方案2】:

    由于 branchId 可以为空,ConvertToInt32 会抛出异常。试试这个。

    int.TryParse(ds.Tables[0].Rows[0]["BranchId",  _userstate.BranchId)
    

    【讨论】:

    • 它抛出编译时错误“TryParse 方法没有过载需要一个参数”
    • @Smokingmonkey :你是对的,我纠正了它,它的第二个参数设置了结果。它应该工作。试试看
    猜你喜欢
    • 2018-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-02-01
    • 1970-01-01
    相关资源
    最近更新 更多