How to create DataTable and add primary key to DataTable in asp.net


Creating DataTable:
//create inatance of datatable
DataTable dt = new DataTable();
//give datatable name to dt
dt.TableName = "MyTable";
//add columns to datatable dt
dt.Columns.Add("ImgPath", typeof(string));
//creating primary key column
DataColumn[] Key = new DataColumn[1];
DataColumn col = new DataColumn();
//setting datatype to column
col.DataType = System.Type.GetType("System.String");
//giving column name
col.ColumnName = "Name";
//adding column to datatable dt
dt.Columns.Add(col);
Key[0] = col;
// adding other columns to datatable directly
dt.Columns.Add("Desc", typeof(string));
dt.Columns.Add("Price", typeof(string));
//setting primary key to datatable
dt.PrimaryKey = Key;

 

Adding Data to Created DataTable:

//creating new data row
DataRow dr = dt.NewRow();
//adding data to the above created datarow
dr[0] = "ImgPath";
dr[1] = "Name";
dr[2] = "Desc";
dr[3] = "123.45";
//adding datarow to datatable
dt.Rows.Add(dr);

How to crate datatable and add primary key to datatable in asp.net


Creating DataTable:

//create inatance of datatable
DataTable dt = new DataTable();
//give datatable name to dt
dt.TableName = “MyTable”;
//add columns to datatable dt
dt.Columns.Add(“ImgPath”, typeof(string));
//creating primary key column
DataColumn[] Key = new DataColumn[1];
DataColumn col = new DataColumn();
//setting datatype to column
col.DataType = System.Type.GetType(“System.String”);
//giving column name
col.ColumnName = “Name”;
//adding column to datatable dt
dt.Columns.Add(col);
Key[0] = col;
// adding other columns to datatable directly
dt.Columns.Add(“Desc”, typeof(string));
dt.Columns.Add(“Price”, typeof(string));
//setting primary key to datatable
dt.PrimaryKey = Key;

Adding Data to Created DataTable:

//creating new data row
DataRow dr = dt.NewRow();
//adding data to the above created datarow
dr[0] = “ImgPath”;
dr[1] = “Name”;
dr[2] = “Desc”;
dr[3] = “123.45”;
//adding datarow to datatable
dt.Rows.Add(dr);

How to create Dynamic Openfiledialog to select image and bind it to image control in wpf


First take a WPF new project application

and design form as follows:

XAML Code:
<Window x:Class="DynamicFileUpLoad.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
Title="MainWindow" Height="350" Width="525">
    <Grid>
        <GroupBox Header="OpenfileDialog" Height="287" HorizontalAlignment="Left" Margin="12,12,0,0" Name="groupBox1" VerticalAlignment="Top" Width="479" FontWeight="Bold" FontSize="15">
            <Grid>
                <Image Height="251" HorizontalAlignment="Left" Margin="6,6,2,2" Name="image1" Stretch="Fill" VerticalAlignment="Top" Width="249" />
                <Button Content="BrowseImage" Height="35" HorizontalAlignment="Left" Margin="304,97,0,0" Name="BtnBrowse" VerticalAlignment="Top" Width="121" Click="BtnBrowse_Click" />
            </Grid>
        </GroupBox>
    </Grid>
</Window>

NameSpace:
using Microsoft.Win32;

CodeBehind(Under ButtonClick Event):
private void BtnBrowse_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                OpenFileDialog opnfdl = new OpenFileDialog();
                opnfdl.InitialDirectory = "My Documents";
                opnfdl.Title = "SelectFile";
                opnfdl.DefaultExt = ".jpg";
                opnfdl.Filter = "All Image files|*.jpg";
                if (opnfdl.ShowDialog() == true)
                {
                    image1.Source = new BitmapImage(new Uri(opnfdl.FileName));
                }
            }
            catch (Exception errorMsg)
            {
                MessageBox.Show(errorMsg.Message);
            }
        }

You Can Get Output like this:

DownloadSampleCode

How to call wcf service from wpf client


DataBaseDesign:

First create database with name MySampleDB and create table as follows with name UserDetails

then add data to table as follows:

WCFService Creation:

First go to file newproject–>wcfservicelibrary

Then add required service files as follows

1. interface class

2.Class Implementing InterFace

3.UserDefinedClasses(if any used)

Write following code in Userdefined Class:

Write this code in Interface Class:

Then Write Following Code in Class implementing Interface

After Completion of creating service run the application

now service will hosted check service is hosted of no as follows

After completion of service hosting you can get the following window

To check wether your service is running or not call the service methods in

wcftestclient window as follows

Now your service is Hosted successfully.

Now prepare client as follows

WPFClient:

Open new visualstudio go to file–> new Project–>wpfclient

Design wpfclient as follows

This is the code for above window design

  1. <Window x:Class="WpfClient.MainWindow"
  2. xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation&quot;
  3. xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml&quot;
  4. Title="MainWindow" Height="500" Width="800">
  5.     <Grid>
  6.         <GroupBox Header="Details" Height="361" HorizontalAlignment="Left" Margin="500,27,0,0" Name="groupBox1" VerticalAlignment="Top" Width="266">
  7.             <Grid>
  8.                 <Label Content="EnterID:" Height="28" HorizontalAlignment="Left" Margin="16,17,0,0" Name="label1" VerticalAlignment="Top" FontWeight="Bold" FontSize="16" />
  9.                 <Label Content="UserID:" FontSize="16" FontWeight="Bold" Height="28" HorizontalAlignment="Left" Margin="16,122,0,0" Name="label2" VerticalAlignment="Top" />
  10.                 <Label Content="UserName:" FontSize="16" FontWeight="Bold" Height="28" HorizontalAlignment="Left" Margin="16,162,0,0" Name="label3" VerticalAlignment="Top" />
  11.                 <Label Content="LastName:" FontSize="16" FontWeight="Bold" Height="28" HorizontalAlignment="Left" Margin="16,204,0,0" Name="label4" VerticalAlignment="Top" />
  12.                 <Label Content="Location:" FontSize="16" FontWeight="Bold" Height="28" HorizontalAlignment="Left" Margin="16,244,0,0" Name="label5" VerticalAlignment="Top" />
  13.                 <TextBox Height="23" HorizontalAlignment="Left" Margin="101,22,0,0" Name="textBox1" VerticalAlignment="Top" Width="120" />
  14.                 <TextBox Height="23" HorizontalAlignment="Left" Margin="118,167,0,0" Name="textBox2" VerticalAlignment="Top" Width="120" />
  15.                 <TextBox Height="23" HorizontalAlignment="Left" Margin="118,127,0,0" Name="textBox3" VerticalAlignment="Top" Width="120" />
  16.                 <TextBox Height="23" HorizontalAlignment="Left" Margin="118,209,0,0" Name="textBox4" VerticalAlignment="Top" Width="120" />
  17.                 <TextBox Height="23" HorizontalAlignment="Left" Margin="118,249,0,0" Name="textBox5" VerticalAlignment="Top" Width="120" />
  18.                 <Button Content="GetSingleRecord" Height="36" HorizontalAlignment="Left" Margin="25,66,0,0" Name="button1" VerticalAlignment="Top" Width="196" Click="button1_Click_1" />
  19.             </Grid>
  20.         </GroupBox>
  21.         <GroupBox Header="AllDetails" Height="361" HorizontalAlignment="Left" Margin="22,27,0,0" Name="groupBox2" VerticalAlignment="Top" Width="453">
  22.             <Grid>
  23.                 <DataGrid AutoGenerateColumns="False" Height="263" HorizontalAlignment="Left" Margin="14,73,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="413" AlternatingRowBackground="#FFDEA5DE" RowBackground="#FFFFBEA5" FontSize="18" FontWeight="Bold">
  24.                     <DataGrid.Columns>
  25.                         <DataGridTemplateColumn Header="UserId" Width="SizeToCells" IsReadOnly="True">
  26.                             <DataGridTemplateColumn.CellTemplate>
  27.                                 <DataTemplate>
  28.                                     <TextBlock Text="{Binding Path=UserId}"></TextBlock>
  29.                                 </DataTemplate>
  30.                             </DataGridTemplateColumn.CellTemplate>
  31.                         </DataGridTemplateColumn>
  32.                         <DataGridTemplateColumn Header="UserId" Width="SizeToCells" IsReadOnly="True">
  33.                             <DataGridTemplateColumn.CellTemplate>
  34.                                 <DataTemplate>
  35.                                     <TextBlock Text="{Binding Path=UserName}"></TextBlock>
  36.                                 </DataTemplate>
  37.                             </DataGridTemplateColumn.CellTemplate>
  38.                         </DataGridTemplateColumn>
  39.                         <DataGridTemplateColumn Header="UserId" Width="SizeToCells" IsReadOnly="True">
  40.                             <DataGridTemplateColumn.CellTemplate>
  41.                                 <DataTemplate>
  42.                                     <TextBlock Text="{Binding Path=LastName}"></TextBlock>
  43.                                 </DataTemplate>
  44.                             </DataGridTemplateColumn.CellTemplate>
  45.                         </DataGridTemplateColumn>
  46.                         <DataGridTemplateColumn Header="UserId" Width="SizeToCells" IsReadOnly="True">
  47.                             <DataGridTemplateColumn.CellTemplate>
  48.                                 <DataTemplate>
  49.                                     <TextBlock Text="{Binding Path=Location}"></TextBlock>
  50.                                 </DataTemplate>
  51.                             </DataGridTemplateColumn.CellTemplate>
  52.                         </DataGridTemplateColumn>
  53.                     </DataGrid.Columns>
  54.                 </DataGrid>
  55.                 <Button Content="GetDataFromDataSet" Height="37" HorizontalAlignment="Left" Margin="14,22,0,0" Name="button2" VerticalAlignment="Top" Width="183" Click="button2_Click_1" />
  56.                 <Button Content="GetDataFromUserDefinedType" Height="39" HorizontalAlignment="Left" Margin="241,20,0,0" Name="button3" VerticalAlignment="Top" Width="186" Click="button3_Click_2" />
  57.             </Grid>
  58.         </GroupBox>
  59.     </Grid>
  60. </Window>

After Completion of designing we need to Create Proxy for service to consume service:

To create Proxy Do the following:

First Copy The Address Of  Service:

Then Add System.ServiceModel  dll file from .net tab of add reference

Then RightClick on your Project select Add ServiceReference:

After Adding this proxy if you want to see things added to servicereference

first select project the click on showhidden files as shown in below diagram

Then Write Following Code In CodeBehind:

Then Satart Calling From Client:

DownloadSampleCode