我不确定我做错了什么。 [英] Im Not Sure What I Am Doing Wrong.
问题描述
我正在尝试创建一个登录,客户端已经有一个已经存在客户等的现有数据库所以asp:登录,据我所知,不是一个选项。这就是我到目前为止:
标记:
< asp:TextBox ID = txtUserName TextMode = 电子邮件 CssClass = registerform runat = server > < / asp:TextBox > < asp:Label ID = lblUserName runat = server ForeColor = Navy Text = 请输入您的电子邮件地址 > < / asp:标签 > < br />
< asp:TextBox ID = txtPassword TextMode = 密码 CssClass = registerform runat = server > < / asp:TextBox > < asp:标签ID = Label2 runat = server ForeColor = Navy Text = 请输入您的密码 > < / asp:L abel > < br />
< asp:按钮ID = cmd_LogIn runat = server Text = 登录 OnClick = cmd_LogIn_Click />
< asp:Label ID = lblMessage runat = server Text = > < / asp:标签 >
代码落后:
使用系统;
使用 System.Data;
使用 System.Configuration;
使用 System.Collections;
使用 System.Web;
使用 System.Web.Security;
使用 System.Web.UI;
使用 System.Web.UI.WebControls;
使用 System.Web.UI.WebControls.WebParts;
使用 System.Web.UI.HtmlControls;
使用 System.Web.Configuration;
使用 System.Data.SqlClient;
使用 System.Text;
public partial class 登录:System.Web.UI.Page
{
protected void Page_Load( object sender,EventArgs e)
{
}
public int Validate_Login( String UserName,字符串密码)
{
string connectionString =
WebConfigurationManager.ConnectionStrings [ BillyNicClothingCon]。ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = EXECUTE dbo.bnc_CheckUser;
cmd.Parameters.Add( @ UserName,SqlDbType.VarChar ).Value = UserName;
cmd.Parameters.Add( @ Password,SqlDbType.VarChar ).Value =密码;
cmd.Parameters.Add( @ OutRes,SqlDbType.Int );
cmd.Parameters [ @ OutRes]。Direction = ParameterDirection。输出;
cmd.Connection = con;
int 结果= 0 ;
尝试
{
con.Open();
cmd.ExecuteNonQuery();
结果=( int )cmd.Parameters [ @ OUTRES跨度>]值。
}
catch (SqlException ex)
{
lblMessage .Text = ex.Message;
}
最后
{
cmd.Dispose() ;
if (con!= null )
{
con.Close();
}
}
return 结果;
}
受保护 void cmd_LogIn_Click( object sender,EventArgs e)
{
int 结果= 0 ;
if (txtUserName.Text!= string .Empty&& txtPassword.Text!= string .Empty)
{
Results = Validate_Login(txtUserName.Text.Trim(),txtPassword。 Text.Trim());
if (结果== 1 )
{
lblMessage.Text = 登录好,将用户发送到另一个页面或启用控件< /跨度>;
}
else
{
lblMessage.Text = < span class =code-string> 无效登录;
lblMessage.ForeColor = System.Drawing.Color.Red;
}
}
其他
{
lblMessage.Text = 请确保用户名和密码是正确的;
}
}
}
存储过程:
USE [BillyNicClothing]
GO
/ * *****对象:StoredProcedure [dbo]。[bnc_CheckUser]脚本日期:10/10/2014 11:25:42 ****** /
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
- ================== ===========================
- 作者:Michael Griffiths
- 创建日期:07/10/2014
- 描述:检查用户是否存在
- =========================== ==================
创建 PROCEDURE [dbo]。[bnc_CheckUser]
- 添加存储过程的参数这里
@ UserName nvarchar ( 254 ),
@ Password varchar ( 20 ),
@ OutRes int OUTP UT
AS
set @ OutRes =( SELECT count(*) FROM CustomerDetails.MembershipDetails
WHERE [UserName] = @ UserName < span class =code-keyword>和 [密码] = @密码)
if ( @ OutRes > 1 )
开始
设置 @ OutRes = 1 - 登录正确
结束
else
begin
set @ OutRes = 0 - 登录不良
end
GO
当我测试页面并使用正确的详细信息登录时,它将不允许我登录,但我迷失了原因。
感谢您提前获得的任何帮助:)
存储过程中的问题..
错误1:
if(@OutRes> 1)
正确1:
if(@OutRes> = 1 )
您将得到1个计算结果且不大于1.
解决SP:
CREATE PROCEDURE [dbo]。[bnc_CheckUser]
- 在这里添加存储过程的参数
@UserName nvarchar(254),
@Password varchar(20),
@OutRes int OUTPUT
AS
set @OutRes =(SELECT count(*)FROM CustomerDetails.MembershipDetails
WHERE [UserName] = @UserName和[Password] = @Password)
if(@OutRes> = 1)
begin
set @OutRes = 1 --Login is Correct
end
else
begin
set @OutRes = 0 --Bad login
end
GO
更改
if(@OutRes> 1)if if(@OutRes> = 1)在StoredProcedure
I am trying to create a login, the client has an existing database with customers etc already on it so asp:Login, as far as i can tell, is not an option. This is what i have so far:
markup:
<asp:TextBox ID="txtUserName" TextMode="Email" CssClass="registerform" runat="server"></asp:TextBox><asp:Label ID="lblUserName" runat="server" ForeColor="Navy" Text="Please Enter Your Email Address"></asp:Label><br />
<asp:TextBox ID="txtPassword" TextMode="Password" CssClass="registerform" runat="server"></asp:TextBox><asp:Label ID="Label2" runat="server" ForeColor="Navy" Text="Please Enter Your Password"></asp:Label><br />
<asp:Button ID="cmd_LogIn" runat="server" Text="Log In" OnClick="cmd_LogIn_Click" />
<asp:Label ID="lblMessage" runat="server" Text=""></asp:Label>
Code behind:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
using System.Data.SqlClient;
using System.Text;
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public int Validate_Login(String UserName, String Password)
{
string connectionString =
WebConfigurationManager.ConnectionStrings["BillyNicClothingCon"].ConnectionString;
SqlConnection con = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "EXECUTE dbo.bnc_CheckUser";
cmd.Parameters.Add("@UserName", SqlDbType.VarChar).Value = UserName;
cmd.Parameters.Add("@Password", SqlDbType.VarChar).Value = Password;
cmd.Parameters.Add("@OutRes", SqlDbType.Int);
cmd.Parameters["@OutRes"].Direction = ParameterDirection.Output;
cmd.Connection = con;
int Results = 0;
try
{
con.Open();
cmd.ExecuteNonQuery();
Results = (int)cmd.Parameters["@OutRes"].Value;
}
catch (SqlException ex)
{
lblMessage.Text = ex.Message;
}
finally
{
cmd.Dispose();
if (con != null)
{
con.Close();
}
}
return Results;
}
protected void cmd_LogIn_Click(object sender, EventArgs e)
{
int Results = 0;
if (txtUserName.Text != string.Empty && txtPassword.Text != string.Empty)
{
Results = Validate_Login(txtUserName.Text.Trim(), txtPassword.Text.Trim());
if (Results == 1)
{
lblMessage.Text = "Login is Good, Send the User to another page or enable controls";
}
else
{
lblMessage.Text = "Invalid Login";
lblMessage.ForeColor = System.Drawing.Color.Red;
}
}
else
{
lblMessage.Text = "Please make sure that the username and the password is Correct";
}
}
}
the stored procedure:
USE [BillyNicClothing]
GO
/****** Object: StoredProcedure [dbo].[bnc_CheckUser] Script Date: 10/10/2014 11:25:42 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Michael Griffiths
-- Create date: 07/10/2014
-- Description: check if user exists
-- =============================================
CREATE PROCEDURE [dbo].[bnc_CheckUser]
-- Add the parameters for the stored procedure here
@UserName nvarchar(254),
@Password varchar(20),
@OutRes int OUTPUT
AS
set @OutRes = (SELECT count(*) FROM CustomerDetails.MembershipDetails
WHERE [UserName] = @UserName And [Password] = @Password)
if(@OutRes > 1)
begin
set @OutRes = 1 --Login is Correct
end
else
begin
set @OutRes = 0 --Bad login
end
GO
When i test the page and log in with the correct details it will not allow me to log in but i am lost as to why.
Thank you in advanced for any help you may be able to offer :)
issue in stored procedure..
Wrong 1 :
if(@OutRes > 1)
Right 1 :
if(@OutRes >= 1)
You will get 1 count in result and that is not greater than 1.
Solved SP :
CREATE PROCEDURE [dbo].[bnc_CheckUser] -- Add the parameters for the stored procedure here @UserName nvarchar(254), @Password varchar(20), @OutRes int OUTPUT AS set @OutRes = (SELECT count(*) FROM CustomerDetails.MembershipDetails WHERE [UserName] = @UserName And [Password] = @Password) if(@OutRes >= 1) begin set @OutRes = 1 --Login is Correct end else begin set @OutRes = 0 --Bad login end GO
change
if(@OutRes > 1) to if(@OutRes >= 1) in StoredProcedure
这篇关于我不确定我做错了什么。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!