我有一个非常简单的代码来调用存储过程。存储过程用于向用户的过期帐户发送提醒。
当用户输入正确的电子邮件地址时,用户会收到一封提醒电子邮件,上面写着“提醒已成功发送”
这正是我们想要的。
但是,如果用户输入的电子邮件地址无效,则用户仍会看到相同的消息“提醒已成功发送”
这不好。
你能帮我解决我做错了吗?
请参阅下面的完整(实际)代码:
Protected Sub BtnSubmit_Click(ByVal sender As System.Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles BtnSubmit.Click
Dim oConnection As SqlConnection = New SqlConnection(ConfigurationManager.ConnectionStrings("sConnectionString").ConnectionString)
Dim oCommand As SqlCommand = New SqlCommand()
Try
oConnection.Open()
oCommand.Connection = oConnection
oCommand.CommandText = "AcountExpiration"
oCommand.CommandType = CommandType.StoredProcedure
oCommand.Parameters.Add(New SqlParameter("@Email", Data.SqlDbType.VarChar, 50)).Value = Email.Text
Dim adpt As New SqlDataAdapter(oCommand)
Dim ds As New DataSet()
adpt.Fill(ds)
oCommand.ExecuteReader()
lblMsg.Text="Reminder successfully sent"
Catch ex As SqlException
Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('" + ex.Message + "')</SCRIPT>")
Finally
oConnection.Close()
End Try
End Sub
c#解决方案也很受欢迎。
Dim scmd As SqlCommand = New SqlCommand("AcountExpiration", Conn)
scmd.CommandType = CommandType.StoredProcedure
scmd.Parameters.AddWithValue("@Email", Email.Text)
'Dim r As SqlDataReader = scmd.ExecuteReader()
Dim validEmail As Boolean = False
Dim reader As SqlDataReader = scmd.ExecuteReader()
While reader.Read()
'if we are here then something got returned.
'so probably a valid email.
validEmail = True
End While
If validEmail = True Then
lblMsg.Text = "Success"
Else
lblMsg.Text = "email does not exit on our system"
End If
答案 0 :(得分:2)
我看到你有几个不同的选择。
答案 1 :(得分:0)
我不会为此使用SqlDataAdapter或Dataset。只需使用SqlDataReader
即可bool validEmail = false;
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
//if we are here then something got returned.
//so probably a valid email.
validEmail = true;
}
bool validEmail = Convert.ToBoolean(command.ExecuteScalar());
然后
if(validEmail)
{
}
else
{
}
更新
将为此添加更新,因为有些人没有意识到某些代码不会神奇地工作。我已经包含了ExecuteScalar和ExecuteReader的链接,以展示如何使用这些方法。
如果您想使用ExecuteScalar,则必须更改存储过程以返回一个可以解析为布尔值的值。
以上方法仅用于检查数据库中是否存在电子邮件。不,这不会对电子邮件地址进行验证,因为我预计会在达到此代码之前发生这种情况。