hello..im a new user of the c# programming language,.im asking for your help in finding a code for my project..can you please give me the code on how to have my own log in form???since c# doesnt have a log in form.

Recommended Answers

All 3 Replies

Its pretty simple. Put the usual user name and password textboxes on your form and one button.
In the button code, connect to your database and see if the query to count the rows with that user name/password combination = 1.
Typing "csharp login code" into Google got plenty of tutorials and snippets. Have you tried that yet?

Try this code I am using:

static class Program
{
   [STAThread]
   static void Main()
   {   
       using (Login login = new Login())
       {
            login.StartPosition = FormStartPosition.CenterScreen;
            if (login.ShowDialog() == DialogResult.OK)
            {      
                  Application.Run(new Form1(login.strUserName)); //passing the userName to the constructor of form1 (from Login form - global variable)!
            }
       }
    }
}

//form1:
public partial class Form1 : Form
{  
     string userName; 
     public Form1(string _strUser)
     {   
           InitializeComponent();
           userName = _strUser; //a local variable of a form1 class has hold user`s name (userName - which u can call it from within the form1 class!
     }
}

public partial class Login : Form
{
       public string UserName { get; private set; } 
       public Login()
       {
              InitializeComponent();
       }

       private void btn_Login_Click(object sender, EventArgs e)
       {
             string userName = textBox1.Text.Trim();
             string pswd = textBox2.Text.Trim();
             bool bLoginOk = CheckUser(userName, pswd);
             if (bLoginOK)
             {
                  this.DialogResult = DialogResult.OK;
                  this.UserName = userName; //setting global variable (a property) so it can get the name out of it later (from program.cs)
                  //code will close login form, and go back to program.cs ...
             }
             else
            {
                 //you can show here a messageBox that userName and password do not match if you like!
                 //AND the login form still stays open!!
            }
      }
   
  
      private bool CheckUser(string name, string pswd)
      {
            bool bChecking = false;

            //do the login checking, if username and password match, return true, else false

            //if username and password match:
            bChecking = true;

            return bChecking;
      }     
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.