Tag Archives: .Net

Alternate row colors for WPF ListView

To set the alternate background colors for the listview in WPF, set the style and then refer that style object in the listview

  <Style x:Key="ListViewAlternateColors" TargetType="ListViewItem">
            <Style.Triggers>
                <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                    <Setter Property="Background" Value="Gray" />
                </Trigger>
                <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                    <Setter Property="Background" Value="LightGoldenrodYellow" />
                </Trigger>
                <Trigger Property="IsSelected" Value="True">
                    <Setter Property="Background" Value="White" />
                </Trigger>
                
            </Style.Triggers>
        </Style>

and then set the style to the listview

 <ListView Height="170" ItemContainerStyle="{StaticResource ListViewAlternateColors}" Name="listView1" Width="719" AlternationCount="2" SelectionChanged="listView1_SelectionChanged" >
            <ListView.ItemTemplate>
                <DataTemplate>
                    <StackPanel>
                        <TextBlock Text="{Binding Title}" />    
                    </StackPanel>
                    
                </DataTemplate>
            </ListView.ItemTemplate>
            
        </ListView>

Hope this helps someone.

.Net Data Design Stratagies

While designing an application to design a data layer or connecting to database there are multiple options:
1. ADO.Net: This the old way of connecting to the data base, creating a connection and binding the data to dataset or datatable and updating or reading the data from database.
2. Entity Framework: Entity Framework is inbuit of .net framework now and it’s a abstract way of connecting to the database and better way for the developers who are not entirely familiar with the database programming. Database is not entirely object oriented like the way the objects are in .net like inheritance so orm tools like Entity Framework maps the relational objects to object oriented entities for the developers to directly interact with the data as objects. Similarly, Entity Framework can map the objects or entities creates in .net and map to relational objects like Code first methodology.

3. WCF Services: .Net Framework version 3.0 introduced WCF as to combine and update several communication technologies like enterprise services, Active Server Methods (ASMX) webservices, Web Service Extensions (WSE), Microsoft Message Queuing (MSMQ) and .Net remoting. WCF web services is the most powerful, flexible, and secure way to provide Application Logic layer services to a Presentation Layer, whether the presentation Layer is part of the same application or different one.

4. WCF Data Services: It was formerly known as ADO.Net Data Services. It allows you to quickly build an OData or REST service. OData or REST are HTTP based open standards for exposing data across the web, where they can be consumed by clients built on a wide variety of platforms, including .Net, Microsoft Silverlight, JQuery, PHP and Javascript.

You can get into a dilemma to choose the correct methodology or framework for data design, below are the few scenarios where we can categorize each with:

Choose Entity Framework when you
–implementing the Data Layer
–connect to the database without network protocol restriction
–require rapid application development
–require strongly typed objects.
Choose WCF Data Services when you are
–implementing the Data Layer
–Connect to the database server by using HTTP
–require access from javascript
–require application level control over access to the data.
Choose WCF web services when you are
–implementing the Application Logic layer
–require specific network protocols, such as WS-Security, SOAP or REST
–require multiple binding types for different clients
–require access from Javascript
Choose ASMX web services when you are
–implementing either Application Logic or Data Layer for a remote application
–need to add web services to an existing ASP.Net application
–will host the application only on IIS
–require only basic HTTP communication.

.Net common questions

Here are some common questions on .Net which can be asked in interviews:

Describe the difference between a Thread and a Process?
An application consists of one or more processes. A process, in the simplest terms, is an executing program. One or more
threads run in the context of the process. A thread is the basic unit to which the operating system allocates processor
time. A thread can execute any part of the process code, incluging parts currently being executed by another thread.

What is a Windows Service and how does its lifecycle differ from a “standard” EXE?
Windows service allows you to create long-running executable applications that run in their own windows sessions. These
services can be managed to start at the boot time or can be controlled manually. The windows service do not have any UI to show
to the user.
Windows service lifetime:
For the service to run, it need to be installed. With the installation, it will load the service into the Service control manager
. Once the service is installed, one can start from the Service Control Manager or from code “start”. The service will run until someone
stops it or the system is shutdown. The service can in any one of these status running, paused or stopped. One can write code to
handle in these statuses.

Standard ‘Exe’ need to be started manually everytime you log in. Standard ‘Exe’ can have UI for the user.

What is the difference between an EXE and a DLL?
EXE is an extension used for executable files while DLL is the extension for a dynamic link library.
An EXE file can be run independently while a DLL is used by other applications
An EXE file defines an entry point while a DLL does not
A DLL file can be reused by other applications while an exe cannot.
A dll would share the same process and memory space of the calling application while an exe creates its separate process and memory space.

What is strong-typing versus weak-typing?
Strong typing means that once assigned a value of a particular kind, objects objey strict rules about how they can interact with other objects of vaious types.
Weak typing means that such rules are more relaxed.
Python is considered strongly typed while PHP is considered weak-typing.
c# is strong types and javascript is weak typed.

still to come…

Cannot explicitly load property for entities that are detached. Objects loaded using the NoTracking merge option are always detached

While working with the POCO using Entity Framework I received the “Cannot explicitly load property for entities that are detached.  Objects loaded using the NoTracking merge option are always detached” error message. I tried Include placed at different places and also used ctx.ContextOptions.LoadProperty method too. At one point it was working but with a different error message, then I realized may be since there is no data in the database this is giving issues. When I loaded some dummy data in the database it started working as expected.

This link is very helpful working with Include.

 

CSS for Disabled anchor tag on asp.net

In the asp.net when the link was disabled it used to apply the disabled properties for the anchor tags. With the 4.0 .Net framework, the colors won’t apply automatically. They introduced a new property for all controls DisabledCssClass which is a string property to set/get the disabled css class. This property is applied to all the controls.
This property can be used to change the name that is rendered for the DisabledCssClass property of individual Web controls. By default, this property returns “aspNetDisabled“.
When SupportsDisabledAttribute is overridden in a derived class to return false, the value of the DisabledCssClass property is rendered as the value of the class attribute of the HTML element for the control. In that case, if there is a value in the CssClass property, both CSS classes will be applied to the rendered HTML element. The class attribute will consist of the value of the DisabledCssClass property followed by the value of the CssClass property, separated by a space.
This property is static, which means that you can set it only for the WebControl class. Whatever value you set it to is used for all controls in a Web application. You cannot specify different values for individual controls.
If you want to set a new class for all the controls all over the application, you can set a new class in the application_start event in global.asax

void Application_Start(object sender, EventArgs e)
{
webcontrol.DisabledCssClass = somecssclass;
}

If you want to set the DisabledCssClass for an anchor tag, you can do that in the stylesheet like

a.aspNetDisabled
{
color:000FFF#;
}

This set of property is applied to anchor tag without affecting any other controls.

Connecting WebService from SQL stored procedure

When I wanted to connect a web service and perform some tasks in database at regular intervals of time, my first choice was going for windows service. That was easy and I was familiar with the process. Due to some constraints in my organization, I had to look for alternatives.I was able to access the webservice from sql clr.Steps:1. Create a wsdl class file of the webservice.using in command prompt

 wsdl http://www.test.com/WebService/Testsvc.asmx /language:vb /out:C:\TestSvc.vb 

 Include this class file in the application where you are creating a sql clr. Below is an example:
Imports Microsoft.VisualBasic
Imports System
Imports System.data
Imports System.Data.Sql
Imports System.Data.SqlClient
Imports Microsoft.SqlServer.Server
Imports System.Data.SqlTypes
Imports System.Runtime.InteropServices
Imports System.XmlPartial 

Public Class StoredProcedures_

  Public Shared Sub GetTest( ByRef value1 As SqlInt32, ByRef value2 As SqlString)
    Dim Connection As New SqlConnection("Context Connection = true")
    Dim iWeb As New com.WSMainTestDim result as Integer = iWeb.GetAdditionTest(value1, value2)Connection.Open()
    Dim insert As String = "Insert into tblResponse (Text) Values ('" + result + "')"
    Dim command1 As New SqlCommand(insert, Connection)
    command1.ExecuteNonQuery()
    Connection.Close()
    Connection.Dispose()
  End Sub
End Class3.

Once I created the clr built the application and turned this class into a dll and also web service is also created as dll. create sgen for webservice.4.Add all 3 dlls to the database on which you wanted to attach the sql and access the webservice. Get to the database, in the assembly folder add these 3 dlls. The main clr file which connects to dll need to have external access permission set.5.Once you add these 3 dlls to the assembly folder, create procedure for the stored procedure clr.

Create PROC [Stored Procedure definition]([Parameters])
   With EXECUTE AS CALLERASEXTERNAL NAME (dll name of the stored procedure).     (class name).(method name)

For my purpose I created a job to perform certain tasks at regular intervals of time.Hope this could help someone.Thanks,Kishore Patil