如何修复参数异常系统中的错误

时间:2019-02-22 13:50:15

标签: c# visual-studio error-handling arguments

    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.SqlClient;




namespace WindowsFormsApp1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            pwnew.PasswordChar = '*';
            pwtxt.PasswordChar = '*';

            signup.Visible = false;

        }

        private void signupbtn_Click(object sender, EventArgs e)
        {
            signup.Visible = true;

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (unnew.Text != null && pwnew.Text != null)
            {
                try
                {
                    Connect obj = new Connect();
                    obj.conn.ConnectionString = obj.locate;
                    obj.conn.Open();
                    string insertuser = "insert into userTable('" + unnew.Text + "', '" + pwnew.Text + "')";
                    obj.cmd.Connection = obj.conn;
                    obj.cmd.CommandText = insertuser;
                    obj.conn.Close();
                    MessageBox.Show("Signup has been completed");
                    signup.Visible = false;

                }
                catch (Exception ex)
                {
                    MessageBox.Show("Error" + ex);


                }

            }
            else
            {
                MessageBox.Show("Error");
            }

        }

        private void loginbtn_Click(object sender, EventArgs e)
        {
            if (untxt.Text != null && pwtxt.Text != null)
            {
                try
                {
                    Connect obj = new Connect();
                    obj.conn.ConnectionString = obj.locate;

                    obj.conn.Open();
                    SqlDataAdapter adapter = new SqlDataAdapter("SELECT COUNT (*) FROM userTable where username = '" + untxt.Text + "' and password '" + pwtxt.Text + "'", obj.conn);
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    if (dt.Rows[0][0].ToString() == "1")
                    {
                        Form2 meLoad = new Form2();
                        meLoad.Visible = true;
                        this.Hide();
                        MessageBox.Show("Success");
                    }
                    else
                    {
                        MessageBox.Show("Username or Password is incorrect");
                    }
                    obj.conn.Close();
                    MessageBox.Show("Successfully Login");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);

                }
            }

            else
            {
                MessageBox.Show("No empty fields are allowed");
            }
            {

            }
        }
    }
}

嗨,我是c#的新手,每当我单击“注册”时,都会出现此错误。它告诉我检查第44行

try
                {
                    Connect obj = new Connect();
                    obj.conn.ConnectionString = obj.locate;
                    obj.conn.Open();
                    string insertuser = "insert into userTable('" + unnew.Text + "', '" + pwnew.Text + "')";
                    obj.cmd.Connection = obj.conn;
                    obj.cmd.CommandText = insertuser;
                    obj.conn.Close();
                    MessageBox.Show("Signup has been completed");
                    signup.Visible = false;

                }

我是一名学生,并且没有足够的编程背景,我希望能得到别人的帮助。我真的很想学习这种编程语言,并且目前遇到了麻烦。enter image description here非常感谢。

我将其用于连接

 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace WindowsFormsApp1
{
    class Connect
    {
        public SqlConnection conn = new SqlConnection();
        public SqlCommand cmd = new SqlCommand();
        public string locate = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='C:\Users\hp\source\repos\WindowsFormsApp1\WindowsFormsApp1\UserDB.mdf;'Integrated Security=True";
    }
}

1 个答案:

答案 0 :(得分:1)

连接数据库似乎有些中断。

请仔细检查您的连接字符串,如果您需要连接字符串的帮助,则可以访问https://www.connectionstrings.com/

本教程可能对您使用c#连接数据库很有帮助。

https://www.guru99.com/c-sharp-access-database.html