Hey,
I'm new to C# and im trying to make a login form at the start of an application that will connect to an SQL database.
The error im getting is that it cannot make a connection to ANY database at all.
cn.Open() is returning the connection failure
I used the example on this site :)
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; using System.Data.SqlClient;
namespace Kronos { public partial class Form1 : Form { public Form1() { InitializeComponent(); }
private void LoginButton1_Click(object sender, EventArgs e) { string username = UserBox1.Text; string password = PassBox1.Text;
if (ValidateUserNamePassword(username, password)) { MessageBox.Show("Succsess!", "Authentication Complete"); } else { MessageBox.Show("Invalid user name or password", "Invalid Login"); return; } }
public bool ValidateUserNamePassword(string _username, string _password) { string connectionString = "Server=localhost;Database=kronos;User ID=root;Password=;";
using (SqlConnection cn = new SqlConnection(connectionString)) { SqlCommand cmd = new SqlCommand(); cmd.Connection = cn; cmd.CommandType = CommandType.StoredProcedure; cmd.CommandText = "tsp_GetUserNameAndPassword";
SqlParameterCollection sqlParams = cmd.Parameters; sqlParams.AddWithValue("@UserName", _username); sqlParams.AddWithValue("@Password", _password);
cn.Open(); SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.SingleRow); if (dr.Read()) { // this will return true if a row matching the username and password is found. // this means that the user's input is valid return true; } else { return false; }
dr.Close(); cn.Close(); } } } }
|
Does anyone know why?
Suthish NairPosted Oct 25, 2010, 3:57 AM
Suthish NairPosted Oct 20, 2010, 6:59 AM
rest of the codes are same from your given sample.
Josh DoustPosted Oct 20, 2010, 5:44 AM
Can you please explain how i would implement this code into my current project?
Thanks :)
Suthish NairPosted Oct 19, 2010, 4:28 PM