wilko100
asked on
Creating a c# login form using ado
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
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");
}
}
}
}
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.AddWithValu e("@UserNa me",txtUse rName.Text );
cmd.Parameters.AddWithValu e("@Passwo rd",txtPas sword.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();
}
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandText = "SELECT UserName FROM SystemUsers WHERE UserName = @UserName AND Password = @Password");
cmd.Paramaters.AddWithValu
cmd.Parameters.AddWithValu
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();
}
ASKER
when i apply jjardine fix i get this error..
Error 1 'System.Data.SqlClient.Sql Command' does not contain a definition for 'Paramaters' and no extension method 'Paramaters' accepting a first argument of type 'System.Data.SqlClient.Sql Command' 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?
Error 1 'System.Data.SqlClient.Sql
complaining about cmd.Paramaters is that anything to do with a particular 'namespace' missing?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
yes just realised! lol, works now, excellent, cheers for that
Get the out put flag check for it in the if condition and act accordingly.
Open in new window