类型为' System.InvalidOperationException'的未处理异常.发生在System.Data.dll中 [英] An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll

查看:83
本文介绍了类型为' System.InvalidOperationException'的未处理异常.发生在System.Data.dll中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我得到了一个登录应用程序,当您单击登录时没有填写任何内容时,应该会给您一个错误,但是如果我单击它,说它是正确的,则我会< g class =" gr_ gr_82 gr-alert gr_spell gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id ="82". id ="82">再次单击登录按钮,并且此< g class ="gr_gr_106 gr-alert gr_gramm gr_run_anim标点多重替换" data-gr-id ="106". id ="106"< g class ="gr_gr_101 gr-alert gr_spell gr_disable_anim_appear ContextualSpelling ins-del multiReplace" data-gr-id ="101". id ="101"> happend./g> ..

System.Data.dll中发生了类型为'System.InvalidOperationException'的未处理的异常

附加信息:ConnectionString属性尚未初始化.

这是我所有的代码:)

使用系统;
使用System.Collections.Generic;
使用System.ComponentModel;
使用System.Data;
使用System.Drawing;
使用System.Linq;
使用System.Text;
使用System.Threading.Tasks;
使用System.Windows.Forms;
使用System.Data.OleDb;

命名空间Viper_Safe
{
     
   公共局部类< g class =" gr_ gr_157 gr-alert gr_gramm gr_run_anim Style multiReplace" data-gr-id ="157". id ="157">登录名:</g>表格
    {
       私有OleDbConnection连接=新的OleDbConnection();
       公开登录()
        {
            InitializeComponent();            
            connection.ConnectionString = @" Provider = Microsoft.ACE.OLEDB.12.0;数据源= C:\ Users \ SillyTen9 \ Documents \ UserDatabase.accdb;持续安全信息= False;;
        }

       私有void bunifuFlatButton2_Click(对象发送者,EventArgs e)
        {
            connection.Open();
            OleDbCommand命令=新的OleDbCommand();
            command.Connection =连接;
            command.CommandText ="从UserDatabase中选择*,其中Email ='" + bunifuMetroTextbox1.Text +''和密码='" + bunifuMetroTextbox2.Text +``''';
            OleDbDataReader reader = command.ExecuteReader();
            int count = 0;
            while(reader.Read())
            {
                count = count + 1;

            }
              if(count == 1)
            {
                MessageBox.Show(用户名和密码正确!");
                connection.Close();
                connection.Dispose();
           //    this.Hide();
             //  FORM f4 =新的FORM();
               //f4.ShowDialog();
            }
           如果(计数> 1)
            {
                MessageBox.Show(重复的用户名和密码");
            }
           如果(count == 0)
            {
                MessageBox.Show(用户名和密码不正确");
            }


            connection.Close();
        }
        
       私有void bunifuImageButton2_Click(对象发送者,EventArgs e)
        {
            Application.Exit();
        }

       私有void login_Load(对象发送者,EventArgs e)
        {
           试试
            {
                connection.Open();

                connection.Close();
            }
            catch(异常例外)
            {
                MessageBox.Show(无法连接?",错误" + ex);
            }
        }

       私人void bunifuCustomLabel4_Click(对象发送者,EventArgs e)
        {
            this.Hide();
            sign_up sf = new sign_up();
            sf.ShowDialog(); 
        }
    }
}


:)

解决方案

如果第一次单击后计数为1,则可能是您的表包含一行,其中包含空的电子邮件"和密码"值.删除此行(可以使用数据库工具或Visual Studio完成).或在尝试之前检查文本框的有效性 访问数据库.

第二个错误可能是由‘connection.Dispose()’引起的.删除它,因为您想重复使用该对象.

顺便说一句,与字符串连接相比,参数化查询更适合.


So I got a login application and when you click login when you have nothing filled in it should give you an error but if I click it once it goes and says it's correct and I <g class="gr_ gr_82 gr-alert gr_spell gr_run_anim ContextualSpelling ins-del multiReplace" data-gr-id="82" id="82">cliked</g> the login button again and this <g class="gr_ gr_106 gr-alert gr_gramm gr_run_anim Punctuation multiReplace" data-gr-id="106" id="106"><g class="gr_ gr_101 gr-alert gr_spell gr_disable_anim_appear ContextualSpelling ins-del multiReplace" data-gr-id="101" id="101">happend</g>..</g>

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll

Additional information: The ConnectionString property has not been initialized.

Here's all my code :)

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

namespace Viper_Safe
{
     
    public partial class <g class="gr_ gr_157 gr-alert gr_gramm gr_run_anim Style multiReplace" data-gr-id="157" id="157">login :</g> Form
    {
        private OleDbConnection connection = new OleDbConnection();
        public login()
        {
            InitializeComponent();            
            connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0; Data Source=C:\Users\SillyTen9\Documents\UserDatabase.accdb; Persist Security Info=False;";
        }

        private void bunifuFlatButton2_Click(object sender, EventArgs e)
        {
            connection.Open();
            OleDbCommand command = new OleDbCommand();
            command.Connection = connection;
            command.CommandText = "select * from UserDatabase where Email='" + bunifuMetroTextbox1.Text + "' and Password='" + bunifuMetroTextbox2.Text + "'";
            OleDbDataReader reader = command.ExecuteReader();
            int count = 0;
            while (reader.Read())
            {
                count = count + 1;

            }
             if (count == 1)
            {
                MessageBox.Show("Username and passwrd is correct!");
                connection.Close();
                connection.Dispose();
           //     this.Hide();
             //  FORM f4 = new FORM();
               // f4.ShowDialog();
            }
            if (count > 1)
            {
                MessageBox.Show("Duplicate Username and password");
            }
            if (count == 0)
            {
                MessageBox.Show("Username and password is not correct");
            }


            connection.Close();
        }
        
        private void bunifuImageButton2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void login_Load(object sender, EventArgs e)
        {
            try
            {
                connection.Open();

                connection.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show("Unable to connect?", "Error" + ex);
            }
        }

        private void bunifuCustomLabel4_Click(object sender, EventArgs e)
        {
            this.Hide();
            sign_up sf = new sign_up();
            sf.ShowDialog(); 
        }
    }
}


:)

解决方案

If count was 1 after the first click, then probably your table contains a row with empty Email and Password values. Remove this row (can be done with the database tools or Visual Studio). Or check the validity of textboxes before trying to access the database.

The second error is probably caused by ‘connection.Dispose()’. Remove it, since you want to reuse the object.

By the way, the parameterised queries are more suitable compared with string concatenations.


这篇关于类型为&amp;#39; System.InvalidOperationException&amp;#39;的未处理异常.发生在System.Data.dll中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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