Click here to Skip to main content
15,897,360 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have two tables countermaster2 and partyMaster2. in first table(countermaster2) one field has primary key and same field has foreign key in the second table(partymaster2).
in my query i want. If i enter partymaster2 fields's value into the given textboxes then related foreign key values should be display automatically.and the correspondent values should be change as per the values.

What I have tried:

SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=DelhiEMP;Integrated Security=True");
           con.Open();
           SqlCommand cmd = new SqlCommand("select Code from partyMaster2 where Code = (select Code from countermaster2 where Code ='@Code')", con);
           cmd.ExecuteScalar();
           string val = string.Empty;
           cmd.Parameters.AddWithValue("@Code",val);
           textBox6.Text = val;

           con.Close();
Posted
Updated 18-Apr-17 17:18pm
Comments
kumar nitesh 13-Apr-17 2:48am    
If you are trying to get all rows from partyMaster2 where code matches your input,then what is the need to select it from countermaster2 . Please make clear your requirements .
faizy001 13-Apr-17 3:16am    
i want if i enter table2 ID in textbox then code value which is table1 correspondence value should be display
faizy001 13-Apr-17 3:40am    
kumar-G main ye chahta hu ki jab main form2 main partymaster2 ki ID insert karu tab automatically countermaster2 se code value textbox main display ho jaye. or jaise jaise main partymaster2 ki ID insert karu tab code value bhi change ho jaye

1 solution

You can do it simply by finding from the countermaster2.

If you have to fetch columns from both of tables, you should use join using countermaster2.

Its your choice but here your mistake as in your code is

cmd.ExecuteScalar();
string val = string.Empty;
cmd.Parameters.AddWithValue("@Code",val);
textBox6.Text = val;


Change this part to be as
string val = textBox6.Text
cmd.Parameters.AddWithValue("@Code",val);
cmd.ExecuteScalar();


Enjoy!
Ye mera India. :-)
 
Share this answer
 

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