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 LenkaPosted Oct 9, 2013, 5:14 AM
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 LenkaPosted Oct 9, 2013, 7:19 AM
if it helps you mark it as answered.
happy coding...
SUNIL GUTTAPosted Oct 9, 2013, 7:16 AM
Have a nice day
Sanjeeb LenkaPosted Oct 9, 2013, 6:57 AM
if present show a msg. if not then insert.
SUNIL GUTTAPosted Oct 9, 2013, 6:55 AM
Thanks
Sanjeeb LenkaPosted Oct 9, 2013, 5:45 AM
As Jignesh said. 1st take the back up of your table then drop it. Create it again with identity.
Jignesh TrivediPosted Oct 9, 2013, 5:42 AM
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...
SUNIL GUTTAPosted Oct 9, 2013, 5:24 AM
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 ??
SUNIL GUTTAPosted Oct 9, 2013, 5:01 AM
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
Sanjeeb LenkaPosted Oct 9, 2013, 4:49 AM
share your code. and mark where you are getting exception.