We help IT Professionals succeed at work.

Code for Login Page ??

chpk asked
I just want to create one page like the following :
         UserName :  “Text Box1”
         Password  :  “Text Box2”
Well in this page we have to enter username in TextBox1 and Password in TextBox2 and then when you click on Login Button then it needs to connect to the database and it has to look the data into the following table.

          Login(Table Name):  Id    UserName  PassWord
                                                1         abc                123
                                               2           def                 456
 I need code for the following
After Enter the username and password when you click to the button it has to come to the above table and it has to check in Username column is that available or not and then it needs to check for that particular password in the password column.
I want code for the above .
Please let me know if the question is not clear(need code in C# and database is SQL Server)
Watch Question

Top Expert 2013
Check these:
1: Uses stored proc to check against DB:

2: Inline Sql: http://forums.asp.net/t/1250726.aspx

Below its the code from link2.
           // Function name Authentication which will get check the user_name and passwrod from sql database then return a value true or false
           protected static Boolean Authentication(string username, string password)
                 string sqlstring;
                 sqlstring = "Select user_name, password from [user_table] where user_name='" + username + "' and password ='" + password + "'";
                 // create a connection with sqldatabase 
                 System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(
                              " Data Source=datebaseservername;Initial Catalog=datebasename;UserID=databaseusername;Password=databasepassword;Connect Timeout=10;TrustServerCertificate=True " ); 

                // create a sql command which will user connection string and your select statement string 
                System.Data.SqlClient.SqlCommand comm = new System.Data.SqlClient.SqlCommand(sqlstring,con);
                // create a sqldatabase reader which will execute the above command to get the values from sqldatabase
                System.Data.SqlClient.SqlDataReader reader;

                // open a connection with sqldatabase

                // execute sql command and store a return values in reade
                reader = comm.ExecuteReader();

                // check if reader has any value then return true otherwise return false
                if (reader.Read())
                   return true;
                   return false;

Open in new window