Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System;
using System.Data;
using System.Data.SqlClient;
using System.Windows.Forms;
using Microsoft.VisualBasic;

namespace Northwind_Sql_Data_Extraction
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'northwindDataSet.Customers' table. You can move, or remove it, as needed.
            this.customersTableAdapter.Fill(this.northwindDataSet.Customers);
            // TODO: This line of code loads data into the 'northwindDataSet.Order_Details' table. You can move, or remove it, as needed.
            this.order_DetailsTableAdapter.Fill(this.northwindDataSet.Order_Details);
            // TODO: This line of code loads data into the 'northwindDataSet.Orders' table. You can move, or remove it, as needed.
            this.ordersTableAdapter.Fill(this.northwindDataSet.Orders);        
        }

        private void FunctionOne()
        {
            using (SqlConnection con = new SqlConnection("Data Source=LAPTOP-CI6IN3IA\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"))
            {
                string str = "SELECT Orders.OrderID, Orders.OrderDate, Customers.CompanyName, Customers.ContactName FROM Customers RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID WHERE(((Orders.OrderID) = 10255))";
                SqlCommand cmd = new SqlCommand(str, con);
                SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                adapter.Fill(dt);
                dataGridView1.DataSource = new BindingSource(dt, null);
            }
        }

        private void FunctionTwo()
        {
            using (SqlConnection con = new SqlConnection("Data Source=LAPTOP-CI6IN3IA\\SQLEXPRESS;Initial Catalog=Northwind;Integrated Security=True"))
            {
                string str2 = "SELECT Customers.CompanyName, Orders.OrderID, Orders.OrderDate, Order Details.ProductID, Order Details.Quantity, Order Details.UnitPrice FROM (Customers RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID) LEFT JOIN Order Details ON Orders.OrderID = Order Details.OrderID WHERE(((Orders.OrderID) = 10255))";
                SqlCommand cmd2 = new SqlCommand(str2, con);
                SqlDataAdapter adapter1 = new SqlDataAdapter(cmd2);
                DataTable dt2 = new DataTable();
                adapter1.Fill(dt2);
                dataGridView1.DataSource = new BindingSource(dt2, null);
            }
        }

        private void Button1_Click(object sender, EventArgs e)
        {
            switch (comboBox1.SelectedItem.ToString())
            {
                case "Task 1":
                    FunctionOne();
                    break;
                case "Task 2":
                    FunctionTwo();
                    break;
                case "Task 3":
                    FunctionTwo();
                    break;
                case "Task 4":
                    FunctionTwo();
                    break;
                case "Task 5":
                    FunctionTwo();
                    break;
                default:
                    MessageBox.Show("Please select a value between 1 and 5");
                    break;
            }
        }
    }
}


What I have tried:

I have tried changing Order details to OrderDetails but it provides another error saying invalid object name, I tried rewriting it in a different way but it keeps on providing the same error, I need help please with function two
Posted
Updated 14-Mar-22 10:57am

The error message is telling you that Order is not a valid name at that point. So exactly which table and items are you trying to refer to? If you have a table with the name "Order Details", then you need to use square brackets round it thus:
SQL
string str2 = "SELECT Customers.CompanyName, Orders.OrderID, Orders.OrderDate, [Order Details].ProductID, [Order Details].Quantity, [Order Details].UnitPrice FROM (Customers RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID) LEFT JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID WHERE(((Orders.OrderID) = 10255))";

But putting spaces in table names is not the best of ideas.
 
Share this answer
 
Comments
Tafadzwa Tapera 14-Mar-22 17:15pm    
noted, thank you
Maciej Los 15-Mar-22 9:29am    
5ed!
Well... when a table name is not a single word, you have to use [] around it.
So, change this:
SQL
LEFT JOIN Order Details

to:
SQL
LEFT JOIN [Order Details]


I'd also suggest to read about aliases[^]. This will help you to shorten query from this:
SQL
FROM (Customers RIGHT JOIN Orders ON Customers.CustomerID = Orders.CustomerID) LEFT JOIN Order Details ON Orders.OrderID = Order Details.OrderID

to this:
SQL
FROM (Customers AS C RIGHT JOIN Orders AS O ON C.CustomerID = O.CustomerID) LEFT JOIN [Order Details] AS OD ON O.OrderID = OD.OrderID


Do you see the difference?

[EDIT]
And the last thing...
Change the code to use single function/procedure with SqlParameters[^]. Keep in mind that you can use parameterized queries. See:
Using Parameters for SQL Server Queries and Stored Procedures[^]
SqlCommand.Parameters Property (System.Data.SqlClient) | Microsoft Docs[^]

Good luck!
 
Share this answer
 
v4
Comments
Tafadzwa Tapera 14-Mar-22 17:02pm    
Yes I see the difference thank you for the tip, I will integrate that into my solution
Maciej Los 14-Mar-22 17:10pm    
You're very welcome. See updated answer (i've added another tip).

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