I have a databound dropdown on the front page that gets Name, and ModelID from the Models table. It is bound to an SQLdatasource like this: SelectCommand="SELECT [Name], [ModelID] FROM [Models].
This is just a small part of the form, and at the end I would add a new car to the Cars database, where its ModelID exists and is a foreign key.
In codebehind, the query that adds the car looks like this:
SqlCommand getCar= new SqlCommand();
getCar.Parameters["@carName"].Value = carName.Text; //From textbox
getCar.CommandText = "INSERT INTO Cars (Name, Year, ModelID, <etc>) VALUES (@carName, @carYear, 1, <etc>)";
getCar.Connection = myConnection;
I hardcoded the 1 so that it sets the ModelID to 1 everytime for example.
How would I retrieve the ModelID acquired in the SelectCommand on the main form and use that to insert into the table? I know I can run queries against this in codebehind, but I'am sure there is a better way.