[英]How to check a value in dataset is empty or not?
我的项目中有一个fileupload选项。 它包含一个返回数据集的查询。 工作正常。 但是现在我要检查返回的数据集是否为空或作为参数传递给查询的值是否相同。 这是我的后端代码。
.cs代码
if ((FileUpload1.HasFile))//&& (ext == ".pdf")
{
ds = db.checkExistingPDF(fileName);
if (dbFileName != fileName)
{
this.FileUpload1.SaveAs(Path.Combine(svrPath, fileName + ".pdf"));
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", " alert('Successfully uploaded');", true);
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", " confirm ('Appeal is availbale for the this competition') ; ", true);
}
else
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", " confirm ('Error') ; ", true);
}
这是我的查询
public DataSet checkExistingPDF(string fileName)
{
string strQuery = @"IF EXISTS (SELECT * FROM APPEAL_MASTER WHERE Attachment_upload = '"+ fileName +"')";
return SqlHelper.ExecuteDataset(strConnStringAppeal, CommandType.Text, strQuery);
}
在数据集对象中获取结果,然后验证NULL和表行数 :
Dataset ds=checkExistingPDF("filename");
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
//record exist with same filename
}
else
{
//no any record exist with same filename
}
检查数据集是否为空您必须检查null和表计数。
Dataset ds=checkExistingPDF("filename");
if(ds != null && ds.Tables.count > 0)
{
// your code
}
DataSet dsReturnedObj = SqlHelper.ExecuteDataset(strConnStringAppeal, CommandType.Text, strQuery);
return dsReturnedObj == null ? null : dsRetunredObj
在cs文件后面的代码中:
Dataset dsReturnedObj = db.checkExistingPDF(fileName)
if (dsReturnedObj != null)
{
if (dsReturnedObj.Tables.Count > 0)
{
if (dsReturnedObj.Tables[0].Rows.Count > 0)
{
this.FileUpload1.SaveAs(Path.Combine(svrPath, fileName + ".pdf"));
ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", " alert('Successfully uploaded');", true);
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.