bharathi
bharathi

Reputation: 6271

Null reference on SQL connection object

I am new to C#. I have created the login screen.I this one am not able to check the username and password.This is my code.Can anyone help me please.Thanks in advance.Please don't hesitate to copy the code.

 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;
 using System.Data.Sql;

    namespace Voting_Editor_Tool
    {
       public partial class Form1 : Form
     {
         SqlConnection cn;
         public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {


    }

    private void button2_Click(object sender, EventArgs e)
    {
        Application.Exit();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string username = txtusername.Text;
        string password = txtpassword.Text;

        if (ValidateUserNamePassword(username, password))
        {
            // move to next form or do whatever you need to do after a successfull login
        }
        else
        {
            MessageBox.Show("Invalid user name or password", "Invalid Login");
            return;
        }
    }
    public bool ValidateUserNamePassword(string _username, string _password)
    {
                  //  string connectionString = "Data Source=[servername];Initial                           Catalog=[databaseName];User ID=[Admin Login];Password=[Admin Password];";

   using (SqlConnection cn= new SqlConnection(@"User ID=sa;Password=password123;Initial Catalog=dish_tv;Data Source=ENMEDIA-CCDDFE5\ENMEDIA"));
   {
      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();
   }
      }


    }
  }

Upvotes: 0

Views: 2784

Answers (2)

Priyank
Priyank

Reputation: 10623

Remove your using clause and put that piece of code into Try.. catch block. Catch the exception object and read its stacktrace. Check the connection string carefully for any typo mistakes. This should give you much more details to debug than generic error like "Object reference not set to an instance of an object"

Upvotes: 2

Leons
Leons

Reputation: 2674

You have a semi-colon at the end of your using statement, therefore terminating the using. Remove the semi-colon and it will work.

Upvotes: 2

Related Questions