Hi
Well i tried to insert records into DB from created web-form . I used string.format(insert statement ) & Data reader :)
Initially 1st insert is going successfully but second insert popping some dam error like below
Violation of PRIMARY KEY constraint 'PK_studentform'. Cannot insert duplicate key in object 'dbo.studentform'.
The statement has been terminated.
I maintained uniqueness in Pk column but why am i facing this ??
Please help me
Loading


Sanjeeb LenkaRank #301 · 8.3K reputationPosted Oct 9, 2013, 1:14 AM EDT
you have to put Indentity to Id column in database so it will increment automatically no need to insert it from front end .
1st make the Studentid column to identity and primary key
then try this code:
protected void Button7_Click(object sender, EventArgs e)
{
cmd.CommandText = string.Format("insert into studentform(studentname,emailid,contactno,course,level,semister,username,password) values({0},'{1}','{2}',{3},{4},{5},{6},'{7}')", TextBox2.Text, TextBox3.Text, TextBox4.Text, TextBox5.Text, TextBox6.Text, TextBox7.Text, TextBox8.Text, TextBox9.Text);
con.Open();
int count = cmd.ExecuteNonQuery();
Response.Write("ssss" + count);
con.Close();
}
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Oct 9, 2013, 12:49 AM EDT
share your code. and mark where you are getting exception.
SUNIL GUTTARank #1,832 · 810 reputationPosted Oct 9, 2013, 1:01 AM EDT
In my website under adminadd.aspx in that content page having button under that one i have code do check that
//error occuring in this section :) at excecute no query
Thank you
SUNIL GUTTARank #1,832 · 810 reputationPosted Oct 9, 2013, 1:24 AM EDT
Well i created identity as u mentioned but when i tried to save the created changes are not applying ?? it asking me to re-create table form scratch // any remedy ??
Jignesh TrivediRank #16 · 50.3K reputationPosted Oct 9, 2013, 1:42 AM EDT
hi,
yes I think you have to drop this column and recreate this column
If the data present on table than temporary transfer it to another table and drop this column and recreate column.
try this let us know if you face any difficulty...
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Oct 9, 2013, 1:45 AM EDT
As Jignesh said. 1st take the back up of your table then drop it. Create it again with identity.
SUNIL GUTTARank #1,832 · 810 reputationPosted Oct 9, 2013, 2:55 AM EDT
Thanks
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Oct 9, 2013, 2:57 AM EDT
if present show a msg. if not then insert.
SUNIL GUTTARank #1,832 · 810 reputationPosted Oct 9, 2013, 3:16 AM EDT
Have a nice day
Sanjeeb LenkaRank #301 · 8.3K reputationPosted Oct 9, 2013, 3:19 AM EDT
if it helps you mark it as answered.
happy coding...