Wednesday, November 25, 2015

Create dynamic Tab Item with different User control using WPF MVVM in 5 steps

To create tab item dynamically with different user control using WPF MVVM, we need to follow below steps :
1. Create ITabViewModel Interface :

    public interface ITabViewModel
    {
        String Header { get; set; }
    }
2. Create three View model as follows :
 
   // ViewModelA

   public  class ViewModelA:ITabViewModel
    {
      public string Header { get; set; }
      public ViewModelA()
       {  
       }
    }

  // ViewModelB

   public  class ViewModelB:ITabViewModel
    {
      public string Header { get; set; }
      public ViewModelB()
       {  
       }
    }

  // ViewModelC

   public  class ViewModelC:ITabViewModel
    {
      public string Header { get; set; }
      public ViewModelC()
       {  
       }
    }
3. Create three user control like :

// First user control ViewAUserControl.xaml

          <UserControl x:Class="WPFTestApp.View.ViewAUserControl"
             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="300" d:DesignWidth="300">
    <Grid>
        <Button Content="A" Height="30" Width="100"></Button>
    </Grid>
</UserControl>

// Second user control ViewBUserControl.xaml

         <UserControl x:Class="WPFTestApp.View.ViewBUserControl"
             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="300" d:DesignWidth="300">
    <Grid>
        <Button Content="B" Height="30" Width="100"></Button>
    </Grid>
</UserControl>


// Third user control ViewCUserControl.xaml

        <UserControl x:Class="WPFTestApp.View.ViewCUserControl"
             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="300" d:DesignWidth="300">
    <Grid>
        <Button Content="C" Height="30" Width="100"></Button>
    </Grid>
</UserControl>

4. Create TabViewModel to use with Tab Window as follows :

    public class TabViewModel
    {
        public ObservableCollection TabViewModels { get; set; }

        public TabViewModel()
        {
            TabViewModels = new ObservableCollection();
            TabViewModels.Add(new ViewModelA { Header = "Tab A" });
            TabViewModels.Add(new ViewModelB { Header = "Tab B" });
            TabViewModels.Add(new ViewModelC { Header = "Tab C" });

        }
    }
5. Create one Tab Window to use user control code look like :

        <Window x:Class="WPFTestApp.View.TabWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:WPFTestApp.View"
        xmlns:vm="clr-namespace:WPFTestApp.ViewModel"
        Title="TabWindow" Height="300" Width="300">
    <Grid>
        <Grid.Resources>
            <vm:TabViewModel x:Key="TabVM"></vm:TabViewModel>
        </Grid.Resources>
       <TabControl x:Name="MyTabControl"
            ItemsSource="{Binding TabViewModels}" DataContext="{StaticResource TabVM}"
           SelectedItem="{Binding SelectedTabViewModel}" >

            <TabControl.Resources>
                <DataTemplate DataType="{x:Type vm:ViewModelA}">
                    <my:ViewAUserControl />
                </DataTemplate>
                <DataTemplate DataType="{x:Type vm:ViewModelB}">
                    <my:ViewBUserControl />
                </DataTemplate>
                <DataTemplate DataType="{x:Type vm:ViewModelC}">
                    <my:ViewCUserControl />
                </DataTemplate>
            </TabControl.Resources>

            <TabControl.ItemContainerStyle>
                <Style TargetType="TabItem">
                    <Setter Property="Header" Value="{Binding Header}" />
                </Style>
            </TabControl.ItemContainerStyle>

        </TabControl>
    </Grid>
</Window>
     

Here we are using data template to add different view model.

Output will look like :

Thursday, September 10, 2009

Datainteraction class in asp.net

Hi friends,
This article includes a datainteraction class which is used for establish connection between asp.net and sql server & retrieving data etc . it involves all the function for data interaction.

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;

///
/// Summary description for DataInteraction
///

public class DataInteraction
{
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();

SqlDataAdapter adpt = new SqlDataAdapter();
SqlDataReader dr;

public DataInteraction()
{

con.ConnectionString = ConfigurationManager.ConnectionStrings["nimbus"].ToString();

}
public DataSet FillDs(String query)
{
DataSet ds = new DataSet();
con.Open();
cmd.Connection = con;
cmd.CommandText = query;
cmd.ExecuteNonQuery();
adpt.SelectCommand = cmd;
adpt.Fill(ds);
con.Close();
return ds;
}
public void IDU(String query)
{
con.Open();
cmd.Connection = con;
cmd.CommandText = query;
cmd.ExecuteNonQuery();
con.Close();
}

public String SelectSclar(String query)
{
con.Open();
cmd.Connection = con;
cmd.CommandText = query;
string result = Convert.ToString(cmd.ExecuteScalar());
con.Close();
return result;

}
public String SelectScalarsingle(string qry)
{
con.Open();
cmd.Connection = con;
cmd.CommandText = qry;
string rs = Convert.ToString(cmd.ExecuteScalar());
con.Close();
return rs;
}
public SqlDataReader FillDr(String query)
{
con.Open();
cmd.Connection = con;
cmd.CommandText = query;
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
con.Close();
return dr;


}

public SqlDataReader filldrdd(string qry)
{
con.Open();
cmd.Connection = con;
cmd.CommandText = qry;
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
return dr;
}
public void fillddlbydr(string qry, string txtfield, string valuefield, DropDownList ddl)
{
ddl.Items.Clear();
dr = filldrdd(qry);
ddl.Items.Add(new ListItem("---Select---", "0"));
if (dr.HasRows)
{
while (dr.Read())
{
ddl.Items.Add(new ListItem(dr[txtfield].ToString(), dr[valuefield].ToString()));
}



}
dr.Dispose();
}
public void fillddlbydrnotselect(string qry, string txtfield, string valuefield, DropDownList ddl)
{
ddl.Items.Clear();
dr = filldrdd(qry);
// ddl.Items.Add(new ListItem("---Select---", "0"));
if (dr.HasRows)
{
while (dr.Read())
{
ddl.Items.Add(new ListItem(dr[txtfield].ToString(), dr[valuefield].ToString()));
}



}
dr.Dispose();
}

public void fillddlbyGarde(string qry, string txtfield, string valuefield, DropDownList ddl)
{
ddl.Items.Clear();
dr = filldrdd(qry);
ddl.Items.Add(new ListItem("Grade", "0"));
if (dr.HasRows)
{
while (dr.Read())
{
ddl.Items.Add(new ListItem(dr[txtfield].ToString(), dr[valuefield].ToString()));
}



}
dr.Dispose();
}
public void fillddlbydrpn(string qry, string txtfield, string valuefield, DropDownList ddl)
{
ddl.Items.Clear();
dr = filldrdd(qry);
ddl.Items.Add(new ListItem("PN", "0"));
if (dr.HasRows)
{
while (dr.Read())
{
ddl.Items.Add(new ListItem(dr[txtfield].ToString(), dr[valuefield].ToString()));
}



}
dr.Dispose();
}
public void fillddlbydrdn(string qry, string txtfield, string valuefield, DropDownList ddl)
{
ddl.Items.Clear();
dr = filldrdd(qry);
ddl.Items.Add(new ListItem("DN", "0"));
if (dr.HasRows)
{
while (dr.Read())
{
ddl.Items.Add(new ListItem(dr[txtfield].ToString(), dr[valuefield].ToString()));
}



}
dr.Dispose();
}
}

Sunday, August 23, 2009

How to Print a Crystal Report Programmatically in ASP.NET?

How to Print a Crystal Report Programmatically in ASP.NET?
You can print a Crystal Report using print option of Crystal Report Viewer. However, there are occasions when you want your application to print a report direct to printer without viewing the report in Crystal Report Viewer.
The ReportDocument class provides PrintToPrinter method that may be used to print a CR direct to the printer. If no printer is selected, the default printer will be used to send the printing pages to.
The PrintToPrinter method takes four parameters.
nCopies : Indicates the number of copies to print.
collated : Indicates whether to collate the pages.
startPageN : Indicates the first page to print.
endPageN : Indicates the last page to print.

The following steps will guide you to achieve the same:
1. Add crystal report (.cr) file to your ASP.NET application.

2. Add a report instance on the page level.
Dim report As MyReport = New MyReport

3.Populate reports data on Page_Init
' Get data in a DataSet or DataTable

Dim ds As DataSet = GetData()
' Fill report with the data
report.SetDataSource(ds)
4. Print Report
report.PrintToPrinter(1, False, 0, 0)
If you wish to print certain page range, change last two parameters From to To page number.
If you want to set page margins, you need to create a PageMargin object and set PrintOptions of the ReportDocument.
The following code sets page margins and printer name:
Dim margins As PageMargins = Report.PrintOptions.PageMargins
margins.bottomMargin = 200
margins.leftMargin = 200
margins.rightMargin = 50
margins.topMargin = 100
Report.PrintOptions.ApplyPageMargins(margins)
' Select the printer name
Report.PrintOptions.PrinterName = printerName