0

Added that SqlConnection part as someone suggests on this website but still getting exception

Cannot insert explicit value for identity column when identity insert is OFF

on dv.SubmitChanges();

The composite primary key is composed of stid and bookid.

Also please tell me how I can make this query without concatenating textbox's value i.e bname.Text

using (SqlConnection conn = new SqlConnection(cs))
{
    conn.Open();

    using (DataClasses1DataContext dv = new DataClasses1DataContext())
    {
        var iq = (from b in dv.Books
                  where SqlMethods.Like(b.name, "%" + bname.Text + "%")
                  select b.Id).FirstOrDefault();

        IssueInfo info = new IssueInfo()
                             {
                                 stid = Convert.ToInt32(rollno.Text),
                                 due_date = Convert.ToDateTime(dateTimePicker1.Text),
                                 bookid = Convert.ToInt32(iq)
                             };

        dv.ExecuteCommand("SET IDENTITY_INSERT IssueInfo ON");
        dv.IssueInfos.InsertOnSubmit(info);
        dv.SubmitChanges();
        dv.ExecuteCommand("SET IDENTITY_INSERT IssueInfo OFF");
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
HTG
  • 1
  • 6
  • Did you check this [link](https://stackoverflow.com/questions/6139163/identity-insert-and-linq-to-sql)? – PrfctByDsgn May 03 '18 at 07:45
  • Thanks! But i was thinking why shoudn't i handle the insertion of duplicate rows from code? – HTG May 03 '18 at 17:47

0 Answers0