Click here to Skip to main content
15,922,325 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hello friends, this is actually my first post here, good to have you people.
I am new to database creation. I created a database using visual studio, it has two tables in them, I am finding it difficult displaying fields from the two tables to one datagridview, please I need help on how to solve this problem. Thanks in advance.
Lovely day.
SUNNY OKORO

This is the code I wrote for it,

SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\DB2TABS.mdf;Integrated Security=True;User Instance=True");
            SqlCommand cmd = new SqlCommand();
            conn.Open();
            SqlCommand CMD = conn.CreateCommand();
            CMD.CommandType = CommandType.Text;
          
//TABCOS, TABDETS ARE THE TABLE NAMES

            CMD.CommandText = "SELECT TABCOS.REGNO. TABDETS.FIRSTNAME FROM TABCOS, TABDETS WHERE TABCOS.REGNO = TABDETS.REGNO";

            SqlDataAdapter DA = new SqlDataAdapter();
            DA.SelectCommand = CMD;


I don't know what to do next, Please help. Thanks.
Posted
Updated 5-Apr-11 21:29pm
v3
Comments
m@dhu 6-Apr-11 2:33am    
Using capital letters is shouting over the net.
Toli Cuturicu 6-Apr-11 3:11am    
DON'T SHOUT AT ME!
Or, I'll shout back at you and vote 1.
Wayne Gaylard 6-Apr-11 3:30am    
I edited the title to remove the Capital Letters. This is considered shouting in public forums.

Try this:-
"SELECT `TC.REGNO`, `TD.FIRSTNAME` FROM `TABCOS` TC INNER JOIN `TADDETS` TD ON `TC.REGNO` = `TD.REGNO`" as your query.

Hope this helps
 
Share this answer
 
v2
use Join to get data from two tables then

SqlDataAdapter dt = new SqlDataAdapter(CommandObject)
//Declare DataSet and assign dataadapter value to dataset
using System.Data
DataSet ds = new dataSet ();
dt.fill(ds);
//to bind gridview Use
gridview1.DataSource = ds 
gridview1.DataBind();
 
Share this answer
 
v2

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