【问题标题】:How to fix Error in argument exception system如何修复参数异常系统中的错误
【发布时间】:2019-07-16 15:12:52
【问题描述】:
    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";
    }
}

【问题讨论】:

  • 堆栈跟踪表明数据库连接无法解析连接字符串。您在此处设置的:obj.conn.ConnectionString = obj.locate;您使用的是什么连接字符串?
  • 在尝试使用数据库之前先按照 C# 的简单教程进行尝试
  • 我确实遵循了这个教程,我只是不明白;我们输入了相同的代码,但是如果教程没有,为什么我会收到错误

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


【解决方案1】:

连接您的数据库似乎有些问题。

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

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

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

【讨论】:

    猜你喜欢
    • 2022-12-04
    • 1970-01-01
    • 1970-01-01
    • 2019-11-13
    • 2023-03-20
    • 2019-09-23
    • 2020-08-13
    • 1970-01-01
    • 2011-09-04
    相关资源
    最近更新 更多