简体   繁体   English

当我在 C# 中提交表单时程序崩溃

[英]program crash when i submit the form in c#

when ever i click submit button of form in my c# application it crashes.当我在我的 C# 应用程序中单击表单的提交按钮时,它会崩溃。 the program suppose to take all the data to my database which is made in microsoft sql server below is the code该程序假设将所有数据带到我的数据库中,这是在下面的 microsoft sql server 中制作的代码

string connection = "Data Source=DESKTOP-EON545D;Initial Catalog=TMS_Database;Integrated Security=True";
public LoginFrame()
        {
           
            InitializeComponent();
            con = new SqlConnection(connection);

        }


 private void button2_Click(object sender, EventArgs e)
    {
        if (checkBox1.Checked)//if terms and conditions are accepted
        {
            AddNewTenant();


        }
    }

public void AddNewTenant()
        {
            string username = textBox1.Text.ToString();
            string password = textBox2.Text.ToString();
            string email = textBox7.Text.ToString();
            string firstName = textBox5.Text.ToString();
            string lastName = textBox6.Text.ToString();
            string address = richTextBox1.Text.ToString();
            int phone = int.Parse(textBox8.Text.ToString());
            string CNIC = textBox9.Text.ToString();
            string appartmentType = radioButton1.Text.ToString();

            string sqlStatement = "INSERT INTO tenant(username,password,firstname,lastname,email,current_address,phone,appartmentType)values" + "'" + username + "'," + "'" + password + "'," + "'" + firstName + "'," + "'" + lastName + "'," + "'" + email + "'," + "'" + address + "'," + "'" + phone + "'," + "'" + appartmentType + "')";
            //Console.WriteLine(sqlStatement);
            SqlCommand cmd = new SqlCommand(sqlStatement, con);
            con.Open();
            int rowsinsert = cmd.ExecuteNonQuery();
            con.Close();
        }

and this is the error i'm getting这是我得到的错误错误

Can you change your code like this, I have assumed all your data types to be string you can pick the appropriate ones from here你能像这样改变你的代码吗,我假设你所有的数据类型都是字符串,你可以从这里选择合适的

using(var cmd= SqlCommand("INSERT INTO tenant(username,password,firstname,lastname,email,current_address,phone,appartmentType) values (@username,@password,@firstname,@email, @current_address,@phone,@appartmentType)", con)
{
    cmd.Parameters.Add("@username", DbType.String).Value = username ;
    cmd.Parameters.Add("@password ", DbType.String).Value = password;
    cmd.Parameters.Add("@firstname", DbType.String).Value = firstname;
    cmd.Parameters.Add("@lastname", DbType.String).Value = lastname;
    cmd.Parameters.Add("@email", DbType.String).Value = email;
    cmd.Parameters.Add("@current_address", DbType.String).Value = current_address;
    cmd.Parameters.Add("@phone", DbType.String).Value = phone;
    cmd.Parameters.Add("@appartmentType", DbType.String).Value = appartmentType;

    con.Open();
    int rowsInsert = cmd.ExecuteNonQuery();
    // Followed by your code 
}

your sql query string has problem near 'values'.您的 sql 查询字符串在“值”附近有问题。

string sqlStatement = "INSERT INTO 

tenant(username, password, firstname, lastname, email, current_address, phone, 
appartmentType) values(" + "'" + username + "'," + "'" + password + "'," + "'" + 
firstName + "'," + "'" + lastName + "'," + "'" + email + "'," + "'" + address + "'," + 
"'" + phone + "'," + "'" + appartmentType + "')";

you missed '(' after values. and I suggest following syntax你错过了 '(' 在值之后。我建议遵循以下语法

string sqlStatement = "INSERT INTO tenant(username, password, firstname, 
lastname, email, current_address, phone, appartmentType) values(@username, 
@password, @firstName, @lastName, @email, @address, @phone,     
@appartmentType)";

SQLiteCommand command = new SQLiteCommand(con)
command.Text = sqlStatement;
command.CommandType = SQLiteCommandType
command.CommandType = CommandType.Text;
command.Parameters.Add(new SQLiteParameter("@username", username));
command.Parameters.Add(new SQLiteParameter("@password", password));
command.Parameters.Add(new SQLiteParameter("@firstname", firstname));
command.Parameters.Add(new SQLiteParameter("@lastname", lastname));
command.Parameters.Add(new SQLiteParameter("@email", email));
command.Parameters.Add(new SQLiteParameter("@current_address", current_address));
command.Parameters.Add(new SQLiteParameter("@phone", phone));
command.Parameters.Add(new SQLiteParameter("@appartmentType", appartmentType));

command.ExecuteNoneQuery();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM