Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何修复参数异常系统中的错误_C#_Visual Studio_Error Handling_Arguments - Fatal编程技术网

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

C# 如何修复参数异常系统中的错误,c#,visual-studio,error-handling,arguments,C#,Visual Studio,Error Handling,Arguments,嗨,我是c语言的新手,每当我点击“注册”时都会出现这个错误。它告诉我检查第44行,它是 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threa

嗨,我是c语言的新手,每当我点击“注册”时都会出现这个错误。它告诉我检查第44行,它是

    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");
            }
            {

            }
        }
    }
}
我是一名学生,没有足够的编程背景,如果有人能帮助我,我将不胜感激。我真的很想学习这种编程语言,但我现在遇到了麻烦。非常感谢你

我用这个连接

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;

                }

连接数据库时似乎出现了问题

请仔细检查您的连接字符串,如果您需要有关连接字符串的帮助,请访问

本教程可能有助于您使用c连接数据库


堆栈跟踪意味着数据库连接无法解析连接字符串。在这里设置:obj.conn.ConnectionString=obj.locate;你用的是什么连接字符串?试着按照C上的简单教程来做,在尝试使用数据库之前,我确实遵循了这方面的教程,我就是不明白;我们输入了相同的代码,但如果教程没有输入,为什么我会出错
 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";
    }
}