Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Im getting error in following code.

C#
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;

public partial class DataRelation : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        string connetionString = null;
        SqlConnection con;
        SqlDataAdapter da1;
        SqlDataAdapter da2;
        DataRelation relation;
        SqlCommand command = new SqlCommand();
        DataSet ds = new DataSet();
        connetionString = ConfigurationManager.ConnectionStrings["ConnectionStringDemo"].ConnectionString.ToString();
        con = new SqlConnection(connetionString);
        da1 = new SqlDataAdapter("select  top  (20)* from Production.Product", con);
        da2 = new SqlDataAdapter("select  top  (20) * from Sales.SalesOrderDetail", con);
        con.Open();
        da1.Fill(ds, "Production.Product");
        da2.Fill(ds, "Sales.SalesOrderDetail");
        con.Close();

        relation = ds.Relations.Add("CustOrders",
            ds.Tables["Production.Product"].Columns["ProductID"],
            ds.Tables["Sales.SalesOrderDetail"].Columns["ProductID"]);

        foreach (DataRow pRow in ds.Tables["Production.Product"].Rows)
        {
            Console.WriteLine(pRow["ProductID"]);
            foreach (DataRow cRow in pRow.GetChildRows(relation))
                Console.WriteLine("\t" + cRow["SalesOrderID"]);
        }
    }
}


pls help me..

[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 27-Sep-12 21:39pm
v2

1 solution

The System.Data namespace already contains a class called DataRelation, and this is returned from a DataSet.Relations.Add method call.
The variable you have tried to store it in is declared as a DataRelation - which is the class name your method sits in. There is no conversion defined between the two unrelated types.

Either change the name of your class (but don't change the definition of relation) or fully qualify the definition:
C#
System.Data.DataRelation relation;
Personally, if I was going to use System.Data, I would not call my class a conflicting name, just to avoid confusion!
 
Share this answer
 
Comments
vision2fly 28-Sep-12 4:19am    
thanks OriginalGriff..!
OriginalGriff 28-Sep-12 5:04am    
You're wecome!

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