asp.net - How to get the primary key of the last row inserted into the table -


i using multiview index insert news details. in first view user can enter details of news inserted db on clicking next button second view gives user add images of news (only 3 images allowed) ,

what having problem first view inserts data table dbo.newsdetail primary key newsid , while second view should add respected images using newsid of news added newsimages table. dont know how newsid of details added in first view news id working foreign key both table. or suggestions highly appreciated .

 static public void insertnews(string newsdescription, string newsimage, string newstitle) {     sqlconnection conn = new sqlconnection(connectionstring);     conn.open();     sqlcommand insertnews = new sqlcommand("insert caravannews(newsdescription, newsimage, newstitle) values ('" + newsdescription + "', '" + newsimage + "' , '" + newstitle + "')",conn);     insertnews.executenonquery();     conn.close();  } 

a separate sql statement

select scope_identity() 

or output clause

insert mytable (...= output inserted.keycol values (...) --or select ... table) 

edit:

  • change insert statement match mine
  • change .executenonquery() blah = xxx.executescalar()

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -