Click here to Skip to main content
15,900,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a login form and i want to check the login form. It include username and password. How can i check username and password with sqlser database. I need code to solve this problem. Anyone can help me.
Posted

I think it is better to start from
here[^]

It will help you to create a standard login page in ASP.NET. :rose:
 
Share this answer
 
If you are using a windows application, create the login form with two textboxes and two labels for the username and password. Set the UseSystemPassword property to true. Then, create the sql database with at least a table with a username and a password column. Here is a sample code: Please note that DataHelper is my class for connecting to the SQL database in just two lines and LoginInfo is a class for encapsulating the user, his roles and rights. If you want just the user and password, ignore the rest of the code.

private LoginInfo Login()
        {
            LoginInfo result = new LoginInfo();
            try
            {
                DataHelperMS dh = new DataHelperMS();
                
                // Get the user
                String sql = String.Format(@"SELECT user_id, user_name
                            FROM  Users
                            WHERE (user_id = '{0}' AND user_password = '{1}')", txtUsername.Text.Replace("'", ""), txtPassword.Text.Replace("'", ""));
                DataTable userTable = dh.ExecuteDataSet(sql).Tables[0];
                result.User_id = userTable.Rows[0][0].ToString();
                result.User_name = userTable.Rows[0][1].ToString();
                
                // Get his roles
                String sql2 = String.Format(@"SELECT R.role_id, R.role_name
                            FROM  Roles R INNER JOIN UserRoles UR ON R.role_id = UR.ur_role_id 
                            WHERE (UR.ur_user_id = '{0}')", result.User_id);
                DataTable roleTable = dh.ExecuteDataSet(sql2).Tables[0];
                foreach (DataRow row in roleTable.Rows)
                {
                    result.Roles_ids.Add(row[0].ToString());
                    result.Roles_names.Add(row[1].ToString());
                }
                // Get his rights
                foreach (String id in result.Roles_ids)
                {
                    String sql3 = String.Format(@"SELECT R.right_id, R.right_name
                            FROM  Rights R INNER JOIN RoleRights RR ON R.right_id = RR.rr_right_id 
                            WHERE (RR.rr_role_id = '{0}')", id);
                    DataTable rightTable = dh.ExecuteDataSet(sql3).Tables[0];
                    foreach (DataRow row in rightTable.Rows)
                    {
                        result.Rights_ids.Add(row[0].ToString());
                        result.Rights_names.Add(row[1].ToString());
                    }
                }
            }
            catch (Exception) // User could not be found
            {
                result = null;
            }
            return result;
        }
 
Share this answer
 
I need code to solve this problem
No one will provide you code until you try yourself first.

How can i check username and password with sqlser database
1. On sumbit button of the login form, get the username and password
2. Pass on the combination to database for verification
3. Verify in the database if the combination is valid using a query. If you get back a valid result then return back true/profile of the user.
4. If combination was found, successful login or else failure.
 
Share this answer
 

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