如何编码登录表单 [英] How to Code Log-in form

查看:66
本文介绍了如何编码登录表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Hello Everyone

我正在为我的学校项目创建一个程序

现在我在我的程序的登录表单中工作。



我收到异常错误对象引用没有设置为对象的实例

at

 ConfigurationManager.ConnectionStrings [  ConnData]。ConnectionString; 





 使用系统; 
使用 System.Windows.Forms;
使用 System.Collections.Generic;
使用 System.ComponentModel;
使用 System.Net;
使用 System.Data.Sql;
使用 System.Configuration;
使用 System.Collections.Specialized;
使用 System.Linq;
使用 System.Text;
使用 System.Windows;
使用 System.Windows.Controls;
使用 System.Windows.Data;
使用 System.Windows.Documents;
使用 System.Windows.Input;
使用 System.Windows.Media;
使用 System.Windows.Media.Imaging;
使用 System.Windows.Navigation;
使用 System.Windows.Shapes;
使用 System.Data;
使用 System.Data.SqlClient;
使用 System.Text.RegularExpressions;
使用 System.Runtime.InteropServices;


命名空间 Proj.Data
{
/ // < 摘要 >
/// 交互逻辑for MainWindow.xaml
/// < ; / summary >
public partial class 登录:Window
{
public 登录()
{
InitializeComponent();
new SplashWindow()。ShowDialog();

}
私有 void Window_Loaded( object sender,RoutedEventArgs e)
{

}


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


私人 void Button1_Click(< span class =code-keyword> 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 UserId = @Username和Password = @Password;
SqlCommand cmd = new SqlCommand(q,con);
cmd.Parameters.AddWithValue( @ Username .textBox1.Text);
cmd.Parameters.AddWithValue( @ Password .passwordbox1.Password);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
if (dr.HasRows == true
{
System.Windows.MessageBox.Show( 登录成功完成);
}
}
如果(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;
}

}




}
}







我对C#和Sql有一点了解,总是在寻找Google for Tutorial,它让我感到困惑。



请你为我的登录建议一个新的代码

解决方案

这些可能对你有帮助 -

视频1 [ ^ ]

视频2 [ ^ ]

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

ConfigurationManager.ConnectionStrings["ConnData"].ConnectionString;



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

解决方案

These might help you -
Video 1[^]
Video 2[^]


这篇关于如何编码登录表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆