?
Solved

Question about ASP.NET forms elements and codebehind SQL

Posted on 2008-02-07
3
Medium Priority
?
218 Views
Last Modified: 2010-04-15
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
0
Comment
Question by:igor92128
3 Comments
 
LVL 10

Expert Comment

by:Alpesh Patel
ID: 20844547
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
 

Author Comment

by:igor92128
ID: 20844682
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
 
LVL 37

Accepted Solution

by:
samtran0331 earned 2000 total points
ID: 20844764
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Kernel Data Recovery is a renowned Data Recovery solution provider which offers wide range of softwares for both enterprise and home users with its cost-effective solutions. Let's have a quick overview of the journey and data recovery tools range he…
Watch the video to learn how one can deal with PST file corruption issue with an outstanding Kernel for Outlook PST Repair Tool easily. Using this tool, non-technical users can swiftly perform the repair process to restore their essential data witho…
Suggested Courses
Course of the Month7 days, 10 hours left to enroll

608 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question