检查在文本框中输入的文本是否存在于数据库中 [英] check the text entered in textbox is present in database or not
问题描述
我试图找出在文本框中输入的txt存在于数据库r中而不是3层中
我尝试了以下代码:
i am trying to find out the txt enterd in the textbox is present in database r not in 3-tier
i have tried the following code:
public int checkmailid(string EmailId)
{
_parameterSet = SqlParameterCache.GetSpParameterSet(_connectionString, "[checkemailid]");
_parameterSet[0].Value = EmailId;
int i = SqlHelper.ExecuteNonQuery(_connectionString, CommandType.StoredProcedure, "[checkemailid]", _parameterSet);
return i;
}
protected void txtEmailId_TextChanged(object sender, EventArgs e)
{
string EmailId=txtEmailId.Text;
int i=_objReg.checkmailid(EmailId);
// _objUdetails.EmailId = txtEmailId.Text;
if (i==1)
{
lblmsg1.Text = "Mail Id already exists";
}
else
{
lblmsg1.Text = "Mail available";
}
}
但是问题在于,如果不存在mailid,则应返回"-1"
如果存在mailid,则应返回"1或0"
但总是返回"-1"
实际上,如果我们使用数据集,则应该返回"1"(如果可用)
和"0"(如果不可用)
任何人都可以在这方面帮助我吗?
我使用了以下Sp
but the problem is that if the mailid is not present it should return "-1"
if the mailid is present it should return "1 or 0"
but always it returning "-1"
actually if we use dataset it should return "1" if available
and "0" if not available
can any one help me out in this aspect
i used the following Sp
ALTER PROCEDURE [dbo].[checkemailid]
@EmailId varchar(50)
AS
BEGIN
if EXISTS (select * from tblTUserDetails where EmailId=@EmailId)
begin
select '1'
end
else
begin
select '0'
end
END
GO
推荐答案
在您的SP上尝试以下操作:
Try this on your SP:
ALTER PROCEDURE [dbo].[checkemailid]
@EmailId varchar(50)
AS
BEGIN
DECLARE counter INT;
select counter=count(EmailId) from tblTUserDetails where EmailId=@EmailId;
RETURN counter;
END
GO
ExecuteNonQuery 返回受影响的行数, ExecuteScalar 返回您需要的(数据库的结果).您的 SqlHelper.ExecuteNonQuery ()方法从不修改记录,因此始终返回-1.
现在在您的C#代码中:
ExecuteNonQuery returns the number of affected rows, ExecuteScalar returns that you need (a result from database). Your SqlHelper.ExecuteNonQuery() method never modifies a record, so always returns -1.
Now in your c# code:
public int checkmailid(string EmailId)
{
_parameterSet = SqlParameterCache.GetSpParameterSet(_connectionString, "[checkemailid]");
_parameterSet[0].Value = EmailId;
int i = Convert.ToInt32(SqlHelper.ExecuteScalar(_connectionString, CommandType.StoredProcedure, "[checkemailid]", _parameterSet).ToString());
return i;
}
希望对您有所帮助.
Hope it helps.
这篇关于检查在文本框中输入的文本是否存在于数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!