我得到的Object引用未设置为对象的实例。的WinForms

时间:2017-10-01 06:17:59

标签: c# winforms registration

每当我按下Windows窗体中的记录按钮时,我都会收到未在Windows窗体中设置对象实例的Object引用。我不知道什么是错的我只是c#的新手。谁能帮我?我在这里有一长串代码,希望有人帮助我。因为我一直在做这个代码,所以我坚持了两个星期。

private void btnNew_Click(object sender, EventArgs e)
{

    string sql = "INSERT INTO applicants(applicant_num, applicant_lname, applicant_fname, applicant_mname, birth_place, highschool, hs_address, hs_year_graduated, seniorhighschool, address, address_barangay, address_city_municipality, nationality, father, occupation, contact, mother, occupation1, contact1, birth_month, birth_date, birth_year, gender, school_graduate, choice1, choice2, religion, exam_room, exam_time, exam_date_month, exam_date_day, exam_date_year) VALUES (@applicant_num, @applicant_lname, @applicant_fname, @applicant_mname, @birth_place, @highschool, @hs_address, @hs_year_graduated, @seniorhighschool, @address, @address_barangay, @address_city_municipality, @nationality, @father, @occupation, @contact, @mother, @occupation1, @contact1, @birth_month, @birth_date, @birth_year, @gender, @school_graduate, @choice1, @choice2, @religion, @exam_room, @exam_time, @exam_date_month, @exam_date_day, @exam_date_year)";
    cmd = new MySqlCommand(sql, con);

    try
    {
        con.Open();
        cmd = new MySqlCommand(sql, con);
        cmd.Parameters.AddWithValue(@"applicant_num", lblApplicantNum.Text);
        cmd.Parameters.AddWithValue(@"applicant_lname", txtLname.Text);
        cmd.Parameters.AddWithValue(@"applicant_fname", txtFname.Text);
        cmd.Parameters.AddWithValue(@"applicant_mname", txtMname.Text);
        cmd.Parameters.AddWithValue(@"birth_place", txtPbirth.Text);
        cmd.Parameters.AddWithValue(@"highschool", txtJuniorHigh.Text);
        cmd.Parameters.AddWithValue(@"hs_address", txtJHaddress.Text);
        cmd.Parameters.AddWithValue(@"hs_year_graduated", txtJHYearGrad.Text);
        cmd.Parameters.AddWithValue(@"seniorhighschool", txtSeniorHigh.Text);
        cmd.Parameters.AddWithValue(@"shs_address", txtSHaddress.Text);
        cmd.Parameters.AddWithValue(@"shs_year_graduated", txtSHSYearGrad.Text);
        cmd.Parameters.AddWithValue(@"address", txtAddress.Text);
        cmd.Parameters.AddWithValue(@"address_barangay", txtBarangay.Text);
        cmd.Parameters.AddWithValue(@"address_district", cmbDistrict.SelectedItem.ToString());
        cmd.Parameters.AddWithValue(@"address_city_municipality", txtCity.Text);
        cmd.Parameters.AddWithValue(@"nationality", txtNationality.Text);
        cmd.Parameters.AddWithValue(@"father", txtParent1.Text);
        cmd.Parameters.AddWithValue(@"occupation", txtOccupation1.Text);
        cmd.Parameters.AddWithValue(@"contact", txtContact1.Text);
        cmd.Parameters.AddWithValue(@"mother", txtParent2.Text);
        cmd.Parameters.AddWithValue(@"occupation1", txtOccupation2.Text);
        cmd.Parameters.AddWithValue(@"contact1", txtContact2.Text);
        cmd.Parameters.AddWithValue(@"birth_month", cmbMonth.SelectedItem.ToString());
        cmd.Parameters.AddWithValue(@"birth_date", cmbDay.SelectedItem.ToString());
        cmd.Parameters.AddWithValue(@"birth_year", cmbYear.SelectedItem.ToString());
        cmd.Parameters.AddWithValue(@"gender", cmbGender.SelectedItem.ToString());
        cmd.Parameters.AddWithValue(@"school_graduate", cmbSchools.SelectedItem.ToString());
        cmd.Parameters.AddWithValue(@"choice1", cmbResident.SelectedItem.ToString());
        cmd.Parameters.AddWithValue(@"choice2", cmbVoter.SelectedItem.ToString());
        cmd.Parameters.AddWithValue(@"religion", cmbReligion.SelectedItem.ToString());
        cmd.Parameters.AddWithValue(@"exam_room", cmbRooms.ToString());
        cmd.Parameters.AddWithValue(@"exam_time", cmbETime.SelectedItem.ToString());
        cmd.Parameters.AddWithValue(@"exam_date_month", cmbEMonth.SelectedItem.ToString());
        cmd.Parameters.AddWithValue(@"exam_date_day", cmbEDay.SelectedItem.ToString());
        cmd.Parameters.AddWithValue(@"exam_date_year", cmbEYear.SelectedItem.ToString());
        if(cmd.ExecuteNonQuery()>0)
        {
            MessageBox.Show("Success!", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }
        con.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, "Mesasage", MessageBoxButtons.OK, MessageBoxIcon.Error);
        con.Close();
    }
}

0 个答案:

没有答案