Click here to Skip to main content
15,892,674 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi! I have a lines of codes in VB.NET and unable to convert it to c#. Please anyone help me. Thanks in advance.



VB
Dim DSNWind As DataSet
       Dim CNnwind As New SqlClient.SqlConnection("DATA SOURCE=Server;USER ID=sa;INITIAL CATALOG=northwind;") '<==== CHANGE HERE
       Dim DACustomers As New SqlClient.SqlDataAdapter("SELECT CustomerID, CompanyName, ContactName, Country FROM customers WHERE country = 'Germany'", CNnwind)
       Dim DAOrders As New SqlClient.SqlDataAdapter("SELECT CustomerID, OrderID, OrderDate, ShippedDate, ShipVia, Freight FROM orders where customerid in (select customerid from customers where country = 'Germany')", CNnwind)
       Dim DAOrderDetails As New SqlClient.SqlDataAdapter("Select * from [Order Details] where OrderID in (SELECT OrderID FROM orders where customerid in (select customerid from customers where country = 'Germany'))", CNnwind)

       DSNWind = New DataSet()
       CNnwind.Open()
       DACustomers.Fill(DSNWind, "dtCustomers")
       DAOrders.Fill(DSNWind, "dtOrders")
       DAOrderDetails.Fill(DSNWind, "dtOrderDetails")
       'Close the connection to the data store; free up the resources
       CNnwind.Close()

       'Create a data relation object to facilitate the relationship between the Customers and Orders data tables.
       DSNWind.Relations.Add("CustToOrd", DSNWind.Tables("dtCustomers").Columns("CustomerID"), DSNWind.Tables("dtOrders").Columns("CustomerID"))
       DSNWind.Relations.Add("OrdToDet", DSNWind.Tables("dtOrders").Columns("OrderID"), DSNWind.Tables("dtOrderdetails").Columns("OrderID"))
       '''''''''''''''''''''''
       TreeView1.Nodes.Clear()
       //Dim i, n As Integer
       Dim parentrow As DataRow
       Dim ParentTable As DataTable
       ParentTable = DSNWind.Tables("dtCustomers")

       For Each parentrow In ParentTable.Rows
           Dim parentnode As TreeNode
           parentnode = New TreeNode(parentrow.Item(0))
           TreeView1.Nodes.Add(parentnode)
           ''''populate child'''''
           '''''''''''''''''''''''
           Dim childrow As DataRow
           Dim childnode As TreeNode
           childnode = New TreeNode()
           For Each childrow In parentrow.GetChildRows("CustToOrd")
               childnode = parentnode.Nodes.Add(childrow(0) & " " & childrow(1) & " " & childrow(2))
               childnode.Tag = childrow("OrderID")
               ''''populate child2''''
               ''''''''''''''''''''''''''
               Dim childrow2 As DataRow
               Dim childnode2 As TreeNode
               childnode2 = New TreeNode()
               For Each childrow2 In childrow.GetChildRows("OrdToDet")
                   childnode2 = childnode.Nodes.Add(childrow2(0))

               Next childrow2
               ''''''''''''''''''''''''

           Next childrow
           '''''''''''''''
       Next parentrow
Posted
Comments
scottgp 7-Mar-12 13:52pm    
Try http://www.developerfusion.com/tools/convert/vb-to-csharp/
apurbadoley 7-Mar-12 14:30pm    
Hi it is showing an error message "Cannot convert from 'object' to 'string'.

Developer fusion[^] auto translated it (once I had corrected the comment immediately after the Nodes.Clear)

C#
DataSet DSNWind = null;
System.Data.SqlClient.SqlConnection CNnwind = new System.Data.SqlClient.SqlConnection("DATA SOURCE=Server;USER ID=sa;INITIAL CATALOG=northwind;");
//<==== CHANGE HERE 
System.Data.SqlClient.SqlDataAdapter DACustomers = new System.Data.SqlClient.SqlDataAdapter("SELECT CustomerID, CompanyName, ContactName, Country FROM customers WHERE country = 'Germany'", CNnwind);
System.Data.SqlClient.SqlDataAdapter DAOrders = new System.Data.SqlClient.SqlDataAdapter("SELECT CustomerID, OrderID, OrderDate, ShippedDate, ShipVia, Freight FROM orders where customerid in (select customerid from customers where country = 'Germany')", CNnwind);
System.Data.SqlClient.SqlDataAdapter DAOrderDetails = new System.Data.SqlClient.SqlDataAdapter("Select * from [Order Details] where OrderID in (SELECT OrderID FROM orders where customerid in (select customerid from customers where country = 'Germany'))", CNnwind);

DSNWind = new DataSet();
CNnwind.Open();
DACustomers.Fill(DSNWind, "dtCustomers");
DAOrders.Fill(DSNWind, "dtOrders");
DAOrderDetails.Fill(DSNWind, "dtOrderDetails");
//Close the connection to the data store; free up the resources
CNnwind.Close();

//Create a data relation object to facilitate the relationship between the Customers and Orders data tables.
DSNWind.Relations.Add("CustToOrd", DSNWind.Tables["dtCustomers"].Columns["CustomerID"], DSNWind.Tables["dtOrders"].Columns["CustomerID"]);
DSNWind.Relations.Add("OrdToDet", DSNWind.Tables["dtOrders"].Columns["OrderID"], DSNWind.Tables["dtOrderdetails"].Columns["OrderID"]);
///''''''''''''''''''''
TreeView1.Nodes.Clear();
////Dim i, n As Integer
DataRow parentrow = null;
DataTable ParentTable = null;
ParentTable = DSNWind.Tables["dtCustomers"];
foreach (DataRow parentrow_loopVariable in ParentTable.Rows) {
	parentrow = parentrow_loopVariable;
	TreeNode parentnode = default(TreeNode);
	parentnode = new TreeNode(parentrow[0]);
	TreeView1.Nodes.Add(parentnode);
	///'populate child'''''
	///''''''''''''''''''''
	DataRow childrow = null;
	TreeNode childnode = default(TreeNode);
	childnode = new TreeNode();
	foreach (DataRow childrow_loopVariable in parentrow.GetChildRows("CustToOrd")) {
		childrow = childrow_loopVariable;
		childnode = parentnode.Nodes.Add(childrow[0] + " " + childrow[1] + " " + childrow[2]);
		childnode.Tag = childrow["OrderID"];
		///'populate child2''''
		///'''''''''''''''''''''''
		DataRow childrow2 = null;
		TreeNode childnode2 = default(TreeNode);
		childnode2 = new TreeNode();
		foreach (DataRow childrow2_loopVariable in childrow.GetChildRows("OrdToDet")) {
			childrow2 = childrow2_loopVariable;
			childnode2 = childnode.Nodes.Add(childrow2[0]);

		}
		///'''''''''''''''''''''

	}
	///''''''''''''
}
 
Share this answer
 
It is showing an error message "Cannot convert from 'object' to 'string'.
 
Share this answer
 
 
Share this answer
 
The solution given by OriginalGriff is correct hence voted 5. But the error you are receiving is because, the value of Column in Row is of Object type and you have to cast it to string when you are using in the Nodes.Add etc. as below

C#
foreach (DataRow parentrow_loopVariable in ParentTable.Rows) {
	parentrow = parentrow_loopVariable;
	TreeNode parentnode = new TreeNode((string)parentrow[0]);
	TreeView1.Nodes.Add(parentnode);
	///'populate child'''''
	///''''''''''''''''''''
	DataRow childrow;
	TreeNode childnode = new TreeNode();
	foreach (DataRow childrow_loopVariable in parentrow.GetChildRows("CustToOrd")) {
		childrow = childrow_loopVariable;
		childnode = parentnode.Nodes.Add((string)childrow[0] + " " + (string)childrow[1] + " " + (string)childrow[2]);
		childnode.Tag = childrow["OrderID"];
		///'populate child2''''
		///'''''''''''''''''''''''
		DataRow childrow2 = null;
		TreeNode childnode2 = new TreeNode();
		foreach (DataRow childrow2_loopVariable in childrow.GetChildRows("OrdToDet")) {
			childrow2 = childrow2_loopVariable;
			childnode2 = childnode.Nodes.Add((string)childrow2[0]);
 
		}
		///'''''''''''''''''''''

	}
	///''''''''''''
}
 
Share this answer
 
v2
CONVERTED CODE OF C# FROM VB CODE....



DataSet DSNWind = default(DataSet);
SqlClient.SqlConnection CNnwind = new SqlClient.SqlConnection("DATA SOURCE=Server;USER ID=sa;INITIAL CATALOG=northwind;");
SqlClient.SqlDataAdapter DACustomers = new SqlClient.SqlDataAdapter("SELECT CustomerID, CompanyName, ContactName, Country FROM customers WHERE country = 'Germany'", CNnwind);
SqlClient.SqlDataAdapter DAOrders = new SqlClient.SqlDataAdapter("SELECT CustomerID, OrderID, OrderDate, ShippedDate, ShipVia, Freight FROM orders where customerid in (select customerid from customers where country = 'Germany')", CNnwind);
SqlClient.SqlDataAdapter DAOrderDetails = new SqlClient.SqlDataAdapter("Select * from [Order Details] where OrderID in (SELECT OrderID FROM orders where customerid in (select customerid from customers where country = 'Germany'))", CNnwind);

DSNWind = new DataSet();
CNnwind.Open();
DACustomers.Fill(DSNWind, "dtCustomers");
DAOrders.Fill(DSNWind, "dtOrders");
DAOrderDetails.Fill(DSNWind, "dtOrderDetails");

CNnwind.Close();

DSNWind.Relations.Add("CustToOrd", DSNWind.Tables("dtCustomers").Columns("CustomerID"), DSNWind.Tables("dtOrders").Columns("CustomerID"));
DSNWind.Relations.Add("OrdToDet", DSNWind.Tables("dtOrders").Columns("OrderID"), DSNWind.Tables("dtOrderdetails").Columns("OrderID"));

TreeView1.Nodes.Clear();

DataRow parentrow = default(DataRow);
DataTable ParentTable = default(DataTable);
ParentTable = DSNWind.Tables("dtCustomers");

foreach ( parentrow in ParentTable.Rows) {
TreeNode parentnode = default(TreeNode);
parentnode = new TreeNode(parentrow.Item(0));
TreeView1.Nodes.Add(parentnode);

DataRow childrow = default(DataRow);
TreeNode childnode = default(TreeNode);
childnode = new TreeNode();
foreach ( childrow in parentrow.GetChildRows("CustToOrd")) {
childnode = parentnode.Nodes.Add(childrow(0) + " " + childrow(1) + " " + childrow(2));
childnode.Tag = childrow("OrderID");

DataRow childrow2 = default(DataRow);
TreeNode childnode2 = default(TreeNode);
childnode2 = new TreeNode();
foreach ( childrow2 in childrow.GetChildRows("OrdToDet")) {
childnode2 = childnode.Nodes.Add(childrow2(0));

}


}

}
 
Share this answer
 

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