Click here to Skip to main content
15,891,409 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a new problem.
My proc:
SQL
create proc sp_Login
	@UserName char(10),
	@passW char(10)
as
	select * from User where UserName=@UserName and passw=@passw
go


C#
public string Login(string user, string passw)
    {
        DataContextDataContext dcon=new DataContextDataContext();
        string msg = "";
        var q = dcon.sp_Login("John","123");
        if (q.Count() > 0)
        {
            msg="hi"+"john";
        return kq;
    }

It doesn't work.How to count number of rows and get the login name?
Posted
Comments
bhagirathimfs 16-May-12 7:35am    
You want to count the number of user present in the database????
Sandeep Mewara 16-May-12 8:17am    
What is this DataContextdataContext class?

Just change your SP to:
SQL
select COUNT(*) from User where UserName=@UserName and passw=@passw


You need just the count and nothing else. Further, you should not retrieve the password without any reason.

Handle the change in your code according to what is now retrieved back.
 
Share this answer
 
v2
Comments
giatuan2011 16-May-12 8:32am    
select Count(*) is successful in sql server.
DataContextDataContext dcon=new DataContextDataContext();
string msg = "";
var q = dcon.sp_Login("John","1234");
if (q.Count()>0)
{
msg="success";

}
return msg;
not chage.Results as before
Sandeep Mewara 16-May-12 12:31pm    
Good to know.:)
 
Share this answer
 
SQL
create proc sp_Login
	@UserName char(10),
	@passW char(10)
as
	select count(*) from User where UserName=@UserName and passw=@passw
go

C#
Collapse | Copy Code

public string Login(string user, string passw)
    {
        DataContextDataContext dcon=new DataContextDataContext();
        string msg = "";
        var q = dcon.sp_Login("John","123");
        if (q.Count() > 0)
        {
            msg="hi"+ user;
        return kq;
    }
 
Share this answer
 
v2
Comments
giatuan2011 16-May-12 8:12am    
It is successful when passw="1234".I do not think Count (*) returns the number of row.Proc does not return
Vasim889 16-May-12 8:50am    
ok.I added another solution below.you try this one.
SQL
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

Create FUNCTION sp_Login
(	
@UserName varchar(100),
@Password int
	
)
RETURNS TABLE 
AS
RETURN 
(
 select count(*) as Counts from User  where ID=@Password and [Name]=@UserName
	
)

GO


you will try this function   select * from sp_Login ("John",123)
 
Share this answer
 
v2
Comments
giatuan2011 16-May-12 9:02am    
I found:
var c = (from user in context.tblUser where user.username = txtusername.Text and user.password = txtpassword.Text).Count();
if c==1->success.
How to I change it?
giatuan2011 16-May-12 9:18am    
Sorry,Vasim889.I get good result from your code.Thank you so much
var q = dcon.sp_Login("John","123");
if (q.Count() ==1)
I have not changed, "John" and "123" to txtuser, txtpass.
Vasim889 16-May-12 9:24am    
ok .thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900