简体   繁体   English

Visual Studio 2012-参数在插入时没有默认值错误

[英]Visual Studio 2012 - parameter has no default value error on insert

I have a windows application that data is being inserted into an Access database. 我有一个Windows应用程序,该数据正在插入到Access数据库中。 However, I am encountering the following error: 但是,我遇到以下错误:

Parameter @IMajor has no default value. 参数@IMajor没有默认值。

Thank you in advance. 先感谢您。 Here is my code: 这是我的代码:

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.OleDb;


namespace InterestCardNew
{
    public partial class Form1 : Form
    {
        public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        // TODO: This line of code loads data into the 'interestDataSet.InterestCard' table. You can move, or remove it, as needed.
        this.interestCardTableAdapter.Fill(this.interestDataSet.InterestCard);
        lblCurrentDate.Text = DateTime.Now.ToShortDateString();
    }


    private void button1_Click(object sender, EventArgs e)
    {
        string conString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data     Source=C:\Temp\Interest.accdb";
        OleDbConnection con = new OleDbConnection(conString);
        OleDbCommand cmd = con.CreateCommand();
        string text = "INSERT INTO InterestCard (FirstName, LastName, StreetAddress, City, State, ZipCode, Phone, Email, DOB, Gender, HighSchool, GraduationYear, PlannedTerm, IntendedMajor) VALUES (@FName, @LName, @SAddress, @City, @State, @Zip, @Phone, @Email, @DOB, @Gender, @HSchool, @GradYear, @PTerm, @IMajor)";
        cmd.CommandText = text;

            con.Open();
            cmd.Parameters.AddWithValue("@FName", txtFirstName.Text);
            cmd.Parameters.AddWithValue("@LName", txtLastName.Text);
            cmd.Parameters.AddWithValue("@SAddress", txtAddress.Text);
            cmd.Parameters.AddWithValue("@City", txtCity.Text);
            cmd.Parameters.AddWithValue("@State", txtState.Text);
            cmd.Parameters.AddWithValue("@Zip", txtZipCode.Text);
            cmd.Parameters.AddWithValue("@Phone", txtPhone.Text);
            cmd.Parameters.AddWithValue("@Email", txtEmail.Text);
            cmd.Parameters.AddWithValue("@DOB", dtpDOB.Value);
            cmd.Parameters.AddWithValue("@Gender", rbFemale.Checked);
            cmd.Parameters.AddWithValue("@HSchool", txtHighSchool.Text);
            cmd.Parameters.AddWithValue("@GradYear", txtGraduationYear.Text);
            cmd.Parameters.AddWithValue("@PTerm", txtTermofEnrollment.Text);
            cmd.Parameters.AddWithValue("@IMajor", cbIntendedMajor.SelectedValue);
            cmd.ExecuteNonQuery();


        }



    }
}   

Parameters that have a null value are ignored, and in your case, cbIntendedMajor.SelectedValue would seem to be null . 具有null值的参数将被忽略,在您的情况下, cbIntendedMajor.SelectedValue似乎为null If null should be allowed here, then you must replace it with DBNull.Value , which will result in the parameter having the expected null value when the query is executed. 如果此处允许使用null ,则必须将其替换为DBNull.Value ,这将导致执行查询时参数具有预期的null值。 Example: 例:

cmd.Parameters.AddWithValue("@IMajor", (object)cbIntendedMajor.SelectedValue ?? DBNull.Value);

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

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