Dynamic Add Data into TreeView in C#

This technique apply for nested data store on relational dbms, for examples:

Table1
+——–+——–+———-+
|  ID     |  Name  | ParentID |
+——–+——–+———-+
|  1     |  A     | 0        |
|  2     |  B     | 0        |
|  3     |  C     | 0        |
|  4     |  a     | 1        |
|  5     |  b     | 1        |
|  6     |  c     | 2        |
|  7     |  d     | 3        |
|  8     |  e     | 3        |
+——–+——–+———-+

And we want to show in Treeview control as:

TreeviewControl
+–
|-A-
|-a
|-b
|
|-B-
|-c
|
|-C-
|-d
|-e

So we do not need to use [Loop] to insert sub tree in to other tree.

IEnumerable<Table1> dataCollection =  // From table [Table1] “in Linq code”;

foreach(var record in dataCollection)
{
if(record.ParentID == 0)
{
TreeviewControl.Nodes.Add(record.ID.toString(), record.Name);
}
else
{
TreeNode[] tn = TreeviewControl.Nodes.Find(record.ParentID.toString(), true);
tn[0].Nodes.Add(record.ID.toString(), record.Name);
}

How to call a .NET web service from android?

Hai, Finally i got the solution for my own question.

Here is the code:

package projects.ksoap2sample;

import org.ksoap2.SoapEnvelope; import org.ksoap2.serialization.SoapObject; import org.ksoap2.serialization.SoapSerializationEnvelope; import org.ksoap2.transport.HttpTransportSE; import android.app.; import android.os.; import android.widget.TextView;

public class ksoap2sample extends Activity { /** Called when the activity is first created. */ private static final String SOAP_ACTION = “http://tempuri.org/HelloWorld&#8221;;

private static final String METHOD_NAME = "HelloWorld"; private static final String NAMESPACE = "http://tempuri.org/"; private static final String URL = "http://192.168.1.19/TestWeb/WebService.asmx"; TextView tv; @Override public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.main);     tv=(TextView)findViewById(R.id.text1);     call(); } public void call() {         try {         SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);         request.addProperty("passonString", "Rajapandian");         SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);         envelope.dotNet=true;         envelope.setOutputSoapObject(request);         HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);         androidHttpTransport.call(SOAP_ACTION, envelope);         Object result = (Object)envelope.getResponse();         tv.setText(result.toString());     } catch (Exception e) {         tv.setText(e.getMessage());         } } 

}

Regards Rajapandian

Referenced by: http://stackoverflow.com/questions/1048310/how-to-call-a-net-web-service-from-android

Connecting to .NET web service from Android

If you are having trouble using .NET Web Services with the Android Platform, you have probably reached to the solution here.

I am here demonstrating the steps using which you can consume data from the .NET web service in your android app. In order to use .Net web Service from your android application you need to first download the ksoap2 android API. Follow the link to download ksoap2 API for android.

After downloading the API, extract the zip file to the file system. Open your Android Project and navigate to the Project Properties. In the project properties go to Java Build Path and say Add External JARs.

Add the reference to the extracted ksoap2-j2se-full-2.1.2.jar file from the downloaded API. You are now ready to use ksoap2 to connect to the .NET web service via Android.

Let’s assume a .NET web service with two methods “Hello World” that returns a string and “Add” that accepts two numbers and returns their sum. Following is the WSDL file of the web service.

From the above WSDL file we get the following Information about the web service:

In order to use this Web Service with our android app:

  1. Open the java file from where you would like to access the Web Service
  2. Include the class library for ksoap2

    import org.ksoap2.*;
    import org.ksoap2.serialization.*;
    import org.ksoap2.transport.*;
    import org.w3c.dom.Text;

  3. Define Web Service Properties in the class

    private static final String NAMESPACE = “http://localhost/TestWebService/&#8221; ;
    private static final String URL = ” http://TestServer/Test/service.asmx&#8221;;
    private static final String HelloWorld_SOAP_ACTION = “http://localhost/TestWebService/HelloWorld&#8221;;
    private static final String METHOD_NAME1 = “HelloWorld”;
    private static final String Add_SOAP_ACTION = “http://localhost/TestWebService/Add&#8221;;
    private static final String METHOD_NAME2 = “Add”;

  4. Add methods to call the web service methods and retrieve the results

    public void GetHelloWorld() {

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME1);
    SoapSerializationEnvelope envelope =
    new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try
    {
    androidHttpTransport.call(HelloWorld_SOAP_ACTION, envelope);
    java.lang.String receivedString = (String)envelope.getResponse();

    }
    catch(Exception e)
    {
    }

    }
    public void GetAdd() {

    SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME2);

    PropertyInfo num1 = new PropertyInfo();
    num1.setName(“a”);
    num1.setValue(5);
    request.addProperty(num1);

    PropertyInfo num2 = new PropertyInfo();
    num2.setName(“b”);
    num2.setValue(9);
    request.addProperty(num2);

    SoapSerializationEnvelope envelope =
    new SoapSerializationEnvelope(SoapEnvelope.VER11);
    envelope.dotNet = true;
    envelope.setOutputSoapObject(request);
    HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

    try
    {
    androidHttpTransport.call(Add_SOAP_ACTION, envelope);
    java.lang.Integer receivedInt = (Integer)envelope.getResponse();

    }
    catch(Exception e)
    {
    }

    }

  5. If you app require access to an array, you can use the following code:

    ArrayList<String> a = new ArrayList<String>();
    try
    {
    androidHttpTransport.call(SOAP_ACTION, envelope);
    java.util.Vector<Object> receivedStrings = (java.util.Vector<Object>)envelope.getResponse();
    if(receivedStrings != null)
    {
    for(Object curStrings : receivedStrings)
    {
    a.add(curStrings.toString());
    }
    }
    }
    catch(Exception e)
    {
    }

Referenced by: http://composedcrap.blogspot.com/2009/08/connecting-to-net-web-service-from.html

Treeview Control – chek child if parent is checked for C#

private void TreeView1_AfterCheck(object sender, System.Windows.Forms.TreeViewEventArgs e)
{
	// For Select all to sub tree
	TreeNode parent_node = e.Node;
	bool is_checked = parent_node.Checked;
	for (int i = 0; i <= e.Node.Nodes.Count - 1; i++) {
		SetSubtreeChecked(parent_node.Nodes(i), is_checked);
	}
	bool blnUncheck = false;

	//---------------------------------------------------

	//Check to see if a parent node exists.

	if ((e.Node.Parent != null)) {
		//Loop through the child nodes.
		foreach (TreeNode child in e.Node.Parent.Nodes) {
			//Check to see if the current node is unchecked.
			if (child.Checked == true) {
				//Set the variable.
				blnUncheck = true;
			}
		}
		//Check the variable.
		if (blnUncheck == false) {
			//Check the parent node.
			e.Node.Parent.Checked = false;
		} else {
			//Uncheck the parent node.
			e.Node.Parent.Checked = true;
		}
	}
}
//'========================================================
//'          For Tree view Items Validation
//'========================================================
private void SetSubtreeChecked(TreeNode parent_node, bool is_checked)
{
	// Set the parent's Checked value.
	parent_node.Checked = is_checked;
	// Set the child nodes' Checked values.
	for (int i = 0; i <= parent_node.Nodes.Count - 1; i++) {
		SetSubtreeChecked(parent_node.Nodes(i), is_checked);
	}
}

Referenced by: http://social.msdn.microsoft.com/Forums/en-US/vssmartdevicesvbcs/thread/f4269ae8-f788-4637-a454-df0f6bf05db5/