asp.net - 如何处理 'The source contains no DataRows. ' 错误

标签 asp.net

请找到下面的代码。如果行为空,它将通过错误“源不包含数据行”。如何处理这个??

if (dtTemSec.Rows.Count > 0)
                    {
                        grdStepDetails.DataSource = dtTemSec.AsEnumerable()
                                        .Where(x => x.Field<string>("Status") != "D" && x.Field<string>("ID") == "ST" && x.Field<int>("Folder") == folder)
                                        .CopyToDataTable();
                        grdStepDetails.DataBind();
                        ClientScript.RegisterStartupScript(GetType(), "Val", "ShowStepPopup();", true);
                    }
                    else
                    {
                        grdStepDetails.DataSource = null;
                        grdStepDetails.DataBind();
                        ClientScript.RegisterStartupScript(GetType(), "Val", "ShowStepPopup();", true);
                    }

最佳答案

像这样使用:-

 if (dtTemSec.Rows.Count > 0)
                    {
                        var table = dtTemSec;
                        var rows = table.AsEnumerable().Where(x => x.Field<string>("Status") != "D" && x.Field<string>("ID") == "ST" && x.Field<int>("Folder") == folder);
                        var dt = rows.Any() ? rows.CopyToDataTable() : table.Clone();
                        grdStepDetails.DataSource = dt;
                        grdStepDetails.DataBind();
                        ClientScript.RegisterStartupScript(GetType(), "Val", "ShowStepPopup();", true);
                    }
                    else
                    {
                        grdStepDetails.DataSource = null;
                        grdStepDetails.DataBind();
                        ClientScript.RegisterStartupScript(GetType(), "Val", "ShowStepPopup();", true);
                    }

关于asp.net - 如何处理 'The source contains no DataRows. ' 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29366065/

相关文章:

c# - 如何在 asp.net 中将其他网站的 RSS FEED 显示到我的站点?

asp.net - 自定义 UserControl 未在 ASP.NET 中注册

c# - 在每四个 + 号后添加空格

.net - 如何将CMS添加到现有网站

asp.net - HTTP 错误 403 - 禁止

asp.net - Windows 身份验证成功,但 IsAuthenticated == false

asp.net - 更改ASP.NET中错误消息的语言

c# - 我可以使用 Azure api 管理进行 gRPC 通信吗?

asp.net - 网络API : DelegatingHandler and ApiController on the same thread?

asp.net - 我们可以在 ASP.Net WEB API 中使用存储库模式和工作单元吗?