0

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();
   }
      }


    }
  }
2
  • 2
    Connecting with sa is usually a bad idea. Commented Apr 25, 2011 at 13:39
  • this is the error i am getting.Object reference not set to an instance of an object. at line cn.Open(); Commented Apr 25, 2011 at 13:46

2 Answers 2

2

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"

Sign up to request clarification or add additional context in comments.

Comments

2

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.