Referencing a User Control in a WPF Application

Currently I am working on a WPF Tool which gets from Database and Excel onto a grid view, modifies it and saves it , I created a User control so as to use in my app . I wanted to use this usercontrol in my Mainwindow.xaml view ... But it was as easy as we do in asp.net....

Consider I have a Usercontrol which i added in my project as follows 

<UserControl x:Class="Hoverhelpinputter.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             mc:Ignorable="d"
            d:DesignHeight="385" Height="541" Width="927">
    <Grid Width="889" Height="545">
       
        <DataGrid  SelectionChanged="DataGrid2_SelectionChanged" AutoGenerateColumns="True" ItemsSource="{Binding}" Name="DataGrid2" Margin="0,55,0,12" Grid.ColumnSpan="3" Grid.RowSpan="4">
           
        </DataGrid>

    </Grid>
</UserControl>

and than I have my Mainwindow.xaml view as follows

<Window xmlns:my="clr-namespace:Smith.WPF.HtmlEditor;assembly=Smith.WPF.HtmlEditor"
        x:Class="Hoverhelpinputter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"  Height="686" Width="1094">

<Grid Width="994">

 </Grid>

</Window>

 xmlns:local="clr-namespace:Hoverhelpinputter"   

The xmlns:local attribute of the Window element, maps the xml namespace localto the Hoverhelpinputter namespace. This is the syntax:

xmlns:{name}="clr-namespace:{namespace};assembly={assembly}"
so add this attribute in the mainwindow.xaml view as below and call it  from the mainwindow through   <local:UserControl1 />


<Window xmlns:my="clr-namespace:Smith.WPF.HtmlEditor;assembly=Smith.WPF.HtmlEditor"
        xmlns:local="clr-namespace:Hoverhelpinputter" 
        x:Class="Hoverhelpinputter.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow"  Height="686" Width="1094">

<Grid Width="994">

<local:UserControl1 />

 </Grid>

</Window>

Checking out files in TFS using C#

Recently I was working on a Project which involved grabbing a file from my local folder in C Drive . Modifying the file though my WPF App using an HTML Editor and than saving the file back to the same folder. In the process however,I realized that when i did any modifications to the file, it wasn't checking the file out ( was using TFS).
  So finally I took out my glasses and went on my favorite internet search engine to google for "Checking out files in TFS using C#", and WOW, there I see my solution right in front of my eye

Here is the code for it



  var workspaceInfo = Workstation.Current.GetLocalWorkspaceInfo(path);
  var server = new TfsTeamProjectCollection(workspaceInfo.ServerUri);
  var workspace = workspaceInfo.GetWorkspace(server);
  workspace.PendEdit(path);



Make sure to add the following references to your project

using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;

SQL Query to search DateTime Value

SELECT * FROM dbo.UserAction
WHERE CONVERT(CHAR(10),ActionDtTm,120) = '2011-10-04'


SELECT * FROM dbo.UserAction
WHERE ActionDtTm >= '2011-10-04' AND ActionDtTm < '2011-10-06'

Schemabinding in SQL Views

To first understand the word Schema Binding .. let us first know where we use it .... I am sure we are all aware abotu SQL Views... View is nothing but a Virtual Table in SQL, which is created for many purposes , eg simplify stuff, security of data , less space required.... etc

Let us  consider a DBA created a View called vw_table1 for a table dbo.table1 in Database without schemabinding option..  and later after sometimes..someone tries to add a new column in the table.. the table gets changed, however the View will break . you might have to drop and recreate the view in this case .
In order to avoid this situation we make use of ScehmaBinding .. Schema binding will lock the table preventing any changes that may change the table schema.

View with Schemabinding syntax

CREATE VIEW  vw_table1  WITH
 schemabinding AS
Select Id,Number,Address
FROM dbo.Employees
GO

Debugging the QuickWatch style

Recently while Debugging I got to learn about this new technique ,so as to examine variable , its values and expressions. It is basically a Dialogue box which is very useful for Developers while finding bugs in softwares and fixing them.

So how do we use it in Visual Studio. 
 1. This feature can be used while we work in Break mode .. (BreakPoint Mode)
 2. Now while in Breakpoint mode , right click on the expression and choose QuickWatch . This          will open up a QuickWatch box
 3. You will see the Value, Name and Datatype for this variable to expression 

The following image would give more insight


once user click on Qucikwatch the dialogue box opens up as follows



   

Serializing an object to XML using C#

Hi All, Recently did I work on Serialization of Objects in my project and understood that it was quite easy to do so.. To sum it up , I will just wirte the code in here .

class Program

    {
        static void Main(string[] args)

        {
            Person p = new Person();
            p.FirstName = "Priti";
            p.MI = "h";
            p.LastName = "Desai";
            p.zipcode = 32045;

            //  XmlSerializer class that serializes an object to XML. When you create an   
               instance of  XmlSerializer, you pass the type of the class that you want to
              serialize into its constructor:

            System.Xml.Serialization.XmlSerializer x = new
            System.Xml.Serialization.XmlSerializer(p.GetType());

            //Serialize is overloaded and can send output to a TextWriter, Stream, or    
            XMLWriter object

            StreamWriter writer = new StreamWriter("c:/p.xml");
            x.Serialize(Console.Out, p);
            x.Serialize(writer, p);
            Console.WriteLine();
            Console.ReadLine();
        }

    }

    public class Person

    {
        public string FirstName;
        public string MI;
        public string LastName;
        public int zipcode;
    }

Clearing Controls in ASP.NET


Just a Day back, we were assigned a Task where in we had a situation, where we load the data in an asp.net form, save that form and open another form, the data from prior form used to get displayed on some of the controls (textbox, Dropdown list). In short loading and saving forms wasn’t clearing the controls.
In order to do so, it would have been very tedious to get each and every control on the page and set the value as null each time the page loads, like
Textbox1.txt=” “;
So what do we do in case we have page with 50 asp.net controls?
Well the following method will take care of that
using System.Web.UI.WebControls;

Public static void ClearInputs(ControlCollection ctrls)

{
       foreach (Control ctrl in ctrls)

      {
             if (ctrl is TextBox)

                  ((TextBox)ctrl).Text = string.Empty;

           else if (ctrl is DropDownList)

                    ((DropDownList)ctrl).ClearSelection();

                   ClearInputs(ctrl.Controls);

        }

}
and call it like
ClearInputs(Page.Controls);

We can also make use of case statement here like
foreach (Control contl in pageControls)
            {
                string strCntName = (contl.GetType()).Name;
                switch (strCntName)
                {
                    case "TextBox":
                        TextBox tbSource = (TextBox)contl;
                        tbSource.Text = "";
                        break;
                    case "RadioButtonList":
                        RadioButtonList rblSource = (RadioButtonList)contl;
                        rblSource.SelectedIndex = -1;
                        break;
                   
                    case "HtmlInputText":
                        HtmlInputText htsource = (HtmlInputText)contl;
                        htsource.Value = "";
                        break;

                }

                ResetFields(contl.Controls);
            }