2010-09-10 65 views
0

我創建了這個簡單的登錄頁面。它應該像「錯誤的用戶」當用戶不存在,「密碼錯誤」時輸入的密碼不正確,等需要ASP.NET初學者登錄應用程序的幫助

標籤文本顯示,但每當我嘗試和登錄它始終顯示「錯誤的用戶」時,用戶確實存在於表格中。我已經手動輸入用戶名和密碼,而不是使用此應用程序中的任何存儲過程。

這是代碼;請告訴我我做錯了什麼。顯然這是一些邏輯錯誤。

Default.cs文件:

using System; 
using System.Configuration; 
using System.Data; 
using System.Data.SqlClient; 
using System.Configuration; 
using System.Linq; 
using System.Web; 
using System.Web.Security; 
using System.Web.UI; 
using System.Web.UI.HtmlControls; 
using System.Web.UI.WebControls; 
using System.Web.UI.WebControls.WebParts; 
using System.Xml.Linq; 

public partial class _Default : System.Web.UI.Page 
{ 
    SqlConnection con = new SqlConnection(); 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     con.ConnectionString = ConfigurationManager.ConnectionStrings["cn"].ConnectionString; 
     if (con.State == ConnectionState.Closed) 
     { 
      con.Open(); 
     } 
    } 

    protected void Button1_Click(object sender, EventArgs e) 
    { 
     Int32 d = checkUser(UserName.Text, Password.Text); 
     if (d == -1) 
     { 
      Label1.Text = "Wrong user"; 

     } 
     if (d == -2) 
     { 
      Label1.Text = "Wrong password"; 
     } 

     if (d == 2) 

     { 
      Label1.Text = "Login Successful"; 

     } 
    } 


    private Int32 checkUser(String u, String p) 
    { 
     SqlCommand cmd = new SqlCommand(); 
     cmd.CommandText = "LogInCheck"; 
     cmd.CommandType = CommandType.StoredProcedure; 
     cmd.Connection = con; 
     cmd.Parameters.Add("@u", SqlDbType.VarChar, 50).Value = u; 
     cmd.Parameters.Add("@p", SqlDbType.VarChar, 50).Value = p; 
     SqlParameter p1 = new SqlParameter("@ret", SqlDbType.Int); 
     p1.Direction = ParameterDirection.ReturnValue; 
     cmd.Parameters.Add(p1); 
     cmd.ExecuteNonQuery(); 
     Int32 k = Convert.ToInt32(cmd.Parameters["@ret"].Value); 
     return k; 
    } 
} 

.aspx文件:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head runat="server"> 
     <title>Untitled Page</title> 
    </head> 
    <body> 
     <form id="form1" runat="server"> 
      <div> 
       Username :&nbsp; 
       <asp:TextBox ID="UserName" runat="server"></asp:TextBox> 
       <br /> 

       Password : 
       <asp:TextBox ID="Password" runat="server" TextMode="Password"></asp:TextBox> 
       &nbsp; 

       <asp:Button ID="Button1" runat="server" Text="Login" onclick="Button1_Click"/> 
       <br /> 

       <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
      </div> 
     </form> 
    </body> 
</html> 

我的存儲過程,LogInCheck:

ALTER PROCEDURE LogInCheck 
    (
     @u varchar(50), 
     @p varchar(50) 
    ) 

AS 
    Declare @ap varchar(50) 
    Select @ap from tbuser where [email protected] 
    if @ap is null 
     begin 
      return -1 
     end 
    else 
     if @[email protected] 
      return 1 
     else 
      return -2 

回答

-1

你的選擇查詢不把任何值@ AP。試試這個:

Select @ap=<col_name> from tbuser where [email protected] 

更新 嘗試把一個開始和結束你開始否則你的主了。

Declare @ap varchar(50) 
Select @ap from tbuser where [email protected] 
if @ap is null 
begin 
return -1 
end 
else 
begin   --Here 
if @[email protected] 
return 1 
else 
return -2 
end   --And here 
+0

在我的表山坳名UNAME和upass..I嘗試什麼üsaid..changed爲 「從哪裏tbuser UNAME = @ U選擇@ AP = upass」 ..其仍然沒有工作.. .displaying錯誤的密碼只..每次 – Serenity 2010-09-10 06:47:23

+0

確定其檢查用戶名好,但只是不會檢查密碼..即使我輸入正確的密碼......它說「錯誤的密碼」 – Serenity 2010-09-10 06:55:00

+1

hellloo ?????? ?? – Serenity 2010-09-10 07:04:22