0

我已经将IDENTITY数据库中的设置为yes,并且每当我运行应用程序时,键不会自动递增,给出关于不允许空值的错误

private void button8_Click(object sender, EventArgs e)
        {

            string fname = textBox1.Text;
            string lname = textBox2.Text;
            //int idnum = Convert.To(textBox3.Text);
            int mobnum = Convert.ToInt32(maskedTextBox1.Text);
            string email = textBox4.Text;

            int EdInst = comboBox1.SelectedIndex+1;
            string EdLev = comboBox3.SelectedText;
            string EdName = comboBox2.SelectedText;
            Boolean Valid = true;
            Valid = Validation(Valid);

            if (Valid == true)
            {
                IS2Team1_TriplexDBDataSet.ApplicantRow NewApplicantRow = iS2Team1_TriplexDBDataSet1.Applicant.NewApplicantRow();

                IS2Team1_TriplexDBDataSet.ApplicationRow NewApplicationRow = iS2Team1_TriplexDBDataSet1.Application.NewApplicationRow();

                //NewApplicantRow.Applicant_ID = ??;  // What do i do here for the ID to auto increment?
                NewApplicantRow.First_Name = fname;
                NewApplicantRow.Last_Name = lname;
                //NewApplicantRow.ID_Number =Convert.ToInt32(textBox3.Text);
                NewApplicantRow.Contact_Number = mobnum;
                NewApplicantRow.Email_Address = email;
                NewApplicantRow.University_ID = EdInst;

              //NewApplicationRow.Application_ID = ??; // What do i do here for the ID to auto increment?
                NewApplicationRow.Application_Status = "Recieved";
                NewApplicationRow.Application_Date = DateTime.Today;



                iS2Team1_TriplexDBDataSet1.Applicant.Rows.Add(NewApplicantRow);
                iS2Team1_TriplexDBDataSet1.Application.Rows.Add(NewApplicationRow);

                MessageBox.Show("Application Submitted", "Application Submitted");

                this.applicantTableAdapter.Update(this.iS2Team1_TriplexDBDataSet1);


                //Hide();
                //Form4 frmUpdate = new Form4();
                //frmUpdate.Show();

            }
                else if (Valid == false)
            {
                    MessageBox.Show("Required Information Missing");
                    textBox1.Focus();
            }
4

3 回答 3

0

我们可以看到你的视图是自动递增的吗?它应该设置为 +1 或标识 (1,1)

于 2013-09-23T09:39:46.023 回答
0

尝试在您的项目中打开 EDMX,单击该列并查看属性 (F4)。将 StoreGeneratedPattern 设置为 Identity。

于 2013-09-23T09:43:23.993 回答
0

您必须在表定义中设置以下值:

图片

示例:不适用于实体框架

如果要插入新行,只需忽略您的 Identity-Row 并使用以下命令获取自动生成的身份:@@Identity

希望这有帮助!

这是一个未经测试的示例:

public static void Test()
    {
        int cIdentity = 0;

        SqlConnection cConnection = new SqlConnection("...");
        cConnection.Open();

        SqlCommand cCommand = new SqlCommand("Insert INTO TestTable (Name) VALUES ('Test'); "+
            "SELECT @@IDENTITY AS Ident", cConnection);

        IDataReader cReader = null;

        try
        {
            cReader = cCommand.ExecuteReader(CommandBehavior.CloseConnection);
            if (cReader.Read())
            {
                cIdentity = cReader.GetInt32(0);
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex);
            if (cReader != null)
            {
                cReader.Close();
            }
        }
        finally
        {
            if (cConnection != null)
            {
                cConnection.Close();
            }
        }
    }
于 2013-09-23T09:43:43.693 回答