Question Trying to load a datagridview

itmasterw

Member
Joined
Jun 5, 2007
Messages
6
Programming Experience
1-3
Hi, I am trying to load a dataGrid and the code I have below works great when you have one table. However, now I have a select Sql statement that joins on two tables (Orders and Customers); and it bombs naturally on the line with:

VB.NET:
myDataAdapter.TableMappings.Add("Table", "Orders")

If someone could tell me how I can make this work I would really appreciate it.
Thank you

Here is my code:

VB.NET:
Private Sub btnFincustomers_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFincustomers.Click 
        Dim connectionString As String = "Data Source=(local)\SQLEXPRESS;Initial Catalog=NotthWind;Integrated Security=True" 

        myConnection = _ 
            New System.Data.SqlClient.SqlConnection(connectionString) 
        myConnection.Open() 
        'If myConnection.Open() Then 
        ' MessageBox.Show("ddd") 
        'End If 
        ' create the DataSet and set a property 
        myDataSet = New System.Data.DataSet() 
        myDataSet.CaseSensitive = True 

        ' create the SqlCommand object and assign the 
        ' connection and the select statement 
        myCommand = New System.Data.SqlClient.SqlCommand() 
        myCommand.Connection = myConnection 
        myCommand.CommandText = "SELECT DISTINCTROW Orders.OrderID, Orders.CustomerID, Orders.EmployeeID, Orders.OrderDate, Orders.RequiredDate, Orders.ShippedDate, Orders.ShipVia, 
Orders.Freight, Orders.ShipName, Orders.ShipAddress, Orders.ShipCity, Orders.ShipRegion, Orders.ShipPostalCode, Orders.ShipCountry, Customers.CompanyName, 
Customers.Address, Customers.City, Customers.Region, Customers.PostalCode, Customers.Country 
FROM Customers 
INNER JOIN Orders 
ON Customers.CustomerID = Orders.CustomerID 
Where Orders.CustomerID = '" + txtCustID.Text + "'" 
        ' create the myDataAdapter object and pass in the 
        ' SQL Command object and establish the table mappings 
        myDataAdapter = New System.Data.SqlClient.SqlDataAdapter() 
        myDataAdapter.SelectCommand = myCommand 
        myDataAdapter.TableMappings.Add("Table", "Orders") 

        ' Tell the myDataAdapter object to fill the DataSet 
        myDataAdapter.Fill(myDataSet) 

        ' display it in the grid 
        dtgvOrder.DataSource = myDataSet.Tables("Orders").DefaultView 

    End Sub
 
Read the DW2 link in my signature, first Creating a Simple Data App (DO NOT skip this step; you need this base knowledge) - it helps you create a simple app that demonstrates showing and saving related data. Then read the more in-depth article of Creating a Form to Show Related Data
 

Latest posts

Back
Top