INSERT语句中的列数多于VALUES子句中的值数

t9eec4r0  于 2022-10-03  发布在  其他
关注(0)|答案(1)|浏览(158)

我也不知道如何将组合框和性别单选按钮的值添加到数据库中。这基本上是为了让注册表单将其值插入数据库。我还需要通过选择组合框值在表单上显示数据库的值。

private void Btn_register_Click(object sender, EventArgs e)
{
    //int regNo = Cbox_regNo.Text; //i need to get the values from the combobox
    string fName = Tbox_fName.Text;
    string lName = Tbox_lName.Text;
    string dob = dtp_dob.Text;
    string address = Tbox_address.Text;
    string email = Tbox_email.Text;
    string mPhone = Tbox_mPhone.Text;
    string hPhone = Tbox_hPhone.Text;
    string pName = Tbox_parentName.Text;
    string nic = Tbox_nic.Text;
    string cNumber = Tbox_cntctNumber.Text;

    string connString = "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename="C:\Users\abhin\Desktop\Final Project\Visual Studio\Final Project\Final Project\Student.mdf";Integrated Security=True";
    string Query = "insert into Registrations (regNo, firstName, lastName, dateOfBirth, gender, address, email, mobilePhone, homePhone, parentName, nic, contactNo) values('"+this.Cbox_regNo.Text+"','" + this.Tbox_fName.Text + "','" + this.Tbox_lName.Text + "','" + this.dtp_dob.Value + "','" + this.Tbox_address.Text + "','" + this.Tbox_email.Text + "','" + this.Tbox_mPhone.Text + "','" + this.Tbox_hPhone.Text + "','" + this.Tbox_parentName.Text + "','" + this.Tbox_nic.Text + "','" + this.Tbox_cntctNumber.Text + "') ;";
    SqlConnection conn = new SqlConnection(connString);
    SqlCommand cmdDB  = new SqlCommand(Query, conn);
    SqlDataReader myReader;
    try
    {
        conn.Open();
        myReader=cmdDB.ExecuteReader();
        MessageBox.Show("Record Added Succesfully", "Register Student", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
        while (myReader.Read())
        {

        }
    }
    catch(Exception ex)
    {
        MessageBox.Show(ex.Message);
        conn.Close();
    }
}
tvmytwxo

tvmytwxo1#

在声明“Query”变量时,您使用的是12列(Regno,FirstName…)和11个价值。您还需要一个值

相关问题