Sunday, December 8, 2013

Load treeview from datasource with hardcoded root nodes on treeview (Windows Forms C#)



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data.SqlClient;


namespace ReportBuilder
{
    public partial class MainForm : Form
    {
        TreeNode parent = null;
        TreeNode child = null;
        TreeNode grandchild = null;      
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string groupmasterparent = "select GroupID,GroupDesc as ParentGroupDesc,ParentGroupID from aGroupMaster where AccountType='A' and ParentGroupID is null";
            SqlCommand groupmasterparentcmd = new SqlCommand(groupmasterparent, DBConnection.Getconnection());
            groupmasterparentcmd.CommandType = CommandType.Text;
            SqlDataAdapter groupmasterparentda = new SqlDataAdapter(groupmasterparentcmd);        
            DataSet ds = new DataSet();          
            groupmasterparentda.Fill(ds, "groupmasterparentdetails");          
            foreach (DataRow dr in ds.Tables["groupmasterparentdetails"].Rows)
            {              
                parent = treeView1.Nodes[0].Nodes.Add(dr["ParentGroupDesc"].ToString());
                PopulateChildinTreeView(Convert.ToInt32(dr["GroupID"].ToString()), parent);
            }          
        }
        private void PopulateChildinTreeView(int parentID, TreeNode parent)
        {                                
            string groupmaster = "select GroupID,GroupDesc,ParentGroupID from aGroupMaster where ParentGroupID="+parentID+"";          
            SqlCommand groupmastercmd = new SqlCommand(groupmaster, DBConnection.Getconnection());          
            groupmastercmd.CommandType = CommandType.Text;          
            SqlDataAdapter groupmasterda = new SqlDataAdapter(groupmastercmd);          
            DataSet ds = new DataSet();
            groupmasterda.Fill(ds, "groupmasterdetails");  
            if (ds.Tables["groupmasterdetails"].Rows.Count > 0)
            {
                foreach (DataRow drChild in ds.Tables["groupmasterdetails"].Rows)
                {
                    if (parent == null)
                        child = treeView1.Nodes[0].Nodes.Add(drChild["GroupDesc"].ToString());
                    else
                        child = parent.Nodes.Add(drChild["GroupDesc"].ToString());
                    PopulateChildinTreeView(Convert.ToInt32(drChild["GroupID"].ToString()), child);
                }              
            }
            else
                PopulateGrandChildinTreeView(parentID, parent);
        }

        private void PopulateGrandChildinTreeView(int childID, TreeNode child)
        {                      
            string accountmaster = "select * from aAccountMaster where groupid="+childID+"";          
            SqlCommand accountmastercmd = new SqlCommand(accountmaster, DBConnection.Getconnection());          
            accountmastercmd.CommandType = CommandType.Text;          
            SqlDataAdapter accountmasterda = new SqlDataAdapter(accountmastercmd);
            DataSet ds = new DataSet();          
            accountmasterda.Fill(ds, "accountmasterdetails");
            foreach (DataRow drChild in ds.Tables["accountmasterdetails"].Rows)
            {
                if (child == null)
                    grandchild = parent.Nodes.Add(drChild["AccountDesc"].ToString());
                else
                    grandchild = child.Nodes.Add(drChild["AccountDesc"].ToString());
            }
        }
    }
}

Load DeveExpress treelistview from datasource with hardcoded root nodes on treelistview (Windows Forms in C#)



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;
using System.Data.SqlClient;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList.Columns;


namespace ReportBuilder
{
    public partial class MainForm : Form
    {      
        TreeListNode rootnodeAsset = null;
        TreeListNode rootnodeLiability = null;
        TreeListNode rootnodeIncome = null;
        TreeListNode rootnodeExpense= null;
        TreeListNode ParentNode = null;
        TreeListNode ChildNode = null;
        TreeListNode GrandChildNode = null;
        public MainForm()
        {
            InitializeComponent();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            string groupmasterparent = "select GroupID,GroupDesc as ParentGroupDesc,ParentGroupID from aGroupMaster where AccountType='A' and ParentGroupID is null";
            SqlCommand groupmasterparentcmd = new SqlCommand(groupmasterparent, DBConnection.Getconnection());
            groupmasterparentcmd.CommandType = CommandType.Text;
            SqlDataAdapter groupmasterparentda = new SqlDataAdapter(groupmasterparentcmd);        
            DataSet ds = new DataSet();          
            groupmasterparentda.Fill(ds, "groupmasterparentdetails");
            rootnodeAsset = trlstGroupMaster.AppendNode(new object[] { "Asset" }, null);
            rootnodeLiability = trlstGroupMaster.AppendNode(new object[] { "Liability" }, null);
            rootnodeIncome = trlstGroupMaster.AppendNode(new object[] { "Income" }, null);
            rootnodeExpense = trlstGroupMaster.AppendNode(new object[] { "Expense" }, null);
            foreach (DataRow dr in ds.Tables["groupmasterparentdetails"].Rows)
            {
                ParentNode = trlstGroupMaster.AppendNode(new object[] { dr["ParentGroupDesc"].ToString() }, rootnodeAsset);
                dxPopulateChildinTreeView(Convert.ToInt32(dr["GroupID"].ToString()), ParentNode);              
            }
                       
        }
        private void dxPopulateChildinTreeView(int parentID, TreeListNode ParentNode)
        {
            string groupmaster = "select GroupID,GroupDesc,ParentGroupID from aGroupMaster where ParentGroupID=" + parentID + "";
            SqlCommand groupmastercmd = new SqlCommand(groupmaster, DBConnection.Getconnection());
            groupmastercmd.CommandType = CommandType.Text;
            SqlDataAdapter groupmasterda = new SqlDataAdapter(groupmastercmd);
            DataSet ds = new DataSet();
            groupmasterda.Fill(ds, "groupmasterdetails");  
            if (ds.Tables["groupmasterdetails"].Rows.Count > 0)
            {
                foreach (DataRow drChild in ds.Tables["groupmasterdetails"].Rows)
                {
                    if (ParentNode == null)
                        ChildNode = trlstGroupMaster.AppendNode(new object[] { drChild["GroupDesc"].ToString() }, rootnodeAsset);
                    else
                        ChildNode = trlstGroupMaster.AppendNode(new object[] { drChild["GroupDesc"].ToString() }, ParentNode);
                    dxPopulateChildinTreeView(Convert.ToInt32(drChild["GroupID"].ToString()), ChildNode);
                }
            }
            else
                dxPopulateGrandChildinTreeView(parentID, ParentNode);
        }

        private void dxPopulateGrandChildinTreeView(int childID, TreeListNode ChildNode)
        {
            string accountmaster = "select * from aAccountMaster where groupid=" + childID + "";
            SqlCommand accountmastercmd = new SqlCommand(accountmaster, DBConnection.Getconnection());
            accountmastercmd.CommandType = CommandType.Text;
            SqlDataAdapter accountmasterda = new SqlDataAdapter(accountmastercmd);
            DataSet ds = new DataSet();
            accountmasterda.Fill(ds, "accountmasterdetails");
            foreach (DataRow drGrandChild in ds.Tables["accountmasterdetails"].Rows)
            {
                if (ChildNode == null)
                    GrandChildNode = trlstGroupMaster.AppendNode(new object[] { drGrandChild["AccountDesc"].ToString() }, ParentNode);
                else
                    GrandChildNode = trlstGroupMaster.AppendNode(new object[] { drGrandChild["AccountDesc"].ToString() }, ChildNode);
            }
        }
    }          
}



Saturday, March 16, 2013

Sunday, March 10, 2013

SQL Backup using Query



USE master

GO


BACKUP DATABASE [DBNAME] TO DISK N'E:\DBBackup\Backup_10032013'
WITH NOFORMAT , INIT, NAME = N'DB Full Database Backup'SKIP, NOREWIND, NOUNLOAD, STATS = 10  

GO 

Tuesday, January 29, 2013

How to hide a Contentpane or any Controls in FormAlpha on a Button click event of FormBeta which loads as a dialog box on top of FormAlpha


Two steps are needed:
First, Form Alpha needs a public Fucntion called "hidePane()" whose sole purpose is to hide the Pane.
Second, Form Beta needs a reference to Form Alpha. I asume that Alpha creates generates Beta and shows it. In this case give Beta a constructor that takes a FormAlpha as parameter:
//Variable to store wich form created this form
FormAlpha creatorForm;

public FormBeta (FormAlpha Creator){
  InitializeComponent();
  creatorForm = Creator;
}
Instead of using the normal empty constructor to create FormBeta:
Form otherForm = new FormBeta();
otherForm.show();
use the newly created one and give it a Reference to Alpha with "this":
Form otherForm = new FormBeta(this);
otherForm.Show();
now all your ButtonClick even has to do is call the hidePanle():
creatorForm.hidePanel();
P.S. if you want to be able to unhide he Panel as well, we may have to change step 1 a little.

Sunday, January 27, 2013

How To: Reset Identity column in SQL Server


How To: Reset Identity column in SQL Server

This is one of those simple tip posts that may seem obvious and taken for granted by those of us who have been working with SQL Server for a while now but maybe a newbie or two out there will find this helpful.
Every so often (just this morning!) I find myself resetting an identity column value back to 0 after I've deleted all the existing records so the table gets a fresh start at primary key 1. Yes, I know all about primary keys not changing and how the value in the primary key doesn't matter and so on. Sometimes I just like the primary keys starting at 1.
The following line resets the Identity value for the Customer table to 0 so that the next record added starts at 1.

DBCC CHECKIDENT('Customer', RESEED, 0)

Have a nice day. :-|