Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
public void find1()
        {
            SqlConnection con = new SqlConnection(constr);
            DataTable DT = new DataTable();
            DataTable DT1 = new DataTable();
            using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandText = "select x.EmpID,x.UserName,a.Assigned from (select distinct UserName,EmpID from LPER.dbo.Passwords where status='Active') x inner join (select distinct VEVCAssign,count(VEVCAssign) as Assigned from datacl where VEVCAssign<>'' and PID='" + txt_pid.Text + "' and (LID = '" + txt_lid1.Text + "' or LID = '" + txt_lid2.Text + "' or LID = '" + txt_lid3.Text + "') group by VEVCAssign) a on x.EmpID=a.VEVCAssign order by a.VEVCAssign";
                using (System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter(cmd))
                {
                    adp.Fill(DT);

                }
            }
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
         

            using (System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand())
            {
                cmd.Connection = con;
                cmd.CommandText = "select b.cnt from (select distinct UserName,EmpID from LPER.dbo.Passwords where status='Active') x inner join (select distinct VEVCAssign,VEVC,count(VEVC) as cnt from datacl where VEVCAssign<>'' and (VEVC<>'')  and PID=210 and (LID = '" + txt_lid1.Text + "' or LID = '" + txt_lid2.Text + "' or LID = '" + txt_lid3.Text + "') group by VEVCAssign,VEVC) b on x.EmpID=b.VEVCAssign order by b.VEVCAssign";
                 using (System.Data.SqlClient.SqlDataAdapter adp = new System.Data.SqlClient.SqlDataAdapter(cmd))
                 {
              
                
                     adp.Fill(DT1);

                }
            }

            DT.Merge(DT1);
            gridview_datacl.DataSource = DT;
            if (con.State != ConnectionState.Closed)
            {
                con.Close();
            }

        }


output is:

EmpID UserName Assigned Count
2016 jeya 1550
2035 saran 5652
3027 sasikumar 7063
3028 sathishbabu 2204
4003 manivannan 1953
1550
5652
7062
2204
1952

But I want output is:


EmpID UserName Assigned Count
2016 jeya 1550 1550
2035 saran 5652 5652
3027 sasikumar 7063 7062
3028 sathishbabu 2204 2204
4003 manivannan 1953 1952

Second table output is display on different rows. That is the problem i want merge both table in same DataGridView. Plz Help Some one...
Posted
Updated 7-Aug-15 0:22am
v2
Comments
RAJKUMAR M G 7-Aug-15 6:44am    
i think inner joins are problem in your query than give me your Two table structure
Arasappan 7-Aug-15 6:45am    
Is that query u used is worked on the sqlserver
Arasappan 7-Aug-15 7:01am    
By means of explaining via table mean,It may easy to understand
like
in table1
---------------
id|name|value|
---------------
1|ara | 23 |
2|RAf | 24 |
--------------
in table2
---------------
id|name|value1|
---------------
1|ara | 25 |
2|RAf | 26 |
--------------
Then I want
---------------
id|name|value|value1
---------------
1|ara | 23 |25
2|RAf | 24 |26
--------------
Richard Deeming 7-Aug-15 8:37am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.

1 solution

Hello ,
Why not you use simple join .
SQL
create    table #tmp (empid varchar(50),username varchar(50),Acount int)
insert into #tmp values('2035','saran',5662)
insert into #tmp values('3027','Sashi kumar',7063)
select * from #tmp

--creating second table
create table #tmp1 (empid varchar(50),Ccount int )
insert into  #tmp1 values('2035' , 5663)

select a.empid , a.acount , b.ccount from #tmp a inner join #tmp1 b on a.empid=b.empid

Out put will be
empid	acount	ccount
2035	5662	5663
 
Share this answer
 
Comments
Member 10385331 10-Aug-15 6:12am    
hai dude,
sry, i am asking how to two table values display in single datagriedview.
i know inner join use and marge the data but i want marge the values using only c# .Net
Animesh Datta 10-Aug-15 7:05am    
if you don't want to join then you can get the result by adding the second table's data one by one by with first datatable (assuming the column structure of both tables are same) . lastly , set the updated datatable as DataSouce.

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