Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Creating a c# login form using ado

Posted on 2009-04-08
6
Medium Priority
?
445 Views
Last Modified: 2013-12-17
Hi

im pretty new to C#.net and want to create a login form using an ADO connection to check password in a SQl db and if the pasword is correect then load up the frmMain. I have created the form with 2 controls (txtUserName and txtPassword) and 2 fields called UserName and Password in table SystemUsers, established connection to my SQL db but im stuck on the bit that goes away and checks the table, any help would be appreciated!!
i have uploaded the code i have done so far
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
 
namespace ClientDetailsApplication1
{
    public partial class frmLogin : Form
    {
        public frmLogin()
        {
            InitializeComponent();
        }     
       
            
        private void btnEnter_Click(object sender, EventArgs e)
        {
            System.Data.SqlClient.SqlConnection conn = new      System.Data.SqlClient.SqlConnection();
            conn.ConnectionString = "integrated security=SSPI;data source=<sqlInstanceName;" +
            "persist security info=False;initial catalog=<database>";
            try
            {
             //if succesful login then open connection and display Main form
                conn.Open();  
                frmMain frmMainDialog = new frmMain();
                frmMainDialog.Show();          
            }
            catch
            {
                MessageBox.Show("Incorrect User name or Password");
            }
            
        }                                                            
                    
                   
       }
}

Open in new window

0
Comment
Question by:wilko100
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
6 Comments
 
LVL 15

Expert Comment

by:spprivate
ID: 24102216
Create this storedprocedure and pass the parameters from your form.
Get the out put flag check for it in the if condition and act accordingly.
Create Procedure CheckValid
@UserName varchar,
@Password varchar,
@returnflag varchar output
as
 
 
 
select *  from UserLogin where username=@Username and pwd=@Password
 
 
if @@rowcount>=1
Begin
     set @returnflag='T'
     
end
 
else
 
Begin
      set @returnflag='F'
end      

Open in new window

0
 
LVL 14

Expert Comment

by:jjardine
ID: 24102229
You need to create a command and execute a reader or some other method to compare the data.   You could do it like this replace your conn.Open call with this code:

using (SqlCommand cmd = new SqlCommand())
{
     cmd.Connection = conn;
     cmd.CommandText = "SELECT UserName FROM SystemUsers WHERE UserName = @UserName AND Password = @Password");
     cmd.Paramaters.AddWithValue("@UserName",txtUserName.Text);
     cmd.Parameters.AddWithValue("@Password",txtPassword.Text);

     cmd.Connection.Open();

     SqlDataReader reader = cmd.ExecuteReader();

     if(reader.HasRows)  // The user must exist and is logged in
         log in
     else                          //  No matching records.. user doesn't exist
         show error message

    cmd.Connection.Close();
}
0
 
LVL 15

Expert Comment

by:spprivate
ID: 24102243
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 

Author Comment

by:wilko100
ID: 24102552
when i apply jjardine fix i get this error..

Error      1      'System.Data.SqlClient.SqlCommand' does not contain a definition for 'Paramaters' and no extension method 'Paramaters' accepting a first argument of type 'System.Data.SqlClient.SqlCommand' could be found (are you missing a using directive or an assembly reference?)      

complaining about cmd.Paramaters is that anything to do with  a particular 'namespace' missing?


0
 
LVL 14

Accepted Solution

by:
jjardine earned 2000 total points
ID: 24102625
Sorry.. I think there is a mispelling..   should be cmd.Parameters   not paramaters    
0
 

Author Comment

by:wilko100
ID: 24102667
yes just realised! lol, works now, excellent, cheers for that
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question