2013-04-05 24 views
0

我有下面的代码登录页面:登录错误 “无法转换隐式空字符串”

程序:

ALTER PROCEDURE [dbo].[Login_Validation] 

@email varchar(100), 
@password varchar(50) 

AS 
BEGIN 

DECLARE @cnt INTEGER 
SET @cnt = (SELECT COUNT(*) FROM SUBSCRIBER_MASTER WHERE [email protected] AND [Password][email protected] AND [Status]='Y') 

END 

类文件:

public void Login_Validation(string email, string password) 
{ 

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString); 
try 
{ 
    LocalConnection_Class(con); 
    SqlCommand cmd = new SqlCommand("Login_Validation", con); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.Parameters.Add(new SqlParameter("@email", SqlDbType.VarChar, 100)).Value = email; 
    cmd.Parameters.Add(new SqlParameter("@password", SqlDbType.VarChar, 50)).Value = password; 
    SqlDataReader dr = cmd.ExecuteReader(); 
    if (dr.HasRows) 
    { 
     dr.Read(); 
    } 

    cmd.Dispose(); 
    con.Close(); 
} 
catch (Exception) 
{ } 
} 

CS文件:

protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
string login = obj.Login_Validation(txtEmail.Text, txtPassword.Text); 
} 

我无法根据站在如何解决错误。有人可以帮我吗? 它给了我一个错误:“无法将隐式void转换为obj.Login_Validation上的字符串”。

+0

该错误是直线前进..您需要更改该方法签名以获得返回以及返回类型的字符串' – MethodMan 2013-04-05 18:08:54

+0

'请说明该存储过程返回的结果以及您在有条件支票和/或 – MethodMan 2013-04-05 18:14:44

+0

后缺少的代码您期待什么'Login_Validation '回来? – Tory 2013-04-05 18:19:12

回答

-1

您声明了一个函数void,public void Login_Validation并试图从中分配一个字符串。

这不起作用,错误信息非常明确。

更改方法签名以返回一个字符串,并从存储过程中添加带有相关数据的return语句。

编辑 - 实际上你的存储过程似乎没有返回任何东西。从那里开始! 也许是输出参数。

ALTER PROCEDURE [dbo].[Login_Validation] 

@email varchar(100), 
@password varchar(50), 
@isValid bit outbit 
AS 
BEGIN 


SET @isValid = EXISTS(SELECT * FROM SUBSCRIBER_MASTER WHERE [email protected] AND [Password][email protected] AND [Status]='Y') 

END 
-1

您的Login_Validation不会返回任何内容。它返回一个'空白'。

如果您希望它返回某些内容,请更改签名以返回一个字符串。

public string Login_Validation 

然后返回你想要的任何东西。

-1

试试这个使用SqlDataReader的而不是无效

public SqlDataReader Login_Validation(string email, string password) 
{ 
    SqlConnection con = new  SqlConnection(ConfigurationManager.ConnectionStrings["ConnString"].ConnectionString); 
    LocalConnection_Class(con); 
    SqlCommand cmd = new SqlCommand("Login_Validation", con); 
    cmd.CommandType = CommandType.StoredProcedure; 
    cmd.Parameters.Add(new SqlParameter("@email", SqlDbType.VarChar, 100)).Value = email; 
    cmd.Parameters.Add(new SqlParameter("@password", SqlDbType.VarChar, 50)).Value = password; 
    SqlDataReader dr = cmd.ExecuteReader(); 
    cmd.Dispose(); 
    con.Close(); 
    return dr ; 
} 

并调用这种方式

protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
SqlDataReader login = obj.Login_Validation(txtEmail.Text, txtPassword.Text); 

if(login.HasRows) 
{ 
//Login Successfully 
} 
else 
{ 
//Login Failed 
} 
} 
0

DJ KRAZE告诉你

string login = obj.Login_Validation(txtEmail.Text, txtPassword.Text);建议你的方法Login_Validation将返回一个字符串,所以你需要做2件事

首先
改变你的方法头
public void Login_Validation(string email, string password)
public string Login_Validation(string email, string password)

谢胜利 现在你需要在你的方法添加回

例如

public string Login_Validation(string email, string password) 
    { 
     //... 

     try 
     { 
      //... 

      return //<- your string if succeed 
     } 
     catch (Exception) 
     { 
      return //<- your string if an error occurs 
     } 

     // or you place just one return here 
    } 
相关问题