Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi every one here i have developed windows application using c# this is the end poin i have to reporting the data and diplay into datagriedview from two or more than 2 table

What I have tried:

cn.Open();
string getdata = "select tbl_vehicle_sale.deal_amnt,tbl_vehicle_sale.paid_amnt,tbl_vehicle_sale.due_amnt,tbl_vehicle_pur_Mast.veh_model,tbl_vehicle_pur_Mast.model_year from tbl_vehicle_sale inner join tbl_vehicle_pur_Mast on tbl_vehicle_sale.veh_no=tbl_vehicle_pur_Mast.veh_no"; cmd = new SqlCommand(getdata, cn);
dr = cmd.ExecuteReader();
while (dr.Read())
{

dataGridView1.DataSource = dr;
}
Posted
Comments
ZurdoDev 11-Feb-16 7:47am    
What is your question?
John C Rayan 11-Feb-16 8:02am    
What is your issue?
Member 12270370 11-Feb-16 8:03am    
access data using inner join from database and showing that accessed data to datagriedview
RDBurmon 11-Feb-16 8:06am    
Are you getting any error? Have you tried to run the above code?
Dj@y 11-Feb-16 8:23am    
remove while (dr.Read())
{

dataGridView1.DataSource = dr;
}
and put only

dataGridView1.DataSource = dr;

If you want to display data in a DataGridView, don't use a DataReader - use a DataTable and a DataAdapter instead:
C#
string getdata = "SELECT s.deal_amnt, s.paid_amnt, m.due_amnt, m.veh_model, m.model_year FROM tbl_vehicle_sale s INNER JOIN tbl_vehicle_pur_Mast m ON s.veh_no = m.veh_no";
using (SqlDataAdapter da = new SqlDataAdapter(getdata, cn))
   {
   DataTable dt = new DataTable();
   da.Fill(dt);
   dataGridView1.DataSource = dt;
   }

If you need a WHERE clause just add it, and add a parameter:
C#
string getdata = "SELECT s.deal_amnt, s.paid_amnt, m.due_amnt, m.veh_model, m.model_year FROM tbl_vehicle_sale s INNER JOIN tbl_vehicle_pur_Mast m ON s.veh_no = m.veh_no& WHERE s.deal_amnt = @AMNT;quot;;
using (SqlDataAdapter da = new SqlDataAdapter(getdata, cn))
   {
   da.SelectCommand.Parameters.AddWithValue("@AMNT", amountToFilterBy);
   DataTable dt = new DataTable();
   da.Fill(dt);
   dataGridView1.DataSource = dt;
   }
 
Share this answer
 
This is a simple solution to your problem add this code where you want to load your grid, either on form load or button click,...
C#
            string sql = "select tbl_vehicle_sale.deal_amnt,tbl_vehicle_sale.paid_amnt,tbl_vehicle_sale.due_amnt,tbl_vehicle_pur_Mast.veh_model,
        tbl_vehicle_pur_Mast.model_year from tbl_vehicle_sale inner join tbl_vehicle_pur_Mast on tbl_vehicle_sale.veh_no=tbl_vehicle_pur_Mast.veh_no";

using (var connection = new SqlConnection(connectionString))
using (var command = new SqlCommand(sql, connection))
using (var adapter = new SqlDataAdapter(command))
{
    connection.Open();
    var myTable = new DataTable();
    adapter.Fill(myTable);
    dataGridView1.DataSource = myTable;
}


This will fill your grid, but i have noticed your SQL query is not very correct, are you sure that query runs well on sqlserver first before coming to C#?
 
Share this answer
 
v2
Comments
Member 12270370 12-Feb-16 2:23am    
Thank you so much..

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