Max Saeed
Max Saeed

Reputation: 13

How to use DataTable from SQL Server in C#

I want to view this table in the list box in C#

Data Table and Accompanying SQL Query

I'm using this query to show the table

select 
    Chicken_Name, WithOrWithout_Name, Chicken_Price 
from  
    Tbl_Add  a
full outer join 
    tbl_Chicken b ON b.Chicken_ID = a.Chicken_ID
full outer join 
    Tbl_WithORWithot c ON a.WorWO_ID = c.WithOrWothout_ID ;

And this code to put it in the list box in C#

private void Chicken()
{
    using (connection = new SqlConnection(connectionString))
    using (SqlDataAdapter adapter = new SqlDataAdapter("select Chicken_Name,WithOrWithout_Name,Chicken_Price from  Tbl_Add  a full outer join tbl_Chicken b ON b.Chicken_ID = a.Chicken_ID full outer join Tbl_WithORWithot c ON a.WorWO_ID = c.WithOrWothout_ID ; ", connection))
    {
        DataTable tbl_Chicken = new DataTable();
        adapter.Fill(tbl_Chicken);
                     
        lst_SHowdata.DisplayMember = "Chicken_Name";
        lst_SHowdata.ValueMember = "Chicken_ID";

        lstSHowdata2.DisplayMember = "Chicken_Price";
              
        lst_SHowdata.DataSource = tbl_Chicken;
        lstSHowdata2.DataSource = tbl_Chicken;
    

This is the full code

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace HABIBIS_GRILL
{
    public partial class HabibisGrll : Form
    {
        SqlConnection connection;
        string connectionString;

        public HabibisGrll()
        {
            InitializeComponent();

            connectionString = ConfigurationManager.ConnectionStrings["HABIBIS_GRILL.Properties.Settings.HabibisGrilllDataBAseConnectionString"].ConnectionString;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            Chicken();
        }

        private void Chicken()
        {
            using (connection = new SqlConnection(connectionString))
            using (SqlDataAdapter adapter = new SqlDataAdapter("select Chicken_Name,WithOrWithout_Name,Chicken_Price from  Tbl_Add  a full outer join tbl_Chicken b ON b.Chicken_ID = a.Chicken_ID full outer join Tbl_WithORWithot c ON a.WorWO_ID = c.WithOrWothout_ID ; ", connection))
            {
                DataTable tbl_Chicken = new DataTable();
                adapter.Fill(tbl_Chicken);

                lst_SHowdata.DisplayMember = "Chicken_Name";
                lst_SHowdata.ValueMember = "Chicken_ID";

                lstSHowdata2.DisplayMember = "Chicken_Price";
             
                lst_SHowdata.DataSource = tbl_Chicken;
                lstSHowdata2.DataSource = tbl_Chicken;
            }
        }
    }
}

The problem is I need to use one list box for each column, how to make them in one list box or do I use another tool ?

And how to put 2 or more tables in one datatable (adapter) ?

Upvotes: 0

Views: 3891

Answers (1)

Badiparmagi
Badiparmagi

Reputation: 1285

first question:

you can use gridview for your query if you want to show more than 1 column from your query.

Second question:

and you can use DataSet to get more than 1 table in case you need.

check this:

string queryCustomer = 
"SELECT CustomerID, CompanyName FROM dbo.Customers";
SqlDataAdapter adapter = new SqlDataAdapter(queryCustomer , connection);

DataSet dataSet = new DataSet();
adapter.Fill(dataSet , "Customers");

string queryOrders = 
"SELECT OrderId, Date FROM dbo.Orders";
SqlDataAdapter adapterOrder = new SqlDataAdapter(queryOrders , connection);
adapterOrder.Fill(dataSet , "Orders");

//assume you have a GridView named myGridview.
//than set its DataSource to one of your DataSet tables.

myGridview.DataSource = dataSet.Tables[0] //customers will be the source

Upvotes: 1

Related Questions