Solved

How to retrieve foreign key values in Drop Down list

Posted on 2011-03-20
6
482 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
Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

 

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

SQL Server engine let you use a Windows account or a SQL Server account to connect to a SQL Server instance. This can be configured immediatly during the SQL Server installation or after in the Server Authentication section in the Server properties …
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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