[英]Can't handle if reader.ReadDataTable() doesn't return any DATA from stored procedure in database
I have created an API called GetCustomerPrivacyStatus
, it is trying to find out the TandCstatus.我创建了一个名为GetCustomerPrivacyStatus
的 API ,它试图找出 TandCstatus。
[HttpGet]
[Route("GetCustomerPrivacyStatus")]
public async Task<EULAInfo> GetCustomerPrivacyStatus()
{
var result = new EULAInfo();
string userObjectId = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value;
var orgId = await GetOrgIdOfUser().ConfigureAwait(false);
var TandCvalue = MasterDatabase.GetTermsAndConditionstatus(orgId).ToString();
var TandCInt = TandCvalue != null ? TandCvalue : "0";
var TandCstatus = Int16.Parse(TandCInt) == 0 ? false : true;
result.status = TandCstatus;
return result;
}
In the GetTermsAndConditionstatus
it is calling a stored procedure, if the orgId
is new that means it is not present is the database, that's why dataTable.Rows[0]["TermsAndConditionAccepted"]
is throwing an error在GetTermsAndConditionstatus
它正在调用存储过程,如果orgId
是新的,这意味着它不存在是数据库,这就是dataTable.Rows[0]["TermsAndConditionAccepted"]
抛出错误的原因
There is no row at position 0 position 0 处没有行
I am not able to able to think how to resolve this situation.我无法思考如何解决这种情况。
public object GetTermsAndConditionstatus(string orgId)
{
using (var reader = new StoredProcedureReader(this, "GetTermsAndConditionstatus", new { orgId }))
{
var dataTable = reader.ReadDataTable();
var result = dataTable.Rows[0]["TermsAndConditionAccepted"] ;
return result;
}
}
Stored procedure is:存储过程是:
CREATE PROCEDURE [dbo].[GetTermsAndConditionstatus]
@orgId VARCHAR(100)
AS
SELECT TermsAndConditionAccepted
FROM TermsAndConditionCheck
WHERE OrgId = @orgId
Is there is any way if there is no rows the result variable should be set to null?如果没有行,有什么办法可以将结果变量设置为 null?
object result = null;
using (var reader = new StoredProcedureReader(this, "GetTermsAndConditionstatus", new { orgId }))
{
var dataTable = reader.ReadDataTable();
if (dataTable.Rows.Count > 0)
result = dataTable.Rows[0]["TermsAndConditionAccepted"];
}
return result;
That should fix your problem那应该可以解决您的问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.