Solved

How to retrieve foreign key values in Drop Down list

Posted on 2011-03-20
6
480 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 

Author Comment

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

Expert Comment

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

Author Comment

by:sivakugan
Comment Utility
Thanks
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
crm development 2 34
comparing string in C# 3 31
C# code editing and collaboration 3 38
c# if statement weird reaction 3 27
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
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.
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

762 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now