簡體   English   中英

當我在 C# 中提交表單時程序崩潰

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

當我在我的 C# 應用程序中單擊表單的提交按鈕時,它會崩潰。 該程序假設將所有數據帶到我的數據庫中,這是在下面的 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();
        }

這是我得到的錯誤錯誤

你能像這樣改變你的代碼嗎,我假設你所有的數據類型都是字符串,你可以從這里選擇合適的

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 
}

您的 sql 查詢字符串在“值”附近有問題。

string sqlStatement = "INSERT INTO 

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

你錯過了 '(' 在值之后。我建議遵循以下語法

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