Click here to Skip to main content
15,914,820 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
can anybody help me in my c# project, i have two table, customer and container, customer primary key is foreign key in container,both have separate form but no any class, now i add/det,in customer form,but i want to do same in container form, but there is combo box for customer foreign key, and it did show any data, althoug, i tried many time, but did not get any success, can anybody help me to solve this ??? i will be thanks full
this is my cod . . .


C#
private void Container_Load(object sender, EventArgs e)
        {

            SqlConnection mycon = new SqlConnection();
            mycon.ConnectionString = "Data Source=abdarkhanyousaf\\sqlexpress;Initial Catalog=REHMAN_BROTHERS;Integrated Security=True";
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = mycon;
            cmd.CommandText = "select * from container ";
            SqlDataAdapter dta = new SqlDataAdapter();
            dta.SelectCommand = cmd;
            DataSet ds = new DataSet();
            DataTable tb = new DataTable();
            dta.Fill(tb);

            customer cu = new customer();
            comboBox1.DataSource = ds.Tables["customer"];
            comboBox1.DisplayMember = ("customer_id");
            comboBox1.ValueMember = ("customer_id");
            
        }
Posted
Updated 10-Jan-16 19:58pm
v2
Comments
Suvendu Shekhar Giri 10-Jan-16 12:46pm    
To solve what? You haven't shared your problem.
Review the question and update accordingly.
Member 11879814 10-Jan-16 13:20pm    
sir my prlbm is just, want to show data in combo box,e.g want to show customer name or key in on container form,

change your sql statement to like below
SQL
"select customer_id,customer_name from customer"

then it will load the customers from customer table. you can set value and display fields as below
C#
comboBox1.DisplayMember = "customer_name";
comboBox1.ValueMember = "customer_id";

Now your combobox will display customers names but when user select item from combobox you can get the selected value as customer id.
 
Share this answer
 
First of all, do not show keys on screen. Show customer names instead. To do this, you will need to use a join between container and customer table. You can choose between left join or inner join based on your requirements. Your query when then return all the details for a container along with customer Id and name. In the C# code, you can then set the name as display member for customer drop down and keep the key as value member for tracking and updates etc.

Your query would probably look something like this:

SQL
select * from containers inner join customers on container.customerId = customers.customerId


You should ideally give column names to return only the data that the screen would need.
 
Share this answer
 
Comments
Member 11879814 11-Jan-16 2:01am    
Thank u sir, good idea, but combo box still did not show any value as my code blow
private void Container_Load(object sender, EventArgs e)
{

SqlConnection mycon = new SqlConnection();
mycon.ConnectionString = "Data Source=abdarkhanyousaf\\sqlexpress;Initial Catalog=REHMAN_BROTHERS;Integrated Security=True";
SqlCommand cmd = new SqlCommand();
cmd.Connection = mycon;
cmd.CommandText = "select * from container inner join customer on container.customer_Id = customer.customer_Id";
SqlDataAdapter dta = new SqlDataAdapter();
dta.SelectCommand = cmd;
DataSet ds = new DataSet();
dta.Fill(ds);
comboBox1.DataSource = ds.Tables["container"];
comboBox1.DisplayMember = ("customer_name");
comboBox1.ValueMember = ("customer_id");

}
dan!sh 11-Jan-16 3:07am    
What is returned from your query? Is there any data returned? Your dataset does not have a table called container. Replace ds.Tables["container"] with ds.Tables[0] and check what data is returned.
Member 11879814 11-Jan-16 4:08am    
it show data in grid view when click on show button but not in combo box,mean i cant insert as well bcuz of no value in combo box,
dan!sh 11-Jan-16 4:09am    
Did you try steps in my comment above?
Member 11879814 11-Jan-16 4:35am    
yes sir i did,i also check the query on sql,work correctly, just prblm is of combo box

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900