Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Archive for October, 2010

Beginners Guide to Visual Studio LightSwitch

Posted by Viral Sarvaiya on October 26, 2010


SIMPLEST WAY TO BUILD BUSINESS APPLICATIONS FOR THE DESKTOP, WEB AND CLOUD

Microsoft Visual Studio LightSwitch gives you a simpler and faster way to create professional-quality business applications for the desktop, the web, and the cloud. LightSwitch is a new addition to the Visual Studio family. Visit this page often to learn more about this exciting product.

Visual Studio LightSwitch is a new tool for building data-driven Silverlight Application using Visual Studio IDE. It automatically generates the User Interface for a DataSource without writing any code. You can write a small amount of code also to meet your requirement.

Fore More Details :

http://www.microsoft.com/visualstudio/en-us/lightswitch

http://msdn.microsoft.com/hi-in/lightswitch/default%28en-us%29.aspx

Make a simple application demo…

http://www.silverlightshow.net/items/Beginners-Guide-to-Visual-Studio-LightSwitch-Part-1.aspx

http://www.silverlightshow.net/items/Beginners-Guide-to-Visual-Studio-LightSwitch-Part-2.aspx

Posted in ASP.NET | Tagged: , , , , | Leave a Comment »

Google Music India Launched

Posted by Viral Sarvaiya on October 25, 2010


Google India has pulled the covers off the Google Music service in India, offering users access to live streaming of Hindi film songs online. This is not new, but the reach that Google has makes it the service to watch. The implementation of Google Music India is similar to that of Guruji.com, which has been sued by music label T-Series, but in Google’s case, they’ve partnered with companies that have licensed content legally: Music label Saregama, Web18 owned In.com and the Hungama-212 media JV – Saavn. Like Guruji, Google music search suggests albums, allows users to search artists, films and albums, and offers to even sort music by date. Google music search doesn’t have top 10 lists yet.

As you can see from the screenshot below, the music is played in a pop-up window, and a display ad is run as a footer there. There is probably a revenue share arrangement between the music licensee and Google. Remember that in case of songs that were being played at Songs.pk via Guruji, there were similar ads. Only, this music is legal, and like Guruji was doing, it is being hosted and played from the someone elses site.

Enjoy Music…

http://www.google.co.in/music

Posted in General | Tagged: , , , | 2 Comments »

Convert Time With Different TimeZones in ASP.NET

Posted by Viral Sarvaiya on October 22, 2010


Here i demostrate how to convert date time with different time zone.

for that make a simple web application having 1 dropdown for select time zone and 2 label for local time and converted time.

in default.aspx page

<table>
 <tr>
 <td>
 <asp:DropDownList ID="ddlZone" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ddlZone_SelectedIndexChanged"
 AppendDataBoundItems="true">
 <asp:ListItem Text="Select a TimeZone" Value="Default value" />
 </asp:DropDownList>
 </td>
 </tr>
 <tr>
 <td>
 Local Time: <asp:Label ID="lblLocalTime" runat="server" Text=""></asp:Label>
 </td>
 </tr>
 <tr>
 <td>
 Converted Time: <asp:Label ID="lblTimeZone" runat="server" Text=""></asp:Label>
 </td>
 </tr>
 </table>

and in default.aspx.cs file

add namespance “using System.Collections.ObjectModel” for readonlycollection<T>

protected void Page_Load(object sender, EventArgs e)
 {
 if (!Page.IsPostBack)
 {
 lblLocalTime.Text = DateTime.Now.ToString();
 ReadOnlyCollection<TimeZoneInfo> tz;
 tz = TimeZoneInfo.GetSystemTimeZones();
 foreach (TimeZoneInfo timeZone in tz)
 {
 ddlZone.Items.Add(new ListItem(timeZone.DisplayName, timeZone.Id));
 }
 }
 }

 protected void ddlZone_SelectedIndexChanged(object sender, EventArgs e)
 {
 if (ddlZone.SelectedIndex > 0)
 {
 DateTime dt = DateTime.Now;
 lblTimeZone.Text = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dt,
 TimeZoneInfo.Local.Id, ddlZone.SelectedValue).ToString();
 }
 }

now press F5 and run the code….

enjoy coding…..

Posted in ASP.NET | Tagged: , , | 1 Comment »

Remove duplicate records from the database table

Posted by Viral Sarvaiya on October 18, 2010


In my recent post https://viralsarvaiya.wordpress.com/2010/09/27/removing-duplicate-records-from-datasetdatatable/, I explain how to remove duplicate records form the dataset or datatable.

But if you want to delete duplicate records directly from the database then…..
First find the duplicate records,


SELECT MAX(ID) as ID, Name, Company FROM Employee GROUP BY Name, Company HAVING COUNT(*) > 1

This records give me the total duplicate records

Now to delete the duplicate records,


DELETE FROM Employee WHERE ID IN (SELECT MAX(ID) FROM Employee GROUP BY Name, Company HAVING COUNT(*) > 1)

Enjoy Coding…..

Posted in Sql Server | Tagged: , , , | Leave a Comment »

Redirect to a New Page using JavaScript

Posted by Viral Sarvaiya on October 16, 2010


An easy way to redirect a user to a new Page using JavaScript is to use


window.location = "http://www.devcurry.com";

However the problem with this approach is that it adds an item to your browser history. So in browsers like IE, the user can access the history by hitting the back button, which can be confusing to users, as they will redirected back and forth.

 

If you want to avoid the back button, use ‘window.location.replace’ which loads the new page and replaces the current page in the user’s History, with the new one, as shown below:


<head>
 <title>Redirect to a New Page</title>
 <script type="text/javascript">
 window.location.replace("http://www.devcurry.com");
 </script>
</head>
<body>

Now when you are redirected to the new page, there is no back button.

For More Details Click Here

Posted in ASP.NET, Javascript | Tagged: , , , | Leave a Comment »

New Data Binding Features in Silverlight 4

Posted by Viral Sarvaiya on October 11, 2010


Data Binding and String Formatting

In prior versions of Silverlight, in order to perform string formatting while data binding you would need to write a converter that would format the data during binding. In Silverlight 4 you are now able to format the data directly in XAML. This greatly simplifies the process of creating bindings that require some formatting of the data. Adding string formatting is as simple as adding a StringFormat extension in the XAML markup of the data binding. The StringFormat extension supports the same formatting options as the String’s Format method. Consider the following XAML. There are four text boxes displayed, all bound to the same property in code behind. The difference, however, is that each TextBox change displays the data differently based on the binding’s StringFormat extension. The first TextBox shows the raw data, the second formats the data to three decimal places, the third show the value in scientific notation, and the fourth shows the data as currency.


<Grid x:Name="LayoutRoot" Background="White">

<Grid.RowDefinitions>

<RowDefinition Height="*" />

<RowDefinition Height="*" />

<RowDefinition Height="*" />

<RowDefinition Height="*" />

</Grid.RowDefinitions>

<TextBox

Margin="5"

Grid.Row="0"

Text="{Binding DecimalValue}" />

<TextBox

Margin="5"

Grid.Row="1"

Text="{Binding DecimalValue, StringFormat='##.###'}" />

<TextBox

Margin="5"

Grid.Row="2"

Text="{Binding DateValue, StringFormat='MMM dd, yyyy'}" />

<TextBox

Margin="5"

Grid.Row="3"

Text="{Binding DecimalValue, StringFormat='c'}" />

</Grid>

As you can see from this example, you can easily change the format of data directly through the XAML.

TargetNullValue and FallBackValue Extensions

Two other extensions have been added to the base binding class in Silverlight 4: the TargetNullValue and FallBackValue extensions. These extensions allow you to specify data that will be displayed in the case when the data being bound to the control is not as expected. TargetNullValue provides a value for the binding that should be used if the data value being bound is null. An example of using this extension is shown here:


<TextBox Text="{Binding ValueName, TargetNullValue='Value is Null'}" />

FallBackValue provides a value to be used if the bound data is missing or if there was a mismatch that occurred. An example of using the FallBackValue extension is shown here:


<TextBox Text="{Binding ValueName, FallbackValue='Value Not Found'}" />

Thanks you.

Posted in ASP.NET, Silverlight | Tagged: , , , , , , , , | Leave a Comment »

What is difference between Web Application Project and Web Site Project

Posted by Viral Sarvaiya on October 6, 2010


The following table helps you select a project type by describing some of the key differences between Web application projects and Web site projects.

Scenario Web Application Project Web Site Project
Project definition Similar to Visual Studio .NET 2003. Only files that are referenced in the project file are part of the project, are displayed in Solution Explorer, and are compiled during a build. Because there is a project file, some scenarios are more easily enabled:

You can subdivide one ASP.NET application into multiple Visual Studio projects.

You can easily exclude files from the project and from source code-control.

Web site projects use the folder structure to define the contents of the project. There is no project file and all files in the folder are part of the project.

This project type is desirable if you have an existing folder structure representing an ASP.NET application that you want to edit in Visual Studio without having to explicitly create a project file.

Compilation and build outputs The compilation model for Web application projects is very similar to that in Visual Studio .NET 2003.

All code-behind class files and stand-alone class files in the project are compiled into a single assembly, which is placed in the Bin folder. Because this is a single assembly, you can specify attributes such as assembly name and version, as well as the location of the output assembly.

Certain other applications scenarios are better enabled, such as the Model-View-Controller (MVC) pattern, because they allow stand-alone classes in the project to reference page and user control classes.

The Build command compiles Web site projects only to test them. To run Web site projects, you deploy source files and rely on ASP.NET dynamic compilation to compile pages and classes in the application.

Alternatively, you can precompile the site for performance, which uses the same compilation semantics as ASP.NET dynamic compilation. The ASP.NET dynamic compilation system has two modes—batch mode (the default) and fixed-names mode. In batch mode, many assemblies (typically one per folder) are produced when precompiling the site. In fixed mode, one assembly is produced for each page or user control in the Web site.

Iterative development To run and debug pages, you must build the entire Web project. Building the entire Web application project is usually fast, because Visual Studio employs an incremental build model that builds only the files that have changed. You can configure build options Visual Studio 2005 for when you run the site: build the site, an individual page, or nothing at all. In the last case, when you run a Web site, Visual Studio simply launches the browser and passes to it the current or start page. The request then invokes ASP.NET dynamic compilation.

Because pages are compiled dynamically and compiled into different assemblies as needed, it is not required that the entire project compile successfully in order to run and debug a page.

By default, Visual Studio completely compiles Web site projects whenever you run or debug any page. This is done to identify compile-time errors anywhere in the site. However, a complete site build can significantly slow down the iterative development process, so it is generally recommended that you change the build project option to compile only the current page on run or debug.

Deployment Because all class files are compiled into a single assembly, only that assembly needs to be deployed, along with the .aspx and .ascx files and other static content files.

In this model, .aspx files are not compiled until they are run in the browser. However, when used with Web Deployment Projects (a downloadable add-in to Visual Studio 2005), the .aspx files can also be compiled and included in a single assembly for deployment.

Each time you deploy the single assembly produced in this model, you replace the code for all pages in the project.

Both .aspx files and code-behind files can be compiled into assemblies using the Publish Website command in Visual Studio. (Note that the Build command does not create a deployable set of assemblies.) The updateable publish option supports compiling only code-behind files while leaving .aspx files unchanged for deployment.

The default mode for precompiling produces several assemblies in the Bin folder, typically one per folder. The fixed-names option produces one assembly per page or user control and can be used to create deployable versions of individual pages. However, the fixed-names option increases the number of assemblies and can result in increased memory usage.

Upgrade from Visual Studio .NET 2003 Because the Web application project model is the same as in the Visual Studio .NET 2003, upgrade is generally simple and will usually not require any restructuring of the application. The compilation option for Web site projects is significantly different than Visual Studio .NET 2003. A conversion wizard is available to upgrade existing Visual Studio .NET 2003 Web projects to Web site projects. For any reasonably complex Visual Studio .NET 2003 projects, manual fix-up is usually required after the conversion. For most scenarios, it is preferable to upgrade existing Visual Studio .NET 2003 projects to Web application projects in Visual Studio 2005.

Posted in ASP.NET | Tagged: , , , | 1 Comment »

How to use ImageMap Control

Posted by Viral Sarvaiya on October 3, 2010


ImageMap control is another new server control released in ASP.NET 2.0. It enables you to turn an image into a navigation menu. In the past, developers would break an image into multiple pieces and put it together again in a table, reassembling the pieces into one image.

When the end user clicked a particular piece of the overall image, the application picked out which piece of image was chosen and based actions upon that particular selection.

With the new ImageMap control, you can define images with distinct regions called hot spots which are identified using coordinates. Each hotspot is independent element which can respond to user actions like postback.

Hot spots features

o    There is no limit on number of hotspots each image may contain.

o    Each hotspot is characterized by various attributes like shape, location and size.

o    Overlapping hotspots are perfectly valid.

o    Hot spots are defined using x and y coordinates.

o    Hot spots can be assigned Image URL’s and are capable of postback.

Different types of hot spots

There are three different types of hot spots offered by ImageMap control. They are:

o    CircleHotspot

o    RectangleHotspot

o    PolygonHotspot

CircleHotspot: CircleHotspot defines circle shaped hot spot region in an ImageMap control. To define the region for a circle hot spot, we should define X and Y coordinates for circle as well as radius property which usually is the distance from the center of circle to the edge.

RectangleHotspot: RectangleHotspot defines rectangle shaped hot spot region in an ImageMap control. To define the region for a Rectangle hot spot, we define Top, Bottom, Left and Right coordinates. Similar is the case for the Polygon hot spot.

Overlapping hotspots are treated with extra attribute called z-index value. The hotspot with higher z-index value will be treated as selected hotspot.

With the new ImageMap control, you can take a single image and specify particular hotspots on the image using coordinates. An example is shown below.


<asp:ImageMap ID="imgmap1" runat="server" ImageUrl="~/SimpleDataBindSilverlight.png" onclick="ImageMap1_Click" HotSpotMode="PostBack">
 <asp:RectangleHotSpot Top="0" Bottom="100" Left="0" Right="100" AlternateText="Top Left" PostBackValue="This is Top Left" />
 <asp:RectangleHotSpot Top="100" Bottom="100" Left="100" Right="100" AlternateText="All 100 pixel" PostBackValue="This is at all 100 pixel" />
 </asp:ImageMap>


protected void ImageMap1_Click(object sender, ImageMapEventArgs e)
 {
 Response.Write("Hi," + e.PostBackValue);
 }

Run the code and click to image…

Happy coding….

Posted in ASP.NET | Tagged: , , , | Leave a Comment »

Simple Data Binding in Silverlight

Posted by Viral Sarvaiya on October 1, 2010


Simple Data Binding in Silverlight
To help explain data binding in Silverlight, let’s build a very simple application. The application will include a Book object that contains two properties: Title and ISBN. These properties will be bound to two TextBox controls.

1.  Create a new Silverlight application in Visual Studio 2010. Name the project BasicDataBinding, and allow Visual Studio to create a Web application project to host your application.

2. Edit the MainPage.xaml file to define two columns and six grid rows. Place a TextBlock in each row in column 1 and a TextBox in each row in column 2. Also add some margins and some alignment assignments to improve the layout.

The code for the page follows:


<Grid x:Name="LayoutRoot" Background="White">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock Text="Book Title"
VerticalAlignment="Center"
Margin="5" />
<TextBlock Text="ISBN-13"
VerticalAlignment="Center"
Margin="5"
Grid.Row="1" />
<TextBox Text="{Binding Title}"
Height="24"
Margin="5"
Grid.Column="1" />
<TextBox Text="{Binding ISBN}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="1" />
<TextBlock Text="Book Title"
VerticalAlignment="Center"
Margin="5"
Grid.Row="2" />
<TextBlock Text="ISBN-13"
VerticalAlignment="Center"
Margin="5"
Grid.Row="3" />

<TextBox Text="{Binding Title}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="2" />
<TextBox Text="{Binding ISBN}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="3" />

</Grid>

3. Next, edit the code behind, MainPage.xaml.cs. Add a Loaded event handler for the application, which will fire when the application is loaded by the client. This is accomplished with the following source code:


public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
}

void Page_Loaded(object sender, RoutedEventArgs e)
{

}
}

Now you need to add a class to define a Book object. Below the MainPage class, add the following class definition:


namespace BasicDataBinding
{
public partial class MainPage : UserControl
{
public MainPage()
{
InitializeComponent();
this.Loaded += new RoutedEventHandler(Page_Loaded);
}
void Page_Loaded(object sender, RoutedEventArgs e)
{
}
}
public class Book
{
public string Title { get; set; }
public string ISBN { get; set; }
}
}

4. Now that you have Book defined, you need to create an instance of Book and set it to the LayoutRoot’s DataContext, as follows:


void Page_Loaded(object sender, RoutedEventArgs e)
{
Book b = new Book()
{
Title = "Beginning Silverlight 4: From Novice to Professional",
ISBN = "978-1430229889"
};
this.LayoutRoot.DataContext = b;
}

When you set up binding definitions for different controls, the controls do not know where they are going to get their data. The DataContext property sets the data context for a control that is participating in data binding.The  DataContext property can be set directly on the control. If a given control does not have a DataContext property specified, it will look to its parent for its data context. The nice thing about this model is that if you look above in the XAML for the page, you will see little indication of where the controls are getting their data. This provides an extreme level of code separation, allowing designers to design XAML UIs and developers to work alongside the designers, defining the specifics of how the controls are bound to their data sources.

5. At this point, you can go ahead and start debugging the application. If all goes well, you will see the four text boxes populated with the data from the Book’s instance

6.  With the application running, change the book title in the first text box to just “Beginning Silverlight,” by removing the “From Novice to Professional.” You might expect that, since the third text box is bound to the same data, it will automatically update to reflect this change. However, a couple of things need to be done to get this type of two-way binding to work. One problem is that, currently, the Book class does not support notifying bound clients of changes to its properties. In other words, when a property changes in Book, the class will not notify the TextBox instances that are bound to the class of the change. You could take care of this by creating a change event for each property. This is far from ideal; fortunately, there is an interface that a class can implement that handles this for you. This interface is known as INotifyPropertyChanged. Let’s use it.

7.  Modify the Book class definition to inherit from INotifyPropertyChanged. Notice that when you inherit from INotifyPropertyChanged, you need to add using System.ComponentModel. Luckily, Visual Studio will help you with this.

Next, you can let Visual Studio do some more work for you. After adding the using System.ComponentModel statement, right-click INotifyPropertyChanged and select Implement Interface ➤ Implement Interface from the pop-up menu

Now Visual Studio has added a new public event to your class:


public class Book : INotifyPropertyChanged
{
public string Title { get; set; }
public string ISBN { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
}

8. Next, you need to create a convenience method that will fire the PropertyChanged event. Call it FirePropertyChanged, as shown in the following code:


public class Book : INotifyPropertyChanged
{
public string Title { get; set; }
public string ISBN { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
void FirePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(property));
}
}
}

9. Now you need to extend the simplified properties by adding private members and full get/set definitions to define the get and set operations, as shown in the following code. The get is just like a normal get operation, where you simply return the internal member value. For the set, you first set the internal member value, and then call the FirePropertyChanged method, passing it the name of the property.


public class Book : INotifyPropertyChanged
{
private string _title;
private string _isbn;
public string Title
{
get
{
return _title;
}
set
{
_title = value;
FirePropertyChanged("Title");
}
}
public string ISBN
{
get
{
return _isbn;
}
set
{
_isbn = value;
FirePropertyChanged("ISBN");
}
}
public event PropertyChangedEventHandler PropertyChanged;
void FirePropertyChanged(string property)
{
if (PropertyChanged != null)
{
PropertyChanged(this,
new PropertyChangedEventArgs(property));
}
}
}

With this completed, your class is set up to notify bound clients of changes to the Title and ISBN properties. But you still need to take one more step. By default, when you bind a source to a target, the BindingMode is set to OneWay binding, which means that the source will send the data to the target, but the target will not send data changes back to the source. In order to get the target to update the source, you need to implement two-way (TwoWay) binding.

10. To change to two-way binding, add the Mode=TwoWay parameter when defining the {Binding} on a control, as follows:


<TextBlock Text="Book Title"
VerticalAlignment="Center"
Margin="5" />
<TextBlock Text="ISBN-13"
VerticalAlignment="Center"
Margin="5"
Grid.Row="1" />
<TextBox Text="{Binding Title, Mode=TwoWay}"
Height="24"
Margin="5"
Grid.Column="1" />
<TextBox Text="{Binding ISBN, Mode=TwoWay}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="1" />
<TextBlock Text="Book Title"
VerticalAlignment="Center"
Margin="5"
Grid.Row="2" />
<TextBlock Text="ISBN-13"
VerticalAlignment="Center"
Margin="5"
Grid.Row="3" />
<TextBox Text="{Binding Title, Mode=TwoWay}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="2" />
<TextBox Text="{Binding ISBN, Mode=TwoWay}"
Height="24"
Margin="5"
Grid.Column="1" Grid.Row="3" />

11.  Rebuild and run your application. Update any of the fields, and leave the focus on the control. You’ll see that the two-way binding is triggered, and the corresponding field is also updated,

Congratulations! You have just created a Silverlight application that allows for two-way data binding.

Posted in ASP.NET, Silverlight | Tagged: , , , , , | Leave a Comment »

 
%d bloggers like this: