Login Page in ASP.net

To make a login page which would acess the database and tell the user that he has been loged on we will require a database table in which username and password is stored. This work will be done in 3 parts
First make a connection string Secondly accessing the data and thirdly loging in.

First we have to make a connection string the connection string is made in Web.Config like this

<connectionStrings>

<add name=”ConnectDBString” connectionString=”server=localhost;Database=test;Integrated Security=SSPI;” />

</connectionStrings>

by this we have made a connection string a connection string has 3 basic parts 1. Server Name 2. Database Name and 3. Authentication Type.
After making the connection string we have to get data from the database to get the data we will use connection less approach and we will get data by calling a stored procedure from database
To get data from database we write these lines to read the connection string from web.config

public db_data()
{
myConnectionString = ConfigurationManager.ConnectionStrings["ConnectDBString"].ConnectionString;

try
{
objcon = new SqlConnection(myConnectionString);
}

catch (Exception ex)
{

}
}

public DataTable Athuntication(string username, string password)
{
SqlCommand m_SqlCmd = new SqlCommand();
m_SqlCmd.CommandText = “dbo.[pr_select_login]”;
m_SqlCmd.CommandType = CommandType.StoredProcedure;
DataTable dtToReturn = new DataTable(“tbl_login”);
SqlDataAdapter m_SqlAdp = new SqlDataAdapter(m_SqlCmd);
m_SqlCmd.Connection = objcon;
try
{
m_SqlCmd.Parameters.Add(new SqlParameter(“@username”, SqlDbType.VarChar, 30, ParameterDirection.Input, false, 10, 0, “”, DataRowVersion.Proposed, username));
m_SqlCmd.Parameters.Add(new SqlParameter(“@password”, SqlDbType.VarChar, 50, ParameterDirection.Input, false, 10, 0, “”, DataRowVersion.Proposed, password));

m_SqlAdp.Fill(dtToReturn);
return dtToReturn;
}
catch(Exception ex)
{
MessageBox.Show(ex.ToString());
return null;
}

}

}

Now we have a datatable full of required data we can use this for logging in.

Comments are closed.