Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Visual Studio 2012-参数在插入时没有默认值错误_C#_Sql_Ms Access - Fatal编程技术网

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

C# Visual Studio 2012-参数在插入时没有默认值错误,c#,sql,ms-access,C#,Sql,Ms Access,我有一个windows应用程序,它将数据插入Access数据库。但是,我遇到了以下错误: 参数@IMajor没有默认值 先谢谢你。 这是我的密码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threadi

我有一个windows应用程序,它将数据插入Access数据库。但是,我遇到了以下错误:

参数@IMajor没有默认值

先谢谢你。 这是我的密码:

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();


        }



    }
}   

具有
null
值的参数将被忽略,在您的情况下,
cbIntendedMajor.SelectedValue
似乎是
null
。如果此处应允许
null
,则必须将其替换为
DBNull.Value
,这将导致执行查询时参数具有预期的null值。例如:

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

ZipCode是列名。参数不必与列名匹配。