Thursday, March 26, 2009

Populate Treeview With Database Fields

This post describes how to populate treeview with database fields. People often required to populate treeview with database fields and they choose different approaches to make it work. Here I am going to describe the simplest way.

Here I'll be using Sql Server Sample Database: Pubs

Relations:

Treeview :


VB Code :

Public Sub GenerateTreeView()
Dim ds As New DataSet
Dim conn As New SqlConnection
Dim connString As String
Dim strQuery As String
Dim da As SqlDataAdapter
Dim dtJob As New DataTable
Dim dtEmp As New DataTable
Dim nodeJob As TreeNode
Dim nodeEmp As TreeNode

Try

connString = ConfigurationManager.ConnectionStrings("pubsConnectionString").ToString()

strQuery = "select job_id,job_desc from jobs"
da = New SqlDataAdapter(strQuery, connString)
da.Fill(dtJob)
dtJob.TableName = "Job"
ds.Tables.Add(dtJob)


strQuery = "SELECT emp_id, fname, lname, job_id FROM employee ORDER BY job_id"
da = New SqlDataAdapter(strQuery, connString)
da.Fill(dtEmp)
dtEmp.TableName = "Employee"
ds.Tables.Add(dtEmp)

'define RelationShip
ds.Relations.Add("JobEmployee", ds.Tables("Job").Columns("job_id"),
ds.Tables("Employee").Columns("job_id"))

For Each dr As DataRow In ds.Tables("job").Rows 'Parent Nodes
nodeJob = New TreeNode()
nodeJob.Value = dr("job_id")
nodeJob.Text = dr("job_desc")
nodeJob.ShowCheckBox = True
nodeJob.Expanded = False
tvSample.Nodes.Add(nodeJob)
For Each drr As DataRow In dr.GetChildRows("JobEmployee") 'Child Nodes
nodeEmp = New TreeNode()
nodeEmp.Text = drr("fname") & " " & drr("lname")
'here concating parent node value with child node value
'so that it will be easy to get the selected node value
nodeEmp.Value = drr("emp_id") & "$" & dr("job_id")
nodeEmp.ShowCheckBox = True
nodeJob.ChildNodes.Add(nodeEmp)
Next
Next

Catch ex As Exception
strQuery = Nothing
da = Nothing
conn = Nothing
connString = Nothing
Finally
End Try
End Sub
C# Code:

public void GenerateTreeView()
{
DataSet ds = new DataSet();
SqlConnection conn = new SqlConnection();
string connString = null;
string strQuery = null;
SqlDataAdapter da;
DataTable dtJob = new DataTable();
DataTable dtEmp = new DataTable();
TreeNode nodeJob;
TreeNode nodeEmp;

try {

connString =
ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString.ToString();

//Job Table Data
strQuery = "select job_id,job_desc from jobs";
da = new SqlDataAdapter(strQuery, connString);
da.Fill(dtJob);
dtJob.TableName = "Job";
ds.Tables.Add(dtJob);

//Employee Table Data
strQuery = "SELECT emp_id, fname, lname, job_id FROM employee ORDER BY job_id";
da = new SqlDataAdapter(strQuery, connString);
da.Fill(dtEmp);
dtEmp.TableName = "Employee";
ds.Tables.Add(dtEmp);

//define RelationShip
ds.Relations.Add("JobEmployee", ds.Tables["Job"].Columns["job_id"],
ds.Tables["Employee"].Columns["job_id"]);

foreach (DataRow dr in ds.Tables["job"].Rows) {
nodeJob = new TreeNode();
nodeJob.Value = dr["job_id"].ToString();
nodeJob.Text = dr["job_desc"].ToString();
nodeJob.ShowCheckBox = true;
nodeJob.Expanded = false;
tvSample.Nodes.Add(nodeJob);
foreach (DataRow drr in dr.GetChildRows("JobEmployee") ) {
nodeEmp = new TreeNode();
nodeEmp = new TreeNode();
nodeEmp.Text = drr["fname"].ToString() + drr["lname"].ToString();
nodeEmp.ShowCheckBox = true;
nodeJob.ChildNodes.Add(nodeEmp);
}
}
}
catch (Exception ex) {
strQuery = null;
da = null;
conn = null;
connString = null;
}

finally {
}

}


}
In my next posts i will write about javascript that will be useful to check/ uncheck all the checkboxes of treeview and how to retrieve value of all the checked nodes server side and client side.

I hope this helps :)

Sunday, March 15, 2009

Ubuntu 8.10, blank screen shows after login screen

Recently i installed Ubuntu 8.10 (Intrepid Ibex) on my machine. After loging in i got only brown blank screen and i couldn't do anything except moving mouse around the screen. I thought i didn't installed it properly, so i reinstalled it twice. Finally i found out the solution. It happened because graphic card can not handle the compiz effects.

There is a bug in the Intel Video driver for the Intel 830MG and 845G video cards. Desktop effects with Compiz will not work on those chips and freeze the system. During that time
keyboard too won't work (meaning Numlock doens't work), just to be sure, are you able to get to consoles (ctrl+alt+f1)? If that too doesn't work, it's probably your kernel crashing.

To fix the problem you need to remove the Compiz, here's how:

1. From the Grub boot menu slect the “recovery mode” option.

2. Select “root Drop to root shell prompt”

Type the following at the prompt:

3. sudo apt-get remove compiz

4. sudo apt-get remove compiz-core

5. exit

6. Select “resume Resume normal boot”

Or you can use the following trick:

1. Select FailSafe Terminal session from the options at Login screen and Login with your Username and Password.

2. Type gconf-editor. It will open configuration editor window, from it select

desktop->gnome->application->window_manager.

3. From right side of the window edit the value for current and default by removing "usr/bin/compiz" with "usr/bin/metacity" and reboot.

4. Select Gnome session and Login with your Username and Password.

That's it!! :)