Question dgv Master / Child code

peterpiper

Member
Joined
Nov 6, 2013
Messages
5
Programming Experience
Beginner
I picked up the following code from the internet but I am getting errors
I think there is an error on the sqlSelect statement. I am using VS2012

Here is the code

C#:
        protected DataSet LoadData()
        {
            string sqlConnectString = @
                Integrated security=SSPI;Initial Catalog=AdventureWorks;

            string sqlSelect =
                @
                FROM Sales.SalesOrderHeader
                WHERE SalesOrderID BETWEEN 43660 AND 43669;
                SELECT SalesOrderID, SalesOrderDetailID, OrderQty, ProductID, LineTotal
                FROM Sales.SalesOrderDetail
                WHERE SalesOrderID BETWEEN 43660 AND 43669;

            // Fill and return a DataSet containing sales order header and sales order
            // detail DataTable objects that are related on the SalesOrderID field
            SqlDataAdapter da = new SqlDataAdapter(sqlSelect, sqlConnectString);
            da.TableMappings.Add("Table", "SalesOrderHeader");
            da.TableMappings.Add("Table1", "SalesOrderDetail");
            DataSet ds = new DataSet();
            da.Fill(ds);

            // Add a relation between parent and child table.
            ds.Relations.Add("FK_SalesOrderDetail_SalesOrderHeader",
                ds.Tables["SalesOrderHeader"].Columns["SalesOrderID"],
                ds.Tables["SalesOrderDetail"].Columns["SalesOrderID"]);

            return ds;
        }
Would appreciate if someone could tell me what is exactly wrong.

The full project code can be found here
synchronizing_master-detail_data_in_a_wi.html
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,140
Location
Sydney, Australia
Programming Experience
10+
Firstly, if you get errors then you should tell us what they are. That way, we know what we're looking for and we don't have to waste our time working out what you already know.

In this case though, I can see one issue right off. You're trying to assign a string literal to the 'sqlSelect' variable but string literals are delimited by double quotes. You have no double quotes, so you presumably have not actually copied the code properly.

EDIT: Just checked the link you provided and it looks like it was their error rather than yours. That said, even a beginner should probably know that string literals require double quotes, so it's important to examine code that you get from elsewhere closely.
 
Top Bottom