Solved

How to retrieve foreign key values in Drop Down list

Posted on 2011-03-20
6
484 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Edgartown IT Case Study

Learn about Edgartown's quest to ensure the safety and security of the entire town's employee and citizen data. Read the case study!

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

751 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