Friday, August 21, 2009

WPF Windows Application, WPF Browser Application and Silverlight Clarified

WPF is Microsoft's presentation technology which is based on XAML.
WPF Windows Application in long run will act as a replacement for Windows Forms.
It runs in fuly trusted environment.


Output













WPF Browser Application
WPF Browser Application or XBAP as name suggest is browser based application. But the drawback is the that it required .net runtime present in the running machine.
It runs in partially trusted environment with limited access to system resources.














Silverlight
Silverlight is Microsoft's alternative to Flash like applications. It is based on plug in model.

Thursday, August 13, 2009

Sql Server 200 Generating Script

An Sql Script is a text file containing the database objects like like table, views, stored procedures etc.

Sql Server 2000 Enterprise Manager or Sql Server 2005/2008 Management Studio can be used for generating Sql Script.







Here is the generated script

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK__Trans__ExpenseId__7C8480AE]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)ALTER TABLE [dbo].[Trans] DROP CONSTRAINT FK__Trans__ExpenseId__7C8480AEGO


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[FK__Expense__GroupId__79A81403]') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)ALTER TABLE [dbo].[Expense] DROP CONSTRAINT FK__Expense__GroupId__79A81403GO


if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[spAllExpense]') and OBJECTPROPERTY(id, N'IsProcedure') = 1)drop procedure [dbo].[spAllExpense]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Expense]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[Expense]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[ExpenseGroup]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[ExpenseGroup]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Trans]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)drop table [dbo].[Trans]
GO

CREATE TABLE [dbo].[Expense] ( [ExpenseId] [int] IDENTITY (1, 1) NOT NULL , [GroupId] [int] NULL , [ExpenseName] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Description] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]
GO

CREATE TABLE [dbo].[ExpenseGroup] ( [GroupId] [int] IDENTITY (1, 1) NOT NULL , [GroupName] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL , [Description] [varchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]
GO

CREATE TABLE [dbo].[Trans] ( [TranId] [int] IDENTITY (1, 1) NOT NULL , [ExpenseId] [int] NULL , [Date] [datetime] NULL , [Amount] [numeric](8, 2) NULL , [Description] [varchar] (40) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ) ON [PRIMARY]GO
SET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON
GO

create procedure spAllExpense asselect * from Expense
GOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON
GO

DataGridView - Extracting the Contents of Currently Selected Row

One of the common requirement is CRUD operation is extracting the contents of the currently selected row in a Grid to be updated in TextBoxes or other controls.

SqlConnection cnn;
SqlDataAdapter da;
DataSet ds = new DataSet();
private void Form1_Load(object sender, EventArgs e)
{
cnn = new SqlConnection("Integrated Security=sspi;Initial catalog=Northwind");
cnn.Open();
da = new SqlDataAdapter("select CategoryId, CategoryName, Description from Categories", cnn);
da.Fill(ds, "Cat");
dataGridView1.DataSource = ds.Tables["Cat"];
}



private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
listBox1.Items.Clear();
for (int colindex = 0; colindex < dataGridView1.Columns.Count ; colindex++)
{
listBox1.Items.Add(dataGridView1.Rows[dataGridView1.CurrentRow.Index].Cells[colindex].Value.ToString());
}
}




















DataGridView ComboBox Placing and Child Table Insertion



SqlConnection cnn;
SqlDataAdapter da;
DataSet ds = new DataSet();

SqlCommand cmd;
SqlDataReader dre;

DataGridViewTextBoxColumn newCol;
DataGridViewComboBoxColumn cboCol;

private void Form1_Load(object sender, EventArgs e)
{
cnn = new SqlConnection("Integrated security=sspi;INitial Catalog=ContactManagement;Data Source=.\\shalvin");
cnn.Open();

da = new SqlDataAdapter("select * from ContactGroups", cnn);
da.Fill(ds, "Grp");

CreateGridViewColumns();
}

private void CreateGridViewColumns()
{
cboCol = new DataGridViewComboBoxColumn();
cboCol.DataSource = ds.Tables["Grp"];
cboCol.DisplayMember = "GroupName";
dataGridView1.Columns.Add(cboCol);

newCol = new DataGridViewTextBoxColumn();
dataGridView1.Columns.Add(newCol);

}



private void btnSave_Click(object sender, EventArgs e)
{
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)
{
string strGroupId = "";
DataGridViewRow row = dataGridView1.Rows[i];

string strSql;
strSql = "select GroupId from ContactGroups where GroupName= '"
+ row.Cells[0].Value.ToString() + "'";

cmd = new SqlCommand(strSql, cnn);
dre = cmd.ExecuteReader();
while(dre.Read())
strGroupId = dre["GroupId"].ToString();
dre.Close();


cmd = new SqlCommand("Insert into Contacts (GroupId, ContactName) values ('" + strGroupId + "', '" + row.Cells[1].Value.ToString() + "')", cnn);
cmd.ExecuteNonQuery();
}

MessageBox.Show("Record saved");
}




Related Blog
Windows Forms ListView
Filling Windows Forms ListView with DataTable

Tuesday, August 11, 2009

Seminar on Windows 7, Windows Server 2008 R2 and Microsoft Exchange 2010

Yesterday (August 11 th) there was a seminar on Seminar on Windows 7, Windows Server 2008 R2 and Microsoft Exchange 2010 at The Gateway Hotel, Marine Drive Kochi.
The seminar was highly informative though there was lack of proper promotion for the event. So I missed technology enthsiasts like Praseed Pai, Mathew K J and Biju Alapatt. Thanks to Plaito Williams for informing me about the event.
All the participants received the Windows 7 DVD which expires on June 2010.

Wednesday, July 29, 2009

Visual Studio .Net Setup Wizard Screenshots





















WMI : Extracting Installed Product Information of another machine

For this example to work you should set a reference to System.Management.

using System.Management;

private void Form1_Load(object sender, EventArgs e)
{
ConnectionOptions DemoOptions = new ConnectionOptions();
DemoOptions.Username = "admin";
DemoOptions.Password = "admin";
ManagementScope DemoScope = new ManagementScope("\\\\indiaop1\\root\\cimv2", DemoOptions);
ObjectQuery DemoQuery = new ObjectQuery("select * from win32_Product");
ManagementObjectSearcher DemoSearcher = new ManagementObjectSearcher(DemoScope, DemoQuery);
ManagementObjectCollection AllObjects = DemoSearcher.Get();
foreach (ManagementObject DemoObject in AllObjects)
listBox1.Items.Add(DemoObject["Caption"].ToString());
}

String Formatting Resources

http://blog.stevex.net/index.php/string-formatting-in-csharp/

Query Analyser with C# Beta

































I am not basically a fan of Sql Server 2005 Management Studio mainly because of its compatibility issues and high overhead.


So I thought of creating a simple query tool. Here is the code.




using System.Data.SqlClient;
namespace TreeView_in_Split_Container
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
SqlConnection cnn, cnn2;
SqlCommand cmd;
SqlDataReader dr, dr2;
List<string> glsdb = new List<string>();
string strDbCnn;
private void Form1_Load(object sender, EventArgs e)
{
cnn = new SqlConnection(@"integrated Security=sspi;Initial Catalog=master;Data Source=.\sqlexpress");
cnn.Open();
cmd = new SqlCommand("sp_helpdb",cnn);
dr = cmd.ExecuteReader();
treeView1.Nodes.Add("Database");
while (dr.Read())
{
glsdb.Add(dr["Name"].ToString());
treeView1.TopNode.Nodes.Add(dr["Name"].ToString());
comboBox1.Items.Add(dr["Name"].ToString());
}
dr.Close();
}





Database Combo Selection
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
cnn = new SqlConnection(@"integrated Security=sspi;Data Source=.\sqlexpress;Initial Catalog=" + comboBox1.Text );
cnn.Open();
}


Object Browser


The project make use of two Context Menu dataContext and contextMenuStrip1. First for database related options like Create Database whose functionality is yet to be implemented. second for table related options like Show Table Data.






private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
try
{
TreeNode tempNode = treeView1.GetNodeAt(e.X, e.Y);
treeView1.SelectedNode = tempNode;
if (treeView1.SelectedNode.Level == 1)
{
treeView1.ContextMenuStrip = null;
strDbCnn = (@"integrated Security=sspi;Data Source=.\sqlexpress;Initial Catalog=" + tempNode.Text);
cnn2 = new SqlConnection(strDbCnn);
cnn2.Open();
cmd = new SqlCommand("select * from sysobjects where xtype = 'u'", cnn2);
dr2 = cmd.ExecuteReader();
treeView1.SelectedNode.Nodes.Clear();
while (dr2.Read())
{
treeView1.SelectedNode.Nodes.Add(dr2[0].ToString());
}
cnn2.Close();
dr2.Close();
}
else if (treeView1.SelectedNode.Level == 0)
{
treeView1.ContextMenuStrip =DataContext;
}
else if (treeView1.SelectedNode.Level == 2)
{
treeView1.ContextMenuStrip = contextMenuStrip1;
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message);
}
}



Execute Button Click Code
private void button1_Click(object sender, EventArgs e)
{
try
{
string strSql = "";
if (textBox1.SelectedText != "")
strSql = textBox1.SelectedText;
else
strSql = textBox1.Text;

string strtmp = strSql;
string[] ss = strSql.Split(new char[]{' '});

if(ss[0].Equals("Use"))
{
richTextBox1.Text = "Database context changed to" + ss[1];
return;
}


cmd = new SqlCommand(strSql, cnn);
dr = cmd.ExecuteReader();
richTextBox1.Text = "";
int irf = dr.RecordsAffected;
while (dr.Read())
{

for (int i = 0; i < dr.FieldCount; i++)
{

richTextBox1.Text += dr[i].ToString() + "\t";

}

richTextBox1.Text += "\n";


}
if (irf > 0)
{
richTextBox1.Text += irf + " records affected";
}
dr.Close();
}
catch (Exception ex)
{
richTextBox1.Text += ex.Message.ToString();
}
}



Showing the SELECT result in Grid View


private void showDataToolStripMenuItem_Click(object sender, EventArgs e)
{

DataGrid d1 = new DataGrid();
d1.Dock = DockStyle.Fill;
cnn2 = new SqlConnection(strDbCnn);
cnn2.Open();
SqlDataAdapter adp;
adp = new SqlDataAdapter("select * from " + treeView1.SelectedNode.Text, cnn2);
DataSet ds=new DataSet();
adp.Fill(ds,"T1");
d1.DataSource = ds.Tables["T1"];
cnn2.Close();

splitContainer2.Panel2.Controls.Add(d1);
d1.BringToFront();
}




Windows Generated Code



private void InitializeComponent()
{
this.panel1 = new System.Windows.Forms.Panel();
this.splitContainer1 = new System.Windows.Forms.SplitContainer();
this.splitContainer2 = new System.Windows.Forms.SplitContainer();
this.textBox1 = new System.Windows.Forms.TextBox();
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.btnExecute = new System.Windows.Forms.Button();
this.treeView1 = new System.Windows.Forms.TreeView();
this.label1 = new System.Windows.Forms.Label();
this.comboBox1 = new System.Windows.Forms.ComboBox();
this.DataContext = new System.Windows.Forms.ContextMenuStrip();
this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip();
this.createDatabaseToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.showDataToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.panel1.SuspendLayout();
this.splitContainer1.Panel1.SuspendLayout();
this.splitContainer1.Panel2.SuspendLayout();
this.splitContainer1.SuspendLayout();
this.splitContainer2.Panel1.SuspendLayout();
this.splitContainer2.Panel2.SuspendLayout();
this.splitContainer2.SuspendLayout();
this.DataContext.SuspendLayout();
this.contextMenuStrip1.SuspendLayout();
this.SuspendLayout();
//
// panel1
//
this.panel1.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;
this.panel1.Controls.Add(this.comboBox1);
this.panel1.Controls.Add(this.label1);
this.panel1.Controls.Add(this.btnExecute);
this.panel1.Dock = System.Windows.Forms.DockStyle.Top;
this.panel1.Location = new System.Drawing.Point(0, 0);
this.panel1.Name = "panel1";
this.panel1.Size = new System.Drawing.Size(529, 77);
this.panel1.TabIndex = 0;
//
// splitContainer1
//
this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer1.Location = new System.Drawing.Point(0, 77);
this.splitContainer1.Name = "splitContainer1";
//
// splitContainer1.Panel1
//
this.splitContainer1.Panel1.Controls.Add(this.treeView1);
//
// splitContainer1.Panel2
//
this.splitContainer1.Panel2.Controls.Add(this.splitContainer2);
this.splitContainer1.Size = new System.Drawing.Size(529, 286);
this.splitContainer1.SplitterDistance = 176;
this.splitContainer1.TabIndex = 1;
//
// splitContainer2
//
this.splitContainer2.Dock = System.Windows.Forms.DockStyle.Fill;
this.splitContainer2.Location = new System.Drawing.Point(0, 0);
this.splitContainer2.Name = "splitContainer2";
this.splitContainer2.Orientation = System.Windows.Forms.Orientation.Horizontal;
//
// splitContainer2.Panel1
//
this.splitContainer2.Panel1.Controls.Add(this.textBox1);
//
// splitContainer2.Panel2
//
this.splitContainer2.Panel2.Controls.Add(this.richTextBox1);
this.splitContainer2.Size = new System.Drawing.Size(349, 286);
this.splitContainer2.SplitterDistance = 95;
this.splitContainer2.TabIndex = 0;
//
// textBox1
//
this.textBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.textBox1.Location = new System.Drawing.Point(0, 0);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(349, 95);
this.textBox1.TabIndex = 0;
//
// richTextBox1
//
this.richTextBox1.Dock = System.Windows.Forms.DockStyle.Fill;
this.richTextBox1.Location = new System.Drawing.Point(0, 0);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(349, 187);
this.richTextBox1.TabIndex = 0;
this.richTextBox1.Text = "";
//
// btnExecute
//
this.btnExecute.Location = new System.Drawing.Point(80, 10);
this.btnExecute.Name = "btnExecute";
this.btnExecute.Size = new System.Drawing.Size(75, 23);
this.btnExecute.TabIndex = 1;
this.btnExecute.Text = "&Execute";
this.btnExecute.UseVisualStyleBackColor = true;
this.btnExecute.Click += new System.EventHandler(this.btnExecute_Click);
//
// treeView1
//
this.treeView1.Dock = System.Windows.Forms.DockStyle.Fill;
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(176, 286);
this.treeView1.TabIndex = 0;
this.treeView1.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeView1_NodeMouseClick);
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(185, 20);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(86, 13);
this.label1.TabIndex = 2;
this.label1.Text = "Select Database";
//
// comboBox1
//
this.comboBox1.FormattingEnabled = true;
this.comboBox1.Location = new System.Drawing.Point(277, 10);
this.comboBox1.Name = "comboBox1";
this.comboBox1.Size = new System.Drawing.Size(121, 21);
this.comboBox1.TabIndex = 3;
this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);
//
// DataContext
//
this.DataContext.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.createDatabaseToolStripMenuItem});
this.DataContext.Name = "DataContext";
this.DataContext.Size = new System.Drawing.Size(157, 26);
//
// contextMenuStrip1
//
this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.showDataToolStripMenuItem});
this.contextMenuStrip1.Name = "contextMenuStrip1";
this.contextMenuStrip1.Size = new System.Drawing.Size(153, 48);
//
// createDatabaseToolStripMenuItem
//
this.createDatabaseToolStripMenuItem.Name = "createDatabaseToolStripMenuItem";
this.createDatabaseToolStripMenuItem.Size = new System.Drawing.Size(156, 22);
this.createDatabaseToolStripMenuItem.Text = "Create Database";
//
// showDataToolStripMenuItem
//
this.showDataToolStripMenuItem.Name = "showDataToolStripMenuItem";
this.showDataToolStripMenuItem.Size = new System.Drawing.Size(152, 22);
this.showDataToolStripMenuItem.Text = "Show Data";
this.showDataToolStripMenuItem.Click += new System.EventHandler(this.showDataToolStripMenuItem_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(529, 363);
this.Controls.Add(this.splitContainer1);
this.Controls.Add(this.panel1);
this.Name = "Form1";
this.Text = "Shalvin Sql Server Management Studio";
this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
this.Load += new System.EventHandler(this.Form1_Load);
this.panel1.ResumeLayout(false);
this.panel1.PerformLayout();
this.splitContainer1.Panel1.ResumeLayout(false);
this.splitContainer1.Panel2.ResumeLayout(false);
this.splitContainer1.ResumeLayout(false);
this.splitContainer2.Panel1.ResumeLayout(false);
this.splitContainer2.Panel1.PerformLayout();
this.splitContainer2.Panel2.ResumeLayout(false);
this.splitContainer2.ResumeLayout(false);
this.DataContext.ResumeLayout(false);
this.contextMenuStrip1.ResumeLayout(false);
this.ResumeLayout(false);
}
private System.Windows.Forms.Panel panel1;
private System.Windows.Forms.SplitContainer splitContainer1;
private System.Windows.Forms.SplitContainer splitContainer2;
private System.Windows.Forms.Button btnExecute;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.RichTextBox richTextBox1;
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.ComboBox comboBox1;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ContextMenuStrip DataContext;
private System.Windows.Forms.ContextMenuStrip contextMenuStrip1;
private System.Windows.Forms.ToolStripMenuItem createDatabaseToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem showDataToolStripMenuItem;