Click here to Skip to main content
15,886,720 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello Everyone
I'm Creating a Program, for my School project
now I am working at Login form of my Program.

I am getting Exception Error "Object reference not set to an instance of an object"
at
C#
ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;


C#
using System;
using System.Windows.Forms;
using System.Collections.Generic;
using System.ComponentModel;
using System.Net;
using System.Data.Sql;
using System.Configuration;
using System.Collections.Specialized;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;


namespace Proj.Data
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class Login : Window
    {
        public Login()
        {
            InitializeComponent();
            new SplashWindow().ShowDialog();
            
        }
       private void Window_Loaded(object sender, RoutedEventArgs e)
        {
           
        }

        
        private void Exit_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }


        private void Button1_Click(object sender, RoutedEventArgs e)
        {
            if ((textBox1.Text != "") && (passwordbox1.Password != ""))
            {
                string connstring = ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;
                SqlConnection con = new SqlConnection(connstring);
                string q = "select * from Login where UserId = @Username and Password = @Password ";
                SqlCommand cmd = new SqlCommand(q, con);
                cmd.Parameters.AddWithValue("@Username", this.textBox1.Text);
                cmd.Parameters.AddWithValue("@Password", this.passwordbox1.Password);
                con.Open();
                SqlDataReader dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    if (dr.HasRows == true)
                    {
                        System.Windows.MessageBox.Show("Login Successfully Done");
                    }
                }
                if (dr.HasRows == false)
                {
                    textBlock5.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    textBlock5.Visibility = System.Windows.Visibility.Visible;
                }
            }
        }
       
        private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
        {
            textBlock5.Visibility = System.Windows.Visibility.Hidden;
            if (Keyboard.GetKeyStates(Key.CapsLock) == KeyStates.Toggled)
            {
                textBlock4.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                textBlock4.Visibility = System.Windows.Visibility.Hidden;
            }
        }

        private void passwordbox1_PasswordChanged(object sender, RoutedEventArgs e)
        {
            textBlock5.Visibility = System.Windows.Visibility.Hidden;
            if (Keyboard.GetKeyStates(Key.CapsLock) == KeyStates.Toggled)
            {
                textBlock4.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                textBlock4.Visibility = System.Windows.Visibility.Hidden;
            }
            
        }

       

       
    }
}



I have a little knowledge about C# and Sql, always looking for Google for Tutorial and it keeps me puzzled.

or Please you can suggest a new Code for my Login
Posted
Updated 1-Aug-14 17:04pm
v2

1 solution

These might help you -
Video 1[^]
Video 2[^]
 
Share this answer
 
v2
Comments
codejet 6-Aug-14 17:20pm    
If you post your app.config file it might help

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900