Question about ASP.NET forms elements and codebehind SQL

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.Add("@carName", SqlDbType.VarChar);
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.

thanks,
Igor
igor92128Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Alpesh PatelCommented:
i am not getting you properly
what u are doing is actually perfect
if you are using dropdown for the model name then u can get its id from dropdown it self
so i am not getting what is your requirement
can u plz elobrate the things so i can help you
bcoze i have the same page with my project which has the same front end
0
igor92128Author Commented:
Well, I'am displaying the model name in the dropdown, which is associate with a ModelID. I don't care about the model name since its only useful when a human is reading the form.  I need to use the insert statement to insert the modelID into a table when creating a new 'Car'.
0
samtran0331Commented:
If your dropdownlist is bound correctly:
        <asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="Name" DataValueField="ModelID">
        </asp:DropDownList>


then to get the value for the insert, you would do it just like you did your textbox:
getCar.Parameters.Add("@carModelID", SqlDbType.Int);
getCar.Parameters["@carModelID"].Value = DropDownList1.SelectedIndex.Value;
...

getCar.CommandText = "INSERT INTO Cars (Name, Year, ModelID, <etc>) VALUES (@carName, @carYear, @carModelID, <etc>)";
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.