Friday, October 30, 2009

Silverlight 3 support in Visual Web Developer 2010 Beta 2

Visual Web Developert 2010 Beta 2 Comes with excellent tool support for Silverlight 3.




Starting With Visual Web Developer 2008 sp1 there is the New Project option in Vwd. So go to File, New Project and Select Silverlight Application.









Let host the Silverlight Application in Asp .Net Application.











If you inspect the Properties Window you can notice that there is both Alphabetical and Categorized View of Entries and also Events. In C# Express Edition 2080 there was only the Categorized View in Properties. I am basically a lover of Alphabetical View.






private void button1_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("Welcome to Silverlight 3");
}






In short Working with Silverlight 3 in Vwd 2010 is as cool as working with Asp .Net Application.

Wednesday, October 14, 2009

Expense Tracking System Sql Script

create database ExpenseTrack

use ExpenseTrack

create table ExpenseGroup(GroupId int primary key, GroupName varchar(40) unique)

insert into ExpenseGroup values (1, 'Food')
insert into ExpenseGroup values (2, 'Clothes')
insert into ExpenseGroup values (3, 'Travelling')

select * from ExpenseGroup

create table Expenses (ExpenseId int primary key,
GroupId int references ExpenseGroup(GroupId),
Expense varchar(40))

insert into Expenses values (1, 1, 'Vegetables')

insert into Expenses values (2, 1, 'Hotel')

insert into Expenses values (3, 2, 'Clothes')

insert into Expenses values (4, 3, 'Petrol')


create table ExpenseTrans (TransId int primary key,
ExpenseId int references Expenses(ExpenseId),
TransDate datetime, Amount numeric(8,2), Description varchar(200))

select * from Expenses

create procedure spGetExpenses as
select G.GroupName, E.Expense from ExpenseGroup G, Expenses E
where G.GroupId = E.GroupId
go

spGetExpenses

insert into ExpenseTrans values (1, 3, '14-Oct-09', 400, 'Bought shirt from Navigator')

insert into ExpenseTrans values (2, 2, '15-Oct-09', 40, 'Had two Masala Dosa from Megha Hotel Kacheripady')
insert into ExpenseTrans values (3, 4, '15-Oct-09', 100, 'Petrol for Rs. 100 from Vaduthal Pump')

select * from ExpenseTrans



create procedure spGetExpenseTrans as
select E.Expense, T.TransDate, T.Amount, T.Description
from Expenses E, ExpensTrans T where E.ExpenseId = T.ExpenseId
go

spGetExpenseTrans


'DbConnect


Imports System.Data.SqlClient
Public Class DbConnect
Public cnn As SqlConnection
Dim ds As New DataSet
Dim da As SqlDataAdapter

Public Sub Open()
cnn = New SqlConnection("Integrated security=sspi;Initial Catalog=ExpenseTrack")
cnn.Open()
End Sub
Public Function GetExpenses() As DataTable
Open()
da = New SqlDataAdapter("select * from Expenses", cnn)
da.Fill(ds, "Expense")
Return ds.Tables("Expense")
End Function
End Class


'frmTransDetial

Imports System.Data.SqlClient
Public Class Form1
Dim dbc As New DbConnect
Dim b As Boolean = False
Dim cmd As SqlCommand
Dim intMax As Integer
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
cboExpense.DataSource = dbc.GetExpenses
cboExpense.DisplayMember = "Expense"
cboExpense.ValueMember = "ExpenseId"
b = True
cmd = New SqlCommand("select Max(TransId) from ExpenseTrans", dbc.cnn)
b = Int32.TryParse(cmd.ExecuteScalar().ToString(), intMax)
intMax += 1
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim strSql As String
strSql = "insert into ExpenseTrans values (" + intMax.ToString + ", "
strSql += cboExpense.SelectedValue.ToString + ", '"
strSql += dtpDate.Value.ToString("d").ToString + "', "
strSql += txtAmount.Text + ", '" + txtRemarks.Text + "')"
cmd = New SqlCommand(strSql, dbc.cnn)
cmd.ExecuteNonQuery()
End Sub
End Class



Windows Generated Code for Expense Transaction Form


Private Sub InitializeComponent()
Me.Label1 = New System.Windows.Forms.Label
Me.dtpDate = New System.Windows.Forms.DateTimePicker
Me.Label2 = New System.Windows.Forms.Label
Me.cboExpense = New System.Windows.Forms.ComboBox
Me.Label3 = New System.Windows.Forms.Label
Me.txtRemarks = New System.Windows.Forms.TextBox
Me.btnSave = New System.Windows.Forms.Button
Me.Label4 = New System.Windows.Forms.Label
Me.txtAmount = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'Label1
'
Me.Label1.AutoSize = True
Me.Label1.Location = New System.Drawing.Point(38, 43)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(30, 13)
Me.Label1.TabIndex = 0
Me.Label1.Text = "Date"
'
'dtpDate
'
Me.dtpDate.CustomFormat = "dd MMM yyyy"
Me.dtpDate.Format = System.Windows.Forms.DateTimePickerFormat.Custom
Me.dtpDate.Location = New System.Drawing.Point(97, 36)
Me.dtpDate.Name = "dtpDate"
Me.dtpDate.Size = New System.Drawing.Size(140, 20)
Me.dtpDate.TabIndex = 1
'
'Label2
'
Me.Label2.AutoSize = True
Me.Label2.Location = New System.Drawing.Point(38, 96)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(48, 13)
Me.Label2.TabIndex = 2
Me.Label2.Text = "Expense"
'
'cboExpense
'
Me.cboExpense.FormattingEnabled = True
Me.cboExpense.Location = New System.Drawing.Point(97, 88)
Me.cboExpense.Name = "cboExpense"
Me.cboExpense.Size = New System.Drawing.Size(121, 21)
Me.cboExpense.TabIndex = 3
'
'Label3
'
Me.Label3.AutoSize = True
Me.Label3.Location = New System.Drawing.Point(38, 180)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(49, 13)
Me.Label3.TabIndex = 4
Me.Label3.Text = "Remarks"
'
'txtRemarks
'
Me.txtRemarks.Location = New System.Drawing.Point(97, 173)
Me.txtRemarks.Multiline = True
Me.txtRemarks.Name = "txtRemarks"
Me.txtRemarks.Size = New System.Drawing.Size(178, 48)
Me.txtRemarks.TabIndex = 5
'
'btnSave
'
Me.btnSave.Location = New System.Drawing.Point(97, 251)
Me.btnSave.Name = "btnSave"
Me.btnSave.Size = New System.Drawing.Size(75, 23)
Me.btnSave.TabIndex = 6
Me.btnSave.Text = "Save"
Me.btnSave.UseVisualStyleBackColor = True
'
'Label4
'
Me.Label4.AutoSize = True
Me.Label4.Location = New System.Drawing.Point(38, 141)
Me.Label4.Name = "Label4"
Me.Label4.Size = New System.Drawing.Size(43, 13)
Me.Label4.TabIndex = 7
Me.Label4.Text = "Amount"
'
'txtAmount
'
Me.txtAmount.Location = New System.Drawing.Point(97, 134)
Me.txtAmount.Name = "txtAmount"
Me.txtAmount.Size = New System.Drawing.Size(100, 20)
Me.txtAmount.TabIndex = 8
'
'Form1
'
Me.AutoScaleDimensions = New System.Drawing.SizeF(6.0!, 13.0!)
Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
Me.ClientSize = New System.Drawing.Size(372, 321)
Me.Controls.Add(Me.txtAmount)
Me.Controls.Add(Me.Label4)
Me.Controls.Add(Me.btnSave)
Me.Controls.Add(Me.txtRemarks)
Me.Controls.Add(Me.Label3)
Me.Controls.Add(Me.cboExpense)
Me.Controls.Add(Me.Label2)
Me.Controls.Add(Me.dtpDate)
Me.Controls.Add(Me.Label1)
Me.Name = "Form1"
Me.Text = "Expense Trans"
Me.ResumeLayout(False)
Me.PerformLayout()
End Sub
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents dtpDate As System.Windows.Forms.DateTimePicker
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents cboExpense As System.Windows.Forms.ComboBox
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents txtRemarks As System.Windows.Forms.TextBox
Friend WithEvents btnSave As System.Windows.Forms.Button
Friend WithEvents Label4 As System.Windows.Forms.Label
Friend WithEvents txtAmount As System.Windows.Forms.TextBox


Related Blog

Simple Contact Management System In C# and VB .Net

Simple Contact Management System In C# and VB .Net

create database ContactManagement

use ContactManagement

create table ContactGroups(GroupId int primary key, GroupName varchar(40))

select * from ContactGroups

insert into ContactGroups values (1, 'Students')
insert into ContactGroups values (2, 'Relatives')

create table Contacts (ContactId int primary key, GroupId int references ContactGroups(GroupId), ContactName varchar(40), Phone varchar(15), Email varchar(40))

insert into Contacts values (1, 1, 'Sumesh S', '9961321010', 'mail4sumo@yahoo.co.in')

insert into Contacts values (2, 1, 'Nithin T M', '9446737393', 'nithintm@gmail.com')

insert into Contacts values (3, 2, 'Shalvin', '998765778', 'shalvin@gmail.com')

select * from Contacts

select G.GroupName, C.ContactName, C.Phone, C.Email from
ContactGroups G, Contacts C where G.GroupId = C.GroupId













































I am adding a class called DbConnect which is to contain Database Connectivity and retrieval code.

using System.Data;
using System.Data.SqlClient;
namespace ContactManagementSystem
{

class DbConnect
{
public SqlConnection cnn;
public void Open()
{
cnn = new SqlConnection("Integrated Security=sspi;Initial Catalog=ContactManagement");
cnn.Open();
}

public DataTable GetGroups()
{
Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("select * from ContactGroups", cnn);
da.Fill(ds, "Grp");
return ds.Tables["Grp"];
}
}

frmGroups
























Please visit my blog Windows Forms Input Validation if your are not familiar with Validation event and ErrorProvider Control.


using System.Data.SqlClient;
DbConnect dbc = new DbConnect();
Boolean b = false;
int intMax = 0;
SqlCommand cmd;

private void frmGroups_Load(object sender, EventArgs e)
{
dbc.Open();
cmd = new SqlCommand("select Max(GroupId) from ContactGroups", dbc.cnn);
b = Int32.TryParse(cmd.ExecuteScalar().ToString(), out intMax);
intMax += 1;

ShowData();
}

private void ShowData()
{
dgvGroups.DataSource = dbc.GetGroups();

}


private void btnAdd_Click(object sender, EventArgs e)
{
cmd = new SqlCommand("insert into ContactGroups values (" + intMax.ToString() + ", '" + txtContactGroup.Text + "')", dbc.cnn);
cmd.ExecuteNonQuery();
MessageBox.Show("Record Saved");
ShowData();
}



private void txtContactGroup_Validating(object sender, CancelEventArgs e)
{
if (txtContactGroup.Text == "")

{
errorProvider1.SetError(txtContactGroup, "Group Name cannot be blank");
txtContactGroup.Focus();
}
else
{
errorProvider1.SetError(txtContactGroup, "");
}

}

Obviously creating an sql statement for passing to SqlCommand is not the right approach. But let me keep it simple and short and not end up in feature creep. I will take up the better approach in a later blog.

frmContact
























Now let's come to the child table data insertion.

using System.Data.SqlClient;

DbConnect dbc = new DbConnect();
SqlCommand cmd;
Boolean b = false;
int intMax = 0;

private void frmContacts_Load(object sender, EventArgs e)
{
dbc.Open();
cboGroup.DataSource = dbc.GetGroups();
cboGroup.DisplayMember = "GroupName";
cboGroup.ValueMember = "GroupId";
}


private void btnSave_Click(object sender, EventArgs e)
{
dbc.Open();
cmd = new SqlCommand("select Max(ContactId) from Contacts", dbc.cnn);
b = Int32.TryParse(cmd.ExecuteScalar().ToString(), out intMax);
intMax += 1;

cmd = new SqlCommand("insert into Contacts values (" + intMax.ToString() + ", " + cboGroup.SelectedValue.ToString() + ", '" + txtContact.Text + "', '" + txtPhone.Text + "', '" + txtEmail.Text + "')", dbc.cnn);
cmd.ExecuteNonQuery();
MessageBox.Show("Record Saved");
}


VB .Net Code

'DbConnect - Class Module

Imports System.Data.SqlClient
Public Class DbConnect
Public cnn As SqlConnection

Public Sub Open()
cnn = New SqlConnection("Integrated Security=sspi;Initial Catalog=ContactManagement")
cnn.Open()
End Sub

Public Function GetGroups() As DataTable
Open()
Dim da As New SqlDataAdapter("select * from ContactGroups", cnn)
Dim ds As New DataSet
da.Fill(ds, "Grp")
Return ds.Tables("Grp")
End Function

Public Function GetContacts() As DataTable
Open()
Dim strSql As String
strSql = "select G.GroupName, C.ContactName, C.Phone, C.Email " strSql += "from ContactGroups G, Contacts C where G.GroupId = C.GroupId"
Dim da As New SqlDataAdapter(strSql, cnn)
Dim ds As New DataSet
da.Fill(ds, "Grp")
Return ds.Tables("Grp")
End Function

End Class

'frmMain

Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click
Close()
End Sub

Private Sub GroupsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GroupsToolStripMenuItem.Click
Dim fg As New frmGroups
fg.MdiParent = Me fg.Show()
End Sub

Private Sub ContactsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ContactsToolStripMenuItem.Click
Dim fc As New frmContacts
fc.MdiParent = Me fc.Show()
End Sub


'frmContactGroups

Imports System.Data.SqlClient

Public Class frmGroups

Dim dbc As New DbConnect
Dim cmd As SqlCommand
Dim intMax As Integer
Dim b As Boolean
Dim be As Boolean = False
Private Sub frmGroups_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load dbc.Open()
IncrementId()
FillGroups()
End Sub

Private Sub IncrementId()
cmd = New SqlCommand("select Max(GroupId) from ContactGroups", dbc.cnn)
b = Int32.TryParse(cmd.ExecuteScalar().ToString(), intMax)
intMax += 1
End Sub

Private Sub FillGroups()
lstGroups.DataSource = dbc.GetGroups
lstGroups.DisplayMember = "GroupName"
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If be = True Then IncrementId()
End If
cmd = New SqlCommand("insert into ContactGroups values (" + intMax.ToString + ", '" + txtGroup.Text + "')", dbc.cnn)
cmd.ExecuteNonQuery()
MessageBox.Show("Record Saved")
FillGroups()
BlankControls()
be = True
End Sub

Private Sub BlankControls()
txtGroup.Text = "" txtGroup.Focus()
End Sub
End Class


'frmContacts
Imports System.Data.SqlClient

Public Class frmContacts

Dim dbc As New DbConnect
Dim cmd As SqlCommand
Dim intMax As Integer
Dim b As Boolean
Dim be As Boolean = False

Private Sub frmContacts_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
FillGroupsCombo()
IncrementId()
FillContactsGrid()
End Sub

Private Sub FillGroupsCombo()
cboGroups.DataSource = dbc.GetGroups
cboGroups.DisplayMember = "GroupName"
cboGroups.ValueMember = "GroupId"
End Sub

Private Sub IncrementId()
cmd = New SqlCommand("select Max(ContactId) from Contacts", dbc.cnn)
b = Int32.TryParse(cmd.ExecuteScalar().ToString(), intMax)
intMax += 1
End Sub

Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
If be = True Then IncrementId()
End If
cmd = New SqlCommand("insert into Contacts values (" + intMax.ToString() + ", " + cboGroups.SelectedValue.ToString + ", '" + txtContact.Text + "', '" + txtPhone.Text + "', '" + txtEmail.Text + "')", dbc.cnn)
cmd.ExecuteNonQuery()
MessageBox.Show("Record Saved")
FillContactsGrid()
be = True
End Sub

Private Sub FillContactsGrid()
DataGridView1.DataSource = dbc.GetContacts
End Sub

Related Blog
Contact Management System VB .Net Windows Generated Code

Tuesday, October 13, 2009

Microsoft Community Tech Day - 24th Oct at ParkCenter, Technopark, Trivandrum

http://k-mug.org/content/ctd09.aspx

Microsoft Community Tech Day - 24th Oct at ParkCenter, Technopark, Trivandrum

Technical Sessions

09.45AM-10.30AM Rapid Prototyping - Lessons from the trenches
10.30AM-11.30AM SQL Server - Tips and Tricks
Tea Break
11.45AM-12.30PM Language Integrated Query (LINQ)
12.30PM-01.30PM Writing Secure Code
Lunch Break
02.30PM-03.30PM Windows Azure & Clouds
03.30PM-04.15PM Windows Presentation Foundation (WPF)

04.15PM-05.00PM Coding For Fun

Tuesday, October 6, 2009

C# Delegates


     
 If you click a Button on a form, the program would call a specific method. It is this pointer which is a delegate. Delegates are good because you can notify several methods that an event has occurred, if you so wish. The delegates are called as type safe because the signature of delegate must match the signature of the function the delegate points to.
A delegate is similar to class in that you create an instance of delegate and pass the function name as parameter to the the delegate constructor. It is this function that the delegate will point to.
A delegate signature is similar to the signature of a function except that delegate signature contains delegate key word

.Net is coming packed with EventHandler delegate. 
public delegate void EventHandler(object? sender, EventArgs e);
 It expects two parameters object and EventArgs. object can be used to inspect which control invoked the event handler. EventArgs represent the arguments of the event.
+= operator is user to wire up an Event handler to an event.

using System;
using System.Windows.Forms;
using System.Drawing;

class frmHello : Form
{
Button btn;
public static void Main()
{
Application.Run(new frmHello());
}
public frmHello()
{
this.Text = "shalvin.com";
btn = new Button();
btn.Location = new Point(50, 50);
btn.Text = "Hello";
btn.Click += new EventHandler(btn_Click);
this.Controls.Add(btn);
}
private void btn_Click(Object sender, EventArgs e)
{
MessageBox.Show("Welcome to C#");
}
}
















Delegate Inference


btnDelInf.Click += BtnDelInf_Click;
private void BtnDelInf_Click(object sender, System.EventArgs e)
{
    MessageBox.Show("Delegate Inference");
}

Mentioning the EvenHandler delegate along with event wiring is not not mandatory.


MenuStrip
using System;
using System.Windows.Forms;
using System.Drawing;

class frmHello : Form
{

MenuStrip menuStrip1;
ToolStripMenuItem fileToolStripMenuItem;
ToolStripMenuItem examplesToolStripMenuItem;
ToolStripMenuItem exitToolStripMenuItem;

public static void Main()
{
Application.Run(new frmHello());

}

public frmHello()
{
this.Load += new EventHandler(Form1_Load);
}
private void Form1_Load(object sender, EventArgs e)
{
this.menuStrip1 = new MenuStrip();
this.Controls.Add(menuStrip1);
fileToolStripMenuItem = new ToolStripMenuItem();
fileToolStripMenuItem.Text = "File";
menuStrip1.Items.Add(fileToolStripMenuItem);

examplesToolStripMenuItem = new ToolStripMenuItem();
examplesToolStripMenuItem.Text = "Examples";
menuStrip1.Items.Add(examplesToolStripMenuItem);

exitToolStripMenuItem = new ToolStripMenuItem();
exitToolStripMenuItem.Text = "Exit";
fileToolStripMenuItem.DropDownItems.Add("Exit");
}

}

ListBox


using System.Windows.Forms;
using System;
using System.Drawing;

namespace ConsoleApplication1
{
class Program : Form 
{
    Label lbl, lblStudents;
    TextBox txtName;
    Button btn;
    ListBox lstStudents;

    static void Main(string[] args)
    {
        Application.Run(new Program());
    }
    public Program()
    {
        this.Text = "Shavin.net";

        lblStudents = new Label { Text = "2012", Location = new Point(50, 50) };
        this.Controls.Add(lblStudents);

        lstStudents = new ListBox { Location = new Point(150, 50) };
        this.Controls.Add(lstStudents);

        lbl = new Label { Text = "Name", Location = new Point(50, 150) };
        this.Controls.Add(lbl);

        txtName = new TextBox{Location = new Point(150, 150)};
        this.Controls.Add(txtName);
       

        btn = new Button();
        btn.Text = "Add";
        btn.Location = new Point(100, 200);
        btn.Click +=new EventHandler(btn_Click);
        this.Controls.Add(btn);
    }
    private void btn_Click(object sender, EventArgs e)
    {
        lstStudents.Items.Add(txtName.Text);
        txtName.Text = "";
        txtName.Focus();
    }
}
}


Mono