Domanda

within my save function, how do I set my Dropdownlist to display the most recent PK inserted?

 try{
//saving stuff where pk is inserted
con.close();
DropDownListTug.Items.Clear();
DropDownListTug.DataBind();
DropDownListTug.SelectedValue = "SCOPE_IDENTITY();"; //??? 
}
È stato utile?

Soluzione

You can use

  SELECT IDENT_CURRENT('Tablename')

Here

NOTE

In this scenario i think using SCOPE_IDENTITY() is more suitable as Crud suggested.

UPDATE

To update your dropdownlist you need to populate your dropdownList in page_load event as i told you in my comments to show recent changes and setting a default value . Look when you click button postback occurs and your page reloads and your code binds dropdownlist again (must be in page_load event) so to set a default value for your dropdownlist you need to set it's SelecedValue in Page_load event.

Altri suggerimenti

Just add the SELECT SCOPE_IDENTITY() line of the insert query as follows.

// Sample sql insert statement
INSERT INTO Test(TestId,Des) 
VALUES(@testId, @des);
SELECT SCOPE_IDENTITY()

And then should be executed the insert query with ExecuteScalar() as follows.It will return the inserted pk value.

 Int32 newId = (Int32) myCommand.ExecuteScalar();
 DropDownListTug.SelectedValue = newId.ToString() ;
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top