Solved

How to retrieve foreign key values in Drop Down list

Posted on 2011-03-20
6
481 Views
Last Modified: 2012-05-11
Hi,

I have two tables as following.

CarCategory- CategoryID,Category-Name
Car - CarID,Descriptions,Seats,Photo,CategoryID(F.K)

I am using FormView to enter the details of Car.
So when the end-user enter values in car, The user should be able to enter Descriptions,Seats,Photo,instead of entering the category id , The end user should be able to select the Category Name from the drop down list .
( The CarID is the primary key, so the end user does not need to enter the values. it will automatically increase by 1.)

I am using object-datasource and sqldatasource in my applications.
My main goal is , when user enter the values for car tables, everything will be entered.
except the category ID, I want the user to select the Category-Name from the drop down list and Category-ID should be entered automatically into the Car Table, when user select the Category-Name.

Thanks
0
Comment
Question by:sivakugan
  • 3
  • 2
6 Comments
 
LVL 9

Expert Comment

by:kaminda
ID: 35178071
You can have add a datatable in to your dataset and get all the CategoryID,Category-Name
coulumns from CarCategory table to that datatable.  Then bind that datatable to your dropdown list. You can set valuemember as teh categoryid and display member as category-name in your dropdown binding.
Then when saving send the dropdown.datavalue to the car table insert script.
0
 
LVL 6

Expert Comment

by:nandithaa
ID: 35178087
You can set Category-Name as "Display member" and Category-ID as "Value member" for combo box. So combo box will list with Category Name and "cmbCategotryName.SelectedValue" will get you Category ID.
public void FillCategory()
        {
            DataTable dtbl = new DataTable();
            dtbl.Columns.Add("CategoryID");
            dtbl.Columns.Add("CategoryName");
            for (int i = 0; i < 5; i++)
            {
                DataRow dr = dtbl.NewRow();
                dr["CategoryID"] = i.ToString();
                dr["CategoryName"] = "Category" + i.ToString();
                dtbl.Rows.Add(dr);
            }
            cmbCategotryName.DataSource = dtbl;
            cmbCategotryName.DisplayMember = "CategoryName";
            cmbCategotryName.ValueMember = "CategoryID";
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            FillCategory();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string CategoryID = cmbCategotryName.SelectedValue.ToString();
        }

Open in new window

0
 
LVL 6

Accepted Solution

by:
nandithaa earned 500 total points
ID: 35178178
Try if this helps:
public DataTable Load()
        {
            DataTable dtblCategory = new DataTable();
            string connStr = ConfigurationManager.ConnectionStrings["ConectionString"].ToString();
            using (SqlConnection conn = new SqlConnection(connStr))
            {
                conn.Open();

                SqlDataAdapter dAd = new SqlDataAdapter("SELECT * FROM Category ", conn);
                dAd.Fill(dtblCategory);
            }
            return dtblCategory;
        }   

public void FillCategory()
        {
            DataTable dtbl = Load();
            
            cmbCategotryName.DataSource = dtbl;
            cmbCategotryName.DisplayMember = "CategoryName";
            cmbCategotryName.ValueMember = "CategoryID";
        }


 private void button1_Click(object sender, EventArgs e)
        {
            string CategoryID = cmbCategotryName.SelectedValue.ToString();
        }

Open in new window


0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:sivakugan
ID: 35194896
Do i have to have two different object data sources to do that?
0
 
LVL 6

Expert Comment

by:nandithaa
ID: 35195831
No, i just posted 2 methods, u can do which one suites u....
0
 

Author Comment

by:sivakugan
ID: 35226460
Thanks
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

In this article I will describe the Backup & Restore method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

809 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