Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. C# SQL express Login

C# SQL express Login

Scheduled Pinned Locked Moved Database
csharpdatabasehelpvisual-studiolinq
17 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    kadaen
    wrote on last edited by
    #1

    i am working on a project and one section of the program is password prohibitied, i am using visual studio 2010 coding in C# and using SQL express database, the project is working perfectly the only i can not seem to figure out is how to create the login form ... ex. you open the project then click on personal information, window pops up asking for username and password, if the information you neter matches the information that is in the database you will be able to continue if the information is wrong a messagebox will show up saying invalid entry been looking all over the net for the problem im having but not having any luck with this issue here is what i have so far

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace EasyStorePro
    {
        public partial class LoginForm : Form
        {
            public LoginForm()
            {
                InitializeComponent();
            }
    
            private void btnLogin_Click(object sender, EventArgs e)
            {
                System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
    
                conn.ConnectionString = "Data Source=KADAEN-PC\\SQLEXPRESS;Initial Catalog=EasyStorePro;Persist Security Info=True;User ID=************;Password=**********";
                string str = ("SELECT username,password FROM dbo.Login WHERE username='"+ tbUsername + "' and password='"+ mtbPassword +"'");
    
                System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn);
    
                conn.Open();
    
                DataTable dt = new DataTable();
                System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
    
                da.Fill(dt);
    
                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("Welcome");
                }
                else
                {
                    MessageBox.Show("Invalid Entry");
                }
    
                conn.Close();
            }
        }
    }
    

    Thank you for your time

    N L S 3 Replies Last reply
    0
    • K kadaen

      i am working on a project and one section of the program is password prohibitied, i am using visual studio 2010 coding in C# and using SQL express database, the project is working perfectly the only i can not seem to figure out is how to create the login form ... ex. you open the project then click on personal information, window pops up asking for username and password, if the information you neter matches the information that is in the database you will be able to continue if the information is wrong a messagebox will show up saying invalid entry been looking all over the net for the problem im having but not having any luck with this issue here is what i have so far

      using System;
      using System.Collections.Generic;
      using System.ComponentModel;
      using System.Data;
      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      
      namespace EasyStorePro
      {
          public partial class LoginForm : Form
          {
              public LoginForm()
              {
                  InitializeComponent();
              }
      
              private void btnLogin_Click(object sender, EventArgs e)
              {
                  System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
      
                  conn.ConnectionString = "Data Source=KADAEN-PC\\SQLEXPRESS;Initial Catalog=EasyStorePro;Persist Security Info=True;User ID=************;Password=**********";
                  string str = ("SELECT username,password FROM dbo.Login WHERE username='"+ tbUsername + "' and password='"+ mtbPassword +"'");
      
                  System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn);
      
                  conn.Open();
      
                  DataTable dt = new DataTable();
                  System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
      
                  da.Fill(dt);
      
                  if (dt.Rows.Count > 0)
                  {
                      MessageBox.Show("Welcome");
                  }
                  else
                  {
                      MessageBox.Show("Invalid Entry");
                  }
      
                  conn.Close();
              }
          }
      }
      

      Thank you for your time

      N Offline
      N Offline
      nagendrathecoder
      wrote on last edited by
      #2

      How this is a database related question? :doh: BTW, you can show the Login form as dialog using Form_Name.ShowDialog(). It'll open up as a popup dialog.

      K 1 Reply Last reply
      0
      • N nagendrathecoder

        How this is a database related question? :doh: BTW, you can show the Login form as dialog using Form_Name.ShowDialog(). It'll open up as a popup dialog.

        K Offline
        K Offline
        kadaen
        wrote on last edited by
        #3

        I figured it was a database question, the problem lies in my program connecting and verifying data entered by the user to the information that is the database, I don’t have any problems bringing up the login form, the only complications I am having is with the username & password being verified with the information in the database and allowing the user to continue to their destination as long as all information entered is correct.

        N 2 Replies Last reply
        0
        • K kadaen

          I figured it was a database question, the problem lies in my program connecting and verifying data entered by the user to the information that is the database, I don’t have any problems bringing up the login form, the only complications I am having is with the username & password being verified with the information in the database and allowing the user to continue to their destination as long as all information entered is correct.

          N Offline
          N Offline
          nagendrathecoder
          wrote on last edited by
          #4

          ok, are you getting any error? Exactly what problem are you facing?

          K 1 Reply Last reply
          0
          • N nagendrathecoder

            ok, are you getting any error? Exactly what problem are you facing?

            K Offline
            K Offline
            kadaen
            wrote on last edited by
            #5

            when I load up the program and open the login form, I input the username and password no error pops up just the messagebox, that says invalid entry, but the information i entered is the exact same information I put into the database itself. i am thinking my problem lies within the if statement but i could be wrong

             if (dt.Rows.Count > 0)
                        {
                            MessageBox.Show("Welcome");
                        }
                        else
                        {
                            MessageBox.Show("Invalid Entry");
                        }
            

            i should be getting the messagebox that states Welcome, but im not.

            1 Reply Last reply
            0
            • K kadaen

              I figured it was a database question, the problem lies in my program connecting and verifying data entered by the user to the information that is the database, I don’t have any problems bringing up the login form, the only complications I am having is with the username & password being verified with the information in the database and allowing the user to continue to their destination as long as all information entered is correct.

              N Offline
              N Offline
              nagendrathecoder
              wrote on last edited by
              #6

              Try using this

              string str = "select count(*)from Login where username='"+ tbUsername + "' and password='"+ mtbPassword +"'");

              System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn);

              conn.Open();

              int rows = cmd.ExecuteScalar();

              if (row > 0)
              {
              MessageBox.Show"Welcome");
              }
              else
              {
              MessageBox.Show("Invalid Entry");
              }
              conn.Close();

              K 1 Reply Last reply
              0
              • N nagendrathecoder

                Try using this

                string str = "select count(*)from Login where username='"+ tbUsername + "' and password='"+ mtbPassword +"'");

                System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn);

                conn.Open();

                int rows = cmd.ExecuteScalar();

                if (row > 0)
                {
                MessageBox.Show"Welcome");
                }
                else
                {
                MessageBox.Show("Invalid Entry");
                }
                conn.Close();

                K Offline
                K Offline
                kadaen
                wrote on last edited by
                #7

                i get the following error when trying to run the program Error 1 Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)

                N 1 Reply Last reply
                0
                • K kadaen

                  i get the following error when trying to run the program Error 1 Cannot implicitly convert type 'object' to 'int'. An explicit conversion exists (are you missing a cast?)

                  N Offline
                  N Offline
                  nagendrathecoder
                  wrote on last edited by
                  #8

                  Ofcourse you have to cast it to int, i missed it and you too.

                  int row = Convert.ToInt32(cmd.ExecuteScalar());

                  K 1 Reply Last reply
                  0
                  • N nagendrathecoder

                    Ofcourse you have to cast it to int, i missed it and you too.

                    int row = Convert.ToInt32(cmd.ExecuteScalar());

                    K Offline
                    K Offline
                    kadaen
                    wrote on last edited by
                    #9

                    lol yeah i just converted it then seen your reply, ok the program runs now with no errors but i still get the Invalid Entry MessageBox when inputting the username and password, i even added a new entry to the database and still get the same thing =/ .. this has had be stumped for the past few days

                    N 1 Reply Last reply
                    0
                    • K kadaen

                      lol yeah i just converted it then seen your reply, ok the program runs now with no errors but i still get the Invalid Entry MessageBox when inputting the username and password, i even added a new entry to the database and still get the same thing =/ .. this has had be stumped for the past few days

                      N Offline
                      N Offline
                      nagendrathecoder
                      wrote on last edited by
                      #10

                      :confused: Trace the program, see what is the value of "str" before executing the command.

                      K 1 Reply Last reply
                      0
                      • N nagendrathecoder

                        :confused: Trace the program, see what is the value of "str" before executing the command.

                        K Offline
                        K Offline
                        kadaen
                        wrote on last edited by
                        #11

                        I looked at it, the str is the string for SELECT username,password FROM Login WHERE username='" + tbUsername + "' and password='" + mtbPassword + "'" i removed the string for this and put it directly into the cmd section...

                        System.Data.SqlClient.SqlCommand cmd;
                                    cmd = new System.Data.SqlClient.SqlCommand("SELECT username,password FROM Login WHERE username='" + tbUsername + "' and password='" + mtbPassword + "'", conn);
                        

                        still not having any luck getting it to work. thank you for time

                        N 1 Reply Last reply
                        0
                        • K kadaen

                          i am working on a project and one section of the program is password prohibitied, i am using visual studio 2010 coding in C# and using SQL express database, the project is working perfectly the only i can not seem to figure out is how to create the login form ... ex. you open the project then click on personal information, window pops up asking for username and password, if the information you neter matches the information that is in the database you will be able to continue if the information is wrong a messagebox will show up saying invalid entry been looking all over the net for the problem im having but not having any luck with this issue here is what i have so far

                          using System;
                          using System.Collections.Generic;
                          using System.ComponentModel;
                          using System.Data;
                          using System.Drawing;
                          using System.Linq;
                          using System.Text;
                          using System.Windows.Forms;
                          
                          namespace EasyStorePro
                          {
                              public partial class LoginForm : Form
                              {
                                  public LoginForm()
                                  {
                                      InitializeComponent();
                                  }
                          
                                  private void btnLogin_Click(object sender, EventArgs e)
                                  {
                                      System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
                          
                                      conn.ConnectionString = "Data Source=KADAEN-PC\\SQLEXPRESS;Initial Catalog=EasyStorePro;Persist Security Info=True;User ID=************;Password=**********";
                                      string str = ("SELECT username,password FROM dbo.Login WHERE username='"+ tbUsername + "' and password='"+ mtbPassword +"'");
                          
                                      System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn);
                          
                                      conn.Open();
                          
                                      DataTable dt = new DataTable();
                                      System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
                          
                                      da.Fill(dt);
                          
                                      if (dt.Rows.Count > 0)
                                      {
                                          MessageBox.Show("Welcome");
                                      }
                                      else
                                      {
                                          MessageBox.Show("Invalid Entry");
                                      }
                          
                                      conn.Close();
                                  }
                              }
                          }
                          

                          Thank you for your time

                          L Offline
                          L Offline
                          loyal ginger
                          wrote on last edited by
                          #12

                          Just set a break point after the SELECT query is constructed. When it breaks at that point, copy the value of the string out and paste it somewhere else to execute the query to see the result. You may figure out the problem that way.

                          C 1 Reply Last reply
                          0
                          • K kadaen

                            I looked at it, the str is the string for SELECT username,password FROM Login WHERE username='" + tbUsername + "' and password='" + mtbPassword + "'" i removed the string for this and put it directly into the cmd section...

                            System.Data.SqlClient.SqlCommand cmd;
                                        cmd = new System.Data.SqlClient.SqlCommand("SELECT username,password FROM Login WHERE username='" + tbUsername + "' and password='" + mtbPassword + "'", conn);
                            

                            still not having any luck getting it to work. thank you for time

                            N Offline
                            N Offline
                            nagendrathecoder
                            wrote on last edited by
                            #13

                            I said you to see the value of "str" after tbUsername & mtbPassword is binding with it. Maybe something is wrong with those values.

                            S 1 Reply Last reply
                            0
                            • L loyal ginger

                              Just set a break point after the SELECT query is constructed. When it breaks at that point, copy the value of the string out and paste it somewhere else to execute the query to see the result. You may figure out the problem that way.

                              C Offline
                              C Offline
                              c0der2009
                              wrote on last edited by
                              #14

                              I too think this is the way to go. Step 1: First you have to make sure the connection string is correct. Try this. string str = ("SELECT username,password FROM dbo.Login "); System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn); conn.Open(); DataTable dt = new DataTable(); System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd); da.Fill(dt); then debug and see if the da has all details from the database table. Step 2: If step 1 was successful, use your program, debug and see what is the value of str just before the query is fired. Execute that query directly from Query Analyzer and verify the results.

                              K 1 Reply Last reply
                              0
                              • C c0der2009

                                I too think this is the way to go. Step 1: First you have to make sure the connection string is correct. Try this. string str = ("SELECT username,password FROM dbo.Login "); System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn); conn.Open(); DataTable dt = new DataTable(); System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd); da.Fill(dt); then debug and see if the da has all details from the database table. Step 2: If step 1 was successful, use your program, debug and see what is the value of str just before the query is fired. Execute that query directly from Query Analyzer and verify the results.

                                K Offline
                                K Offline
                                kadaen
                                wrote on last edited by
                                #15

                                ok with that i get an error due to no connection string so i add in conn.ConnectionString = "Data Source=KADAEN-PC\\SQLEXPRESS;Initial Catalog=EasyStorePro;Persist Security Info=True;User ID=********;Password=***********"; and it runs i think my main problem is in the if statment section, more or less getting the textbox to verify itself with the information in the database so something like password is equal to mtbPassword.Text

                                1 Reply Last reply
                                0
                                • K kadaen

                                  i am working on a project and one section of the program is password prohibitied, i am using visual studio 2010 coding in C# and using SQL express database, the project is working perfectly the only i can not seem to figure out is how to create the login form ... ex. you open the project then click on personal information, window pops up asking for username and password, if the information you neter matches the information that is in the database you will be able to continue if the information is wrong a messagebox will show up saying invalid entry been looking all over the net for the problem im having but not having any luck with this issue here is what i have so far

                                  using System;
                                  using System.Collections.Generic;
                                  using System.ComponentModel;
                                  using System.Data;
                                  using System.Drawing;
                                  using System.Linq;
                                  using System.Text;
                                  using System.Windows.Forms;
                                  
                                  namespace EasyStorePro
                                  {
                                      public partial class LoginForm : Form
                                      {
                                          public LoginForm()
                                          {
                                              InitializeComponent();
                                          }
                                  
                                          private void btnLogin_Click(object sender, EventArgs e)
                                          {
                                              System.Data.SqlClient.SqlConnection conn = new System.Data.SqlClient.SqlConnection();
                                  
                                              conn.ConnectionString = "Data Source=KADAEN-PC\\SQLEXPRESS;Initial Catalog=EasyStorePro;Persist Security Info=True;User ID=************;Password=**********";
                                              string str = ("SELECT username,password FROM dbo.Login WHERE username='"+ tbUsername + "' and password='"+ mtbPassword +"'");
                                  
                                              System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(str, conn);
                                  
                                              conn.Open();
                                  
                                              DataTable dt = new DataTable();
                                              System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
                                  
                                              da.Fill(dt);
                                  
                                              if (dt.Rows.Count > 0)
                                              {
                                                  MessageBox.Show("Welcome");
                                              }
                                              else
                                              {
                                                  MessageBox.Show("Invalid Entry");
                                              }
                                  
                                              conn.Close();
                                          }
                                      }
                                  }
                                  

                                  Thank you for your time

                                  S Offline
                                  S Offline
                                  shaikh adil
                                  wrote on last edited by
                                  #16

                                  can i get the corrected code plz i am working on the same project

                                  1 Reply Last reply
                                  0
                                  • N nagendrathecoder

                                    I said you to see the value of "str" after tbUsername & mtbPassword is binding with it. Maybe something is wrong with those values.

                                    S Offline
                                    S Offline
                                    shaikh adil
                                    wrote on last edited by
                                    #17

                                    can i get the corrected code plz i am working on the same project

                                    1 Reply Last reply
                                    0
                                    Reply
                                    • Reply as topic
                                    Log in to reply
                                    • Oldest to Newest
                                    • Newest to Oldest
                                    • Most Votes


                                    • Login

                                    • Don't have an account? Register

                                    • Login or register to search.
                                    • First post
                                      Last post
                                    0
                                    • Categories
                                    • Recent
                                    • Tags
                                    • Popular
                                    • World
                                    • Users
                                    • Groups