Skip to content
Loading
Checking form credentials with database in .aspx web forms.  
  •   
  •   
  •   
  •   
  •   
  •   
  • login.aspx.cs :
    1. using Newtonsoft.Json;  
    2. using System;  
    3. using System.Collections.Generic;  
    4. using System.Data;  
    5. using System.Data.SqlClient;  
    6. using System.Linq;  
    7. using System.Web;  
    8. using System.Web.Services;  
    9. using System.Web.UI;  
    10. using System.Web.UI.WebControls;  
    11. using System.Web.Security;  
    12. using System.Configuration;  
    13. public partial class login : System.Web.UI.Page  
    14. {  
    15. protected void Page_Load(object sender, EventArgs e)  
    16. {  
    17. }  
    18. [WebMethod]  
    19. public static string ValidateUser(string username, string password)  
    20. {  
    21. string status = "";  
    22. int userId = 0;  
    23. string constr = ConfigurationManager.ConnectionStrings["ERPConnectionString"].ConnectionString;  
    24. using (SqlConnection con = new SqlConnection(constr))  
    25. {  
    26. using (SqlCommand cmd = new SqlCommand("Validate_User"))  
    27. {  
    28. cmd.CommandType = CommandType.StoredProcedure;  
    29. cmd.Parameters.AddWithValue("@Username", username);  
    30. cmd.Parameters.AddWithValue("@Password", password);  
    31. cmd.Connection = con;  
    32. con.Open();  
    33. userId = Convert.ToInt32(cmd.ExecuteScalar());  
    34. con.Close();  
    35. }  
    36. switch (userId)  
    37. {  
    38. case -1:  
    39. status = "Username and/or password is incorrect.";  
    40. break;  
    41. case -2:  
    42. status = "Account has not been activated.";  
    43. break;  
    44. default:  
    45. break;  
    46. }  
    47. }  
    48. return status;  
    49. }  
    50. }  
    success.aspx :
    1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="success.aspx.cs" Inherits="View_success" %>  
    2.   
    3. "http://www.w3.org/1999/xhtml">  
    4. "server">  
    5. "https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js">  
    6. "stylesheet" media="screen" href='https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css' />  
    7. "stylesheet" href="login.css" />  
    8.   
    9.   
    10.   
    11. "form1" runat="server">  
    12.   
    13. Login successful!

        
      
  •   
  •   
  •   
  • I don't know how to check the details with the database and process it to the next page.
     
    Regards
    Suresh Kumar P

    5 Replies

    Know the answer? Post it — somebody with the same question will find it here.