关于INSERT过程的SqlException未处理 - C#SQL

时间:2021-11-12 02:06:14

I have a form like this: 关于INSERT过程的SqlException未处理 -  C#SQL

我有一个这样的表格:

After filling the form, when I click the add button (which is trying to insert the data to my database), I got a SqlException error.

填写表单后,当我单击添加按钮(试图将数据插入我的数据库)时,我收到了SqlException错误。

关于INSERT过程的SqlException未处理 -  C#SQL

This is my code in C#

这是我在C#中的代码

SqlParameter NoIDPenghuniNew, NamaPenghuniNew, NoTelpPenghuniNew, JKPenghuniNew, AlamatPenghuniNew, NoKamarPenghuniNew, IDCabangK, NoKamarPenghuniNew2, IDCabangK2;

                NoIDPenghuniNew = new SqlParameter();
                NamaPenghuniNew = new SqlParameter();
                NoTelpPenghuniNew = new SqlParameter();
                JKPenghuniNew = new SqlParameter();
                AlamatPenghuniNew = new SqlParameter();
                NoKamarPenghuniNew = new SqlParameter();
                IDCabangK = new SqlParameter();
                NoKamarPenghuniNew2 = new SqlParameter();
                IDCabangK2 = new SqlParameter();


                //string IDCabangX = "";

                SqlCommand com, com2;

                SqlConnection con = new SqlConnection(strCon);

                com = new SqlCommand();
                com.Connection = con;

                com2 = new SqlCommand();
                com2.Connection = con;

                con.Open();

                com.CommandType = CommandType.StoredProcedure;
                com.CommandText = "insertPenghuni";

                com2.CommandType = CommandType.StoredProcedure;
                com2.CommandText = "update_kamar";

                //untuk NoIDPenghuniNew baru
                NoIDPenghuniNew.SqlDbType = SqlDbType.VarChar;
                NoIDPenghuniNew.Size = 10;
                NoIDPenghuniNew.ParameterName = "@NoIDPenghuni";
                NoIDPenghuniNew.Value = TextBox_Add_ResidentID.Text.ToString();
                NoIDPenghuniNew.Direction = ParameterDirection.Input;

                //untuk NamaPenghuniNew baru
                NamaPenghuniNew.SqlDbType = SqlDbType.VarChar;
                NamaPenghuniNew.Size = 20;
                NamaPenghuniNew.ParameterName = "@NamaPenghuni";
                NamaPenghuniNew.Value = TextBox_Add_Name.Text.ToString();
                NamaPenghuniNew.Direction = ParameterDirection.Input;

                //untuk NoTelpPenghuniNew baru
                NoTelpPenghuniNew.SqlDbType = SqlDbType.VarChar;
                NoTelpPenghuniNew.Size = 15;
                NoTelpPenghuniNew.ParameterName = "@NoTelpPenghuni";
                NoTelpPenghuniNew.Value = TextBox_Add_TelephoneNumber.Text.ToString();
                NoTelpPenghuniNew.Direction = ParameterDirection.Input;

                string jkx = "";
                //untuk jk staff baru
                JKPenghuniNew.SqlDbType = SqlDbType.VarChar;
                JKPenghuniNew.Size = 5;
                JKPenghuniNew.ParameterName = "@JKPenghuni";

                if (RadioButton_Add_Male.Checked == true)
                {
                    jkx = "P";
                }
                else if (RadioButton_Add_Female.Checked == true)
                {
                    jkx = "W";
                }
                JKPenghuniNew.Value = jkx;
                JKPenghuniNew.Direction = ParameterDirection.Input;

                //untuk alamat staff penghuni baru
                AlamatPenghuniNew.SqlDbType = SqlDbType.VarChar;
                AlamatPenghuniNew.Size = 500;
                AlamatPenghuniNew.ParameterName = "@AlamatPenghuni";
                AlamatPenghuniNew.Value = TextBox_Add_Address.Text.ToString();
                AlamatPenghuniNew.Direction = ParameterDirection.Input;

                //NoKamarPenghuniNew

                //string nokamar;
                //nokamar = comboBox3.SelectedValue.ToString(); 

                NoKamarPenghuniNew.SqlDbType = SqlDbType.VarChar;
                NoKamarPenghuniNew.Size = 5;
                NoKamarPenghuniNew.ParameterName = "@NoKamar";
                NoKamarPenghuniNew.Value = comboBox3.SelectedText; 
                NoKamarPenghuniNew.Direction = ParameterDirection.Input;

                IDCabangK.SqlDbType = SqlDbType.VarChar;
                IDCabangK.Size = 5;
                IDCabangK.ParameterName = "@IDCabang";
                IDCabangK.Value = IDCabangC;
                IDCabangK.Direction = ParameterDirection.Input;

                NoKamarPenghuniNew2.SqlDbType = SqlDbType.VarChar;
                NoKamarPenghuniNew2.Size = 5;
                NoKamarPenghuniNew2.ParameterName = "@NoKamar";
                NoKamarPenghuniNew2.Value = comboBox3.SelectedText;
                NoKamarPenghuniNew2.Direction = ParameterDirection.Input;

                IDCabangK2.SqlDbType = SqlDbType.VarChar;
                IDCabangK2.Size = 5;
                IDCabangK2.ParameterName = "@IDCabang";
                IDCabangK2.Value = IDCabangC;
                IDCabangK2.Direction = ParameterDirection.Input;



                com.Parameters.Add(NoIDPenghuniNew);
                com.Parameters.Add(NamaPenghuniNew);
                com.Parameters.Add(NoTelpPenghuniNew);
                com.Parameters.Add(JKPenghuniNew);
                com.Parameters.Add(AlamatPenghuniNew);
                com.Parameters.Add(NoKamarPenghuniNew);
                com.Parameters.Add(IDCabangK);

                com2.Parameters.Add(NoKamarPenghuniNew2);
                com2.Parameters.Add(IDCabangK2);

                com.ExecuteNonQuery();

And this is the query on SQL:

这是关于SQL的查询:

CREATE PROCEDURE insertPenghuni
    @NoIDPenghuni VARCHAR(10),
    @NoKamar VARCHAR(5),
    @IDCabang VARCHAR(5),
    @NamaPenghuni VARCHAR(30),
    @NoTelpPenghuni VARCHAR(15),
    @JKPenghuni VARCHAR(5),
    @AlamatPenghuni VARCHAR(500)
as
insert into MsPenghuni (NoIDPenghuni,NoKamar,IDCabang,NamaPenghuni,NoTelpPenghuni,JKPenghuni,AlamatPenghuni)
values (@NoIDPenghuni, @NoKamar, @IDCabang, @NamaPenghuni, @NoTelpPenghuni, @JKPenghuni, @AlamatPenghuni)
--execute update_kamar
go

--drop procedure insertpenghuni

CREATE PROCEDURE update_kamar
    @NoKamar VARCHAR(5),
    @IDCabang VARCHAR(5)
as
update MsKamar SET AvailabilityKamar=AvailabilityKamar-1
where NoKamar=@NoKamar and IDCabang=@IDCabang
GO

and this is the ERD

这是ERD

关于INSERT过程的SqlException未处理 -  C#SQL

What's wrong? And what should I do to fix this?

怎么了?我该怎么做才能解决这个问题?

1 个解决方案

#1


1  

You are trying to insert a record containing a value for a column constrained with uniqueness and the data being submitted doesn't satisfy that condition. Make sure you're specifying a valid Id, or that it is auto-incremental, if one exists, otherwise check your other columns for constraints stopping multiple instances of the same value appearing (say, in a relation-bound column).

您正在尝试插入包含受唯一性约束的列的值的记录,并且提交的数据不满足该条件。确保您指定了有效的Id,或者它是自动增量的(如果存在),否则请检查其他列是否存在阻止出现相同值的多个实例的约束(例如,在关系绑定列中)。

#1


1  

You are trying to insert a record containing a value for a column constrained with uniqueness and the data being submitted doesn't satisfy that condition. Make sure you're specifying a valid Id, or that it is auto-incremental, if one exists, otherwise check your other columns for constraints stopping multiple instances of the same value appearing (say, in a relation-bound column).

您正在尝试插入包含受唯一性约束的列的值的记录,并且提交的数据不满足该条件。确保您指定了有效的Id,或者它是自动增量的(如果存在),否则请检查其他列是否存在阻止出现相同值的多个实例的约束(例如,在关系绑定列中)。