异常:System.InvalidOperationException试图验证登录信息 [英] Exception: System.InvalidOperationException Trying to validate a login information

查看:67
本文介绍了异常:System.InvalidOperationException试图验证登录信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个登录表单.但是数据库有问题.我创建了一个Windows窗体,其中包含用户名和密码以及一个登录按钮.但是我认为声明:

I am trying to create a login Form. But Having Problems with database. I have created a windows form which consists of a user name and password, and a login button. But I think statement :

DataAdapterObject.Fill(DataTableObject) 

有一些错误.我正在使用Visual Studio Profesional 2013 Update 4和Sql Server 2014 Enterprise Editon.

has some error. I am using Visual Studio Profesional 2013 Update 4 and Sql Server 2014 Enterprise Editon.

代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Society_Accounting_Software
{
 public partial class LoginScreen : Form
{
    SqlConnection databaseConnect = new SqlConnection();


    public LoginScreen()
    {
        SqlConnection databaseConnect = new SqlConnection();
        databaseConnect.ConnectionString = "Data Source=GAURAV-PC;Initial Catalog=SocietyAccountingDatabase;Integrated Security=True";
        InitializeComponent();

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {


        SqlConnection databaseConnect = new SqlConnection("Data Source=GAURAV-PC;Initial Catalog=SocietyAccountingDatabase;Integrated Security=True");

        databaseConnect.Open();

    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged_1(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        string queryString = "select UserId,UserPassword from UserAccounts where UserId='gaurav' AND  UserPassword='test123'";
        SqlConnection databaseConnect = new SqlConnection();
        databaseConnect.ConnectionString = "Data Source=GAURAV-PC;Initial Catalog=SocietyAccountingDatabase;Integrated Security=True";
        databaseConnect.Open();
        string userName = UserNameTextBox.Text;
        string Password = PasswordTextBox.Text;
        SqlCommand SqlCommandObject = new SqlCommand("select UserId,UserPassword from UserAccounts where UserId='"+userName+"' AND  UserPassword='"+Password+"'");
        SqlDataAdapter DataAdapterObject = new SqlDataAdapter(SqlCommandObject);
        DataTable DataTableObject = new DataTable();
        DataAdapterObject.Fill(DataTableObject);



        if (DataTableObject.Rows.Count > 0)
        {
            MessageBox.Show("Login Sucessful");
            AdminConsoleForm AdminConsole= new AdminConsoleForm();
            this.Hide();
            AdminConsole.Show();

        }
        else
        {
            MessageBox.Show("Invalid Login Name And Password Please Try Again!");

        }

        databaseConnect.Close();




        //AdminConsoleForm AdminConsole= new AdminConsoleForm();
        //this.Hide();
        //AdminConsole.Show();





    }
}
}

有人可以帮忙吗?

推荐答案

登录表单的简单代码:

namespace Society_Accounting_Software
{
public partial class LoginScreen : Form
{
    SqlConnection databaseConnect = new SqlConnection();


    public LoginScreen()
    {
        SqlConnection databaseConnect = new SqlConnection();
        databaseConnect.ConnectionString = "Data Source=GAURAV-PC;Initial Catalog=SocietyAccountingDatabase;Integrated Security=True";
        InitializeComponent();

    }

    private void label1_Click(object sender, EventArgs e)
    {

    }

    private void Form1_Load(object sender, EventArgs e)
    {


    }
    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged_1(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)

    {

        try

        {

            if (!(UserNameTextBox.Text == string.Empty))

            {

                if (!(PasswordTextBox.Text== string.Empty))

                {

                   //this represent your connection to database
                    String str = "Data Source=GAURAV-PC;Initial Catalog=SocietyAccountingDatabase;Integrated Security=True";

                    String query = "select * from UserAccounts where userid = '"+UserNameTextBox.Text+"'and password = '"+this.PasswordTextBox.Text+"'";

                    SqlConnection con = new SqlConnection(str);

                    SqlCommand cmd = new SqlCommand(query, con);

                    SqlDataReader dbr;

                    con.Open();

                    dbr = cmd.ExecuteReader();

                    int count = 0;

                    while (dbr.Read())

                    {

                        count = count + 1;

                    }

                    con.Close();

                    if (count == 1)

                    {
                        AdminConsoleForm objmain = new AdminConsoleForm();
                        objmain.Show(); //after login Redirect to second window  
                        this.Hide();//after login hide the  Login window   

                    }

                    else if (count > 1)

                    {

                        MessageBox.Show("Duplicate username and password", "login page");

                    }

                    else

                    {

                        MessageBox.Show(" Username and Password Incorrect", "login page");

                    }

                }

                else

                {

                    MessageBox.Show(" Password Empty", "login page");

                }


            }
            else

            {

                MessageBox.Show(" Username Empty", "login page");

            }




        }

        catch (Exception es)

        {

            MessageBox.Show(es.Message);
        }

    }
}

我使代码非常简单.如果需要安全性,可以加盐.

I have kept the code really simple. You can do salting if you want security.

这篇关于异常:System.InvalidOperationException试图验证登录信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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