I'm doing a project to develop a library management system to a library. In it, I'm registering students by saving their details to the database. When a record is inserted at first it is saved without the given membership number.
And then, when I start to save the next student details, I get an error message saying "Violation of PRIMARY KEY constraint."
After I click the OK button in the message and try to save the data once more, I get a message saying, "Connection was not closed".
Although I try to find unclosed connection, I can't find one in my code.Here is the code.
try
{
if (rbtnMale.Checked == true)
{
Gender = "Male";
}
else if (rbtnFemale.Checked == true)
{
Gender = "Female";
}
if (cmbMemNo.Visible == true)
{
String insert_query = "INSERT INTO StReg VALUES('" + cmbMemNo.Text + "','" + txtFName.Text + "','" + txtName.Text + "','" + Gender + "','" + dtpDOB.Text + "','" + txtTelNo.Text + "','" + txtSchool.Text + "','" + txtAdNo.Text + "','" + txtMom.Text + "','" + txtMomOcc.Text + "','" + txtDad.Text + "','" + txtDadOcc.Text + "')";
Con.Open();
Cmd = new SqlCommand(insert_query, Con);
Cmd.ExecuteNonQuery();
Con.Close();
MessageBox.Show("The new Student " + txtName.Text + "( S-" + cmbMemNo.Text + " ) has successfully inserted into the system!!!", "INSERTED!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
Clear();
}
else if (txtMemNo.Visible == true)
{
String insert_query = "INSERT INTO StReg VALUES('" + cmbMemNo.Text + "','" + txtFName.Text + "','" + txtName.Text + "','" + Gender + "','" + dtpDOB.Text + "','" + txtTelNo.Text + "','" + txtSchool.Text + "','" + txtAdNo.Text + "','" + txtMom.Text + "','" + txtMomOcc.Text + "','" + txtDad.Text + "','" + txtDadOcc.Text + "')";
Con.Open();
Cmd = new SqlCommand(insert_query, Con);
Cmd.ExecuteNonQuery();
Con.Close();
MessageBox.Show("The new Student " + txtName.Text + "( S-" + cmbMemNo.Text + " ) has successfully inserted into the system!!!", "INSERTED!!!", MessageBoxButtons.OK, MessageBoxIcon.Information);
Clear();
}
}
catch (Exception ex)
{
MessageBox.Show("Error while Inserting details to the Database!!!" + Environment.NewLine + ex);
}
This is my table design.
And here is my table definition code.
CREATE TABLE [dbo].[StReg]
(
[MemNo] VARCHAR(12) NOT NULL,
[FName] VARCHAR(MAX) NOT NULL,
[Name] VARCHAR(50) NOT NULL,
[Gender] VARCHAR(6) NOT NULL,
[DOB] DATE NOT NULL,
[TelNo] VARCHAR(10) NULL,
[School] VARCHAR(50) NOT NULL,
[AdNo] VARCHAR(10) NOT NULL,
[MomName] VARCHAR(50) NOT NULL,
[MomOcc] VARCHAR(50) NOT NULL,
[DadName] VARCHAR(50) NOT NULL,
[DadOcc] VARCHAR(50) NOT NULL,
PRIMARY KEY CLUSTERED ([MemNo] ASC)
);
No matter how hard I try to find any error, I still cannot figure it out. Please help!




memNoset up as your primary key ofstringdatatype and then for the new student you are sending the same key. which is blank in your case which is giving you the Primary key violation error as there is already an entry in the table with the same value(i.e. blank).breakpointin your code before insert and check what is the value in thecmbMemNo.Textand match that value with the one in theDBfor fieldMemNo.