Skip to content

SQL Login Form - Please Help

Josh Doust

Asked Oct 19, 2010, 11:20 AM EDT4 answers
Share
LinkedInX
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 :)

C#
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?

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • Suthish Nair
    0
    try..
    PowerShell
    using (SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["conString"].ToString()))
    {
     using (SqlCommand cmd = new SqlCommand())
     {
      cmd.Connection = con;
      con.Open();
      
      //Add Parameteres here
      
      SqlDataReader reader = cmd.ExecuteReader();
      if (reader.HasRows)
      {
       while (reader.Read())
       {
        Console.WriteLine("{0}\t{1}", reader.GetInt32(0), reader.GetString(1));
       }
      }
      else
      {
       Console.WriteLine("No rows found.");
      }
      reader.Close();
     }
    }
    0
  • 0
    Thanks mate but im REALLY new to C# and coding all together.
    Can you please explain how i would implement this code into my current project?
    Thanks :)
    0
  • Suthish Nair
    0
    remove the line code Console.WriteLine and apply your logic.
    rest of the codes are same from your given sample.
    0
  • Suthish Nair
    0
    Please accept the post as answer, if your query is resolved. Thank You.
    0