Hi friends,
When user inputs username and password how to validate user against many users in database?
I dont want to use open connection ie DataReader for this.. After loading DataSet with all the users from the table in SQL how can i check to validate the login?
Please guide
Loading
Santhosh Kumar JayaramanPosted Jul 25, 2012, 8:33 AM
If you didnt understand the code, then check my previous answer,..I have added comments now to explain the code
rani m mkPosted Jul 25, 2012, 8:32 AM
Santhosh Kumar JayaramanPosted Jul 24, 2012, 1:51 AM
//Get all records from user table and fill it to adapter
SqlDataAdapter adapter = new SqlDataAdapter("select * from Users", connection);
DataTable dt = new DataTable();
//Fill the adapter to datatable
adapter.Fill(dt);
//This will be the linq query in which first am converting datatable as Enumerable. If its Enumerable, then we can loop and query each records . Then am using Linq and checking whether Username column inside datatable(now its enumerable) has value User1 and also samething for password. if it matches, then i am selecting the columns i wanted such as userid, password, name,etc
var user=from u in dt.AsEnumerable()
where u.Field
&& u.Field
select new {
userId=u.Field
userName=u.Field
Password=u.Field
};
Satyapriya NayakPosted Jul 24, 2012, 1:49 AM
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="login_using_parameter._Default" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
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;
using System.Data.SqlClient;
namespace login_using_parameter
{
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str = null;
SqlCommand com;
protected void btn_login_Click(object sender, EventArgs e)
{
object obj = null;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select count(*) from login where UserName=@UserName and Password =@Password";
com = new SqlCommand(str, con);
com.CommandType = CommandType.Text;
com.Parameters.AddWithValue("@UserName", TextBox_user_name.Text);
com.Parameters.AddWithValue("@Password", TextBox_password.Text);
obj = com.ExecuteScalar();
if ((int)(obj) != 0)
{
lb1.Text = "WELLCOME :: " + TextBox_user_name.Text;
}
else
{
lb1.Text = "invalid user name and password";
}
con.Close();
}
}
}
Thanks