Uğur UMUTLUOĞLU
Web Technologies, Microsoft and .NET Technologies

Listing Assembly Files in GAC in Normal View

Tuesday, 6 July 2010 17:35 by ugur

We need to back up the files in the production environment especially when we update our projects in this environment. There is not any problem to reach the files under IIS but we don’t have a chance to copy the dll files inside the GAC(Global Assembly Cache) because Windows lists these files in a different view. If you want to reach the dll files inside the GAC(Global Assembly Cache) directly, the command line that will be written on MS-DOS screen will help you. 

regsvr32 –u C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\shfusion.dll

By this command, the assembly files inside the GAC are listed in normal view. After you back up the files, if you want to switch to old view you must use the same command without using –u parameter as given below;

regsvr32 C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\shfusion.dll

PS: Sometimes you can see that the files aren’t listed properly although you execute the second command. In this case, you can try to refresh the page or close and reopen the page.

Tags:   ,
Categories:   .NET Framework
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

C# 4.0 Dynamic Type - Reflection Speed Test

Tuesday, 8 June 2010 19:04 by ugur

At first view, you can think that dynamic types come with C# 4.0 are only used for to make a type written outside the .Net environment applicable in .Net environment. However, we can achieve important performance earnings when we use Dynamic types instead of Reflection types while coding by using Reflection types. Therefore, it makes sense to use Dynamic types instead of Reflection types in the applications developed by using C# 4.0.  

In the example below, we have two classes named Music and Video that apply IMedia interface. In this simple console application I prepared, I created Music and Video objects and I called generic PlayMedia<T> method that performs Play operations of these objects. Depending on the scenario, in order to make the use of reflection or dynamic type necessary, I designed this application that any object that includes Play method can be used in PlayMedia method. The Play method of media object which’s type is uncertain during the compiling is called by using dynamic type in the lines numbered 20 and 21 of PlayMedia method. If you want to test the usage of Reflection types, you can turn the lines 20 and 21 to comment lines and remove the comment tags in the line numbered 19. First, I see the results by operating only the line 19. Then, when I operate the lines 20 and 21, the difference between the results is highly impressive :)

Reflection is trying to reach the members included in a type such as method and field by analyzing the structure of the type. However, using dynamic type can reach the result faster because dynamic type achieves the intended member directly without analyzing the type. Anyway, you can see the speed difference if you test this issue as given below.

The class diagram of the types used in the example is given below.

    1 class Program

    2 {

    3     static void Main(string[] args)

    4     {

    5         Music m = new Music { FileName = "amazing.mp3" };

    6         Video v = new Video { FileName = "avatar_fragman.wmv" };

    7 

    8         PlayMedia<Music>(m);

    9         PlayMedia<Video>(v);

   10         Console.ReadLine();

   11     }

   12 

   13     static void PlayMedia<T>(T media)

   14     {

   15         DateTime d1, d2;

   16         d1 = DateTime.Now;

   17         for (int i = 0; i < 1000000; i++)

   18         {

   19             //media.GetType().GetMethod("Play").Invoke(media, null);

   20             dynamic d = media;

   21             d.Play();

   22         }

   23 

   24         d2 = DateTime.Now;

   25         TimeSpan t = d2 - d1;

   26 

   27         Console.WriteLine(t.TotalSeconds);

   28     }

   29 }

As can be seen from the test results, the process done upon 1.000.000 record by using reflection type takes 1.85 seconds whereas when I use dynamic type, it takes average 0.17 seconds. Therefore, we can achieve the result 10 times faster when we use dynamic type. Of course, different results can be obtained in different scenarios but we can say that dynamic type is clearly faster than reflection types. It is obvious that, dynamic type is a better choice in terms of performance in these types of processes.     

Tags:   , ,
Categories:   C#
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Microsoft MVP Award in 2010

Monday, 12 April 2010 19:13 by ugur
MVP LogoI was awarded again MVP(Most Valuable Professional) title under ASP.NET category in 2010 as a result of my studies and sharing information in the last one year. Thus, I owned this title for the second time.

I thank everyone who shares my success and helped me in a manner to earn this title.

Caching More Than One Table with SqlCacheDependency

Monday, 14 December 2009 19:31 by ugur

SqlCacheDependency object provides caching according to updates in database in ASP.NET applications. We can use this object which is independent from time and provides us important gains according to speed and performance, for caching records from one table. But, in most of the applications by using JOIN statements, we can match records of different tables. Well, how can we do caching operation by SqlCacheDependency object in this kind of situation? Because, depending on its structure, this object was envisioned to work with only one table. The answer of the question: AggregateCacheDependency object (If I am not wrong, comes with .NET Framework 2.0). AggregateCacheDependency object will remove the information of cache object stored in memory in case the state of one of the objects changes and will make caching operation successfully be done by keeping more than one CacheDependency objects inside. The only thing we must do is that adding SqlCacheDependency objects formed for each table to AggregateCacheDependency object by using Add method and adding this object to Cache by using Insert method. You can see how to do this operation below by following these code snippets.

SqlCacheDependency dep1 = new SqlCacheDependency("dbCon", "Products");

SqlCacheDependency dep2 = new SqlCacheDependency("dbCon", "Orders");

SqlCacheDependency dep3 = new SqlCacheDependency("dbCon", "Customers");

AggregateCacheDependency aggDep = new AggregateCacheDependency();

aggDep.Add(dep1,dep2,dep3);

Cache.Insert("ProductOrder", dt, aggDep);


SqlCacheDependency provides us to do caching operations in ASP.NET applications very efficiently. But, the important point that must be considered in caching operations is that if we store large amount of data in Cache and system resources aren’t sufficient, then OutOfMemoryException error might be occurred. So, both errors and to take up a lot of space in memory is not good for the performance of the application. Because of this reason, the size of the data that will be stored in Cache is an important criterion for us, we shouldn’t forget this detail.

Paging in ListView Over QueryString

Thursday, 16 July 2009 11:11 by ugur

ListView that comes with ASP.NET 3.5 presents considerably a flexible structure. In addition to all operations of GridView, also Insert operation can be done by using this control. On the other side, we can compose the HTML output as the way we want. To perform this kind of process was nearly impossible in GridView.

Another advantage of ListView is that we can do paging operation over QueryString! ListView control does the paging operations on a control named DataPager(DataPager has also come with ASP.NET 3.5). ListView control that is formed ordinarily do the paging operations by LinkButtons included in DataPager; thus JavaScript functions called as a result of Postback operation help doing paging operation. So, there won’t be any change in the URL of the page.  If we want paging operation to be done by a value like PageNo carried on QueryString as UrunListele.aspx?SayfaNo=3, the only thing we must do is that to assign a value like SayfaNo to the QueryStringField property of DataPager control. I also want to mention about this property; you can define how many records will be seen in one page by using PageSize property of DataPager.  The changes made in DataPager control and an example screen output can be seen below.

<asp:DataPager ID="DataPager1" runat="server" QueryStringField="SayfaNo" PageSize="15">

    ...

</asp:DataPager>


Actually, it is a very important and useful tip for users to reach the pages they desired over QueryString and for search engine improvement(SEO).

Tags:   , ,
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Using More Than One DefaultButton in a Page

Wednesday, 1 July 2009 08:52 by ugur

We can assign the ID value of a button control to DefaultButton property in <form> element in order to send the form information filled by the user to the server easily by button object in ASP.NET pages. By this way, the information given in the form can be sent to server when user push Enter key from the keyboard without clicking the button. If there are different forms are included in our page (for example there is a login form at the left of the page and sign up form at the right of the page), how can we make the related button clicked when the user pushes Enter key from the keyboard after filling one of the forms? We can handle this situation again by using DefaultButton property but not the DefaultButton property of <form> element!

Firstly, we must locate both of the forms in Panel controls for solving this situation. Actually, Panel control has DefaultButton property too. Therefore, the forms will function as we want, if we assign the ID value of the button that is included inside of the panel and will do the Submit operation, to the DefaultButton property of the relevant Panel. If you have another form besides these panels and if you want the submit button in this form to be triggered when the Enter key is pushed outside the panels, you can use DefaultButton property of <form> element anyway.  When you test the code snippet given below in your project, you can clearly see the situation.

Default.aspx
<
form id="form1" runat="server" defaultbutton="button1">

    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

    <asp:Button ID="button1" runat="server" Text="Button-1" OnClick="btn_Click" />

    <br />

    <asp:Panel ID="panel1" runat="server" BackColor="LightGreen" DefaultButton="button2" Width="300">

        <asp:TextBox ID="text1" runat="server"></asp:TextBox>

        <asp:Button ID="button2" runat="server" Text="Button-2" OnClick="btn_Click" />

    </asp:Panel>

    <asp:Panel ID="panel2" runat="server" BackColor="LightBlue" DefaultButton="button3" Width="300">

        <asp:TextBox ID="text2" runat="server"></asp:TextBox>

        <asp:Button ID="button3" runat="server" Text="Button-3" OnClick="btn_Click" />

    </asp:Panel>

</form>



Default.aspx.cs

protected
void btn_Click(object sender, EventArgs e)

{

    Response.Write("Clicked button: " + ((Button)sender).Text);

}

Tags:  
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

CheckBoxList: Select All / Clear All

Wednesday, 24 June 2009 23:17 by ugur

Actually it is not related to ASP.NET directly but this code snippet could be useful ASP.NET programmers anyway. Scenario is something like that; we have many record listed in CheckBoxList and we want all of the records can be selected or selected items can be deleted by one move. We can handle this easily by using a simple JavaScript function that is seen below.

<script language="javascript" type="text/javascript">

    function ListeSecim(kontrol, durum) {

        var ctl = document.getElementById(kontrol);

        var inputs = ctl.getElementsByTagName('input'); //Bring input elements in CheckBoxList

        //Assign checked property of all input elements in CheckBoxList to the value in state variable (true/false)

        for (var i = 0; i < inputs.length; i++)

            inputs[i].checked = durum;

    }

</script>

Of course, additionally we need links that will do selecting and deleting all items. We will call the function above by using these links. The only difference between these two links is the value of second parameter that will sent to ListSelect function.

<a href="javascript:ListeSecim('CheckBoxList1', true)">Select All</a>&nbsp;

<a href="javascript:ListeSecim('CheckBoxList1', false)">Clear All</a>

 

Tags:   ,
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Using More Than One Sitemap File in ASP.NET Applications

Wednesday, 6 May 2009 23:14 by ugur
When we use navigation controls with SiteMapDataSource in ASP.NET applications, we can use only one sitemap file. The reason of this situation is that, the web.config file included in the directory in which .NET Framework is installed that is used as an inheritance by web.config file in the applications has only one SiteMap provider definition. If you look <siteMap> nodule included in web.config file in C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\CONFIG directory in a computer in which .NET Framework 2.0 has been installed, you can see only one provider defined and there is web.sitemap file name that has siteMapFile attribution. Actually, we can solve how to use more than one sitemap file starting in here. If we want to add second sitemap file to our application and use it with navigation controls, firstly we should make a new sitemap provider definition in the web.config file of our application. You can see the code snippet that will be added to <system.web> nodule in web.config file below.

web.config
<
system.web>

    ...

    <siteMap>

        <providers>

            <add name="SiteMapProvider2" siteMapFile="web2.sitemap" type="System.Web.XmlSiteMapProvider, System.Web, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"/>

        </providers>

    </siteMap>

</system.web>

We defined a new siteMap provider named SiteMapProvider2 that will work with web2.sitemap file. We can now use another sitemap file named web2.sitemap beside the web.sitemap file in this application. We are testing the use of more than one sitemap file by adding two Menu controls and two SiteMapDataSource controls to the ASP.NET page. The point that must be paid attention in here is equaling the SiteMapProvider property of SiteMapDataSource control that will be connect to web2.sitemap file to the SiteMapProvider2 defined in web.config.

 

Default.aspx

<asp:Menu ID="Menu1" runat="server" DataSourceID="SiteMapDataSource1"></asp:Menu>

<br />

<asp:Menu ID="Menu2" runat="server" DataSourceID="SiteMapDataSource2"></asp:Menu>

<asp:SiteMapDataSource ID="SiteMapDataSource1" runat="server" ShowStartingNode="False" />

<asp:SiteMapDataSource ID="SiteMapDataSource2" runat="server" ShowStartingNode="False" SiteMapProvider="SiteMapProvider2" />

Tags:   ,
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (1) | Comment RSSRSS comment feed

Microsoft MVP Award

Friday, 10 April 2009 03:43 by ugur

Microsoft awarded me Most Valuable Professional (MVP) prize under ASP/ASP.NET category on account of studies conducted by me within the previous year. It is a really honorable event that the articles I have published in community websites and magazines, seminars, webcasts and training sessions I have conducted and volunteerly sharing information with all those people reached me via e-mail are awarded with such a valuable prize by Microsoft.  

I owe thanks to all of the people that always guide me, help me and in my opinion that have a share in this prize. Thanks to Microsoft!

 You can reach my MVP Profile here.

Delay Loading in ASP.NET AJAX

Thursday, 29 January 2009 09:01 by ugur

You have content that will be uploaded synchronously in your page and content that will be uploaded asynchronously in UpdatePanel. However, the upload process of UpdatePanel content may take more time… If I give more concrete example, the upload process of normal content may take 2 seconds and the upload process of asynchronous content may take 7-8 seconds. If we wait for the whole page uploaded under normal circumstances and then send to the client, we will keep the client waiting at least 7-8 seconds. Well, what if we send the synchronous content to the browser of the client during the first upload of the page while the UpdatePanel continues to upload its own content? I guess you are curious about how to do such a process. In this post, we will search for how to delay loading process in ASP.NET AJAX applications.

First, the needed ingredients; 1 ScriptManager, 1 Label (for synchronous content), 1 UpdatePanel, 1 Button inside the panel and 1 more Label for asynchronous content (if we want our page more attractive, 1 UpdateProgress). The first label will hide the normal content, in other words this control will go to the client after the first upload. The other label is for asynchronously updated content, in other words the upload process in here will be performed just after the page reached to the client. The HTML codes and the code snippet that we will use to update the UpdatePanel asynchronously are given below.

Default.aspx

<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

<asp:Label ID="lblStatik" runat="server"></asp:Label>

<br /><br />      

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

    <ContentTemplate>

        <asp:LinkButton ID="lbGuncelle" runat="server" Text="Güncelle" onclick="lbGuncelle_Click"></asp:LinkButton><br />

        <asp:Label ID="lblAsenkronIcerik" runat="server"></asp:Label><br />

        <asp:UpdateProgress ID="UpdateProgress1" runat="server">

            <ProgressTemplate>

                Loading. Please wait...

            </ProgressTemplate>

        </asp:UpdateProgress>

    </ContentTemplate>

</asp:UpdatePanel>


Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)

{

    lblStatik.Text = "Static content will be here";

}

protected void lbGuncelle_Click(object sender, EventArgs e)

{

    System.Threading.Thread.Sleep(3000); //For delaying 

    lblAsenkronIcerik.Text = DateTime.Now.ToString();

}

As can be seen the LinkButton control named lbGuncelle updates UpdatePanel content. The page as it is will not create any output in UpdatePanel after the first upload process. But, if we make the LinkButton automatically postbacks when the page is uploaded at the first time, at the same time UpdatePanel content will reach to the client. If we can perform postback process at the same time with the upload of the page at the client side, the upload process will be delayed. There are two questions to be answered in here; first, "how can I catch the exact moment of upload of the page in the client side?", second, "how can I trigger the partial postback process without clicking LinkButton?" Because both of these processes eventuate at the client side, JavaScript must be used. At the moment that the page is loading, the pageLoad function of JavaScript is stepped in. On the other hand, one of the other functions of JavaScript named __doPostBack is used for to apply postback process to a control programmatically. The JavaScript pageLoad function given below will provide the delay in loading the UpdatePanel content during the loading of the page at the first time.

Default.aspx

<script language="javascript" type="text/javascript">

    var firstLoad = true;

    function pageLoad()

    {

        if(firstLoad)

        {

            __doPostBack('lbGuncelle', ''); //Programatically postback for LinkButton control

            firstLoad = false;

        }

    }

</script>

pageLoad function executes while the page is loading to the client. Because the firstLoad variable is true at this moment, LinkButton control makes the postback process and UpdatePanel content is started to be updated. Because we delay loading with Sleep method of Thread class, we can see the delay in updating UpdatePanel content clearly when the page is uploaded to the client.

Tags:   ,
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (5) | Comment RSSRSS comment feed

ASP.NET MVC 1.0 RC Has Been Released

Wednesday, 28 January 2009 11:12 by ugur

ASP.NET MVC 1.0 RC has been released today, after Internet Explorer 8.0 RC1 that was released yesterday. This important announcement excited me like many ASP.NET developers. You can follow the links above to get more detailed information about RC version. 

Scott Guthrie - ASP.NET MVC 1.0 Release Candidate Now Available
David Hayden - Download ASP.NET MVC 1.0 Release Candidate

Opening New Window with Response.Redirect() Method

Thursday, 15 January 2009 23:47 by ugur

In some occasions, we can direct the user to a specific page by using Response.Redirect method in the code-behind side. Well, how can we make the directed page to be displayed in a new browser window? In fact, the answer of this question is not related to Response.Redirect method. This instance can be solved completely by an operation done on the trigger control of this method. For example, if button control carry out the direct process, then the code snippet can be seen below will be enough. By the way, I think it isn't necessary to mention that Button1 control is involved in the form element named form1:)

<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Click Me" OnClientClick="form1.target='_blank';" />

It will be adequate to add standard Redirect method to the Click event method of Button.

protected void Button1_Click(object sender, EventArgs e)

{

    Response.Redirect("Default.aspx");

}

But, how is the process done? form1.target='_blank' expression added in Button1 control and functions in client side transfers the operation to be done in a new window. Thus, the page makes postback on the new page and is directed to Default.aspx page.

Tags:  
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

"You tried to assign the Null value to a variable..." Error and Solution

Tuesday, 6 January 2009 12:44 by ugur
When you use a table formed in MS Access with AccessDataSource control in Insert processes, you might come across an error like "You tried to assign the Null value to a variable that is not a Variant data type". Even if there is not a null value in your query, this error will occur because the identity column of your table has been added to the Insert statement in Visual Studio as the data input column. The solution is actually very simple, you should remove the name and parameter (the ? character in Value region) of identity column (is usually primary key column) from InsertCommand property of AccessDataSource control in HTML codes. At last, you can successfully execute Insert comment after you have removed the parameter added for identity column in InsertParameters collection.
Tags:   ,
Categories:   ASP.NET
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Microsoft Products' Codename History

Monday, 22 December 2008 22:37 by ugur

As you know, Microsoft announces a lot of products that are in beta phase with specific codenames and makes us to warm towards future improvements earlier. If I want from you to list some of the codenames, which ones do you think of first? Whidbey, Orcas, Yukon, Longhorn etc...

When I examined a link that I came across in Wikipedia, I realized that Microsoft has been using codename as an important standard since years. You can both scan codename history and see the point where we are while time travelling from this page. Where is Windows 3.1, where is Windows Vista

Tags:  
Categories:   News&Announcements
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Showing XPS Documents with DocumentViewer Control in WPF

Saturday, 20 December 2008 02:11 by ugur

XPS is a file format that is started to be commonly used with the release of Windows Vista. Word, Excel, PowerPoint files can be translated into XPS format and can be displayed even in a computer in which Office is not installed via the help of an explorer program such as Internet Explorer 7. In this post, I will try to explain how to display XPS formatted files in WPF (Windows Presentation Foundation) applications with DocumentViewer control. 

We need an XpsDocument object in order to display a XPS file on DocumentView that is a control comes with WPF. Because this class is not included in mscorlib.dll, we must add ReachFramework.dll to our project.

Adding ReachFramework.dll to project's references
Adding ReachFramework.dll to project's references

We can move to our application part after adding related dll file to our project references. In this part the procedure is very simple actually; we add a DocumentViewer control to our application form and we add two code lines that can be seen below to the Loaded event of the application form.

Window1.xaml

<Window x:Class="WpfDocumentViewerXps.Window1"

   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

   Title="DocumentViewer ile XPS Dosyalarının Görüntülemesi" Height="320" Width="600">

    <Grid>

        <DocumentViewer Name="documentViewer1" />

    </Grid>

</Window>


Window1.xaml.cs

...

using System.Windows.Xps.Packaging; //Required namespace

namespace WpfDocumentViewerXps

{

    public partial class Window1 : Window

    {

        public Window1()

        {

            InitializeComponent();

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)

        {

            //With GetFixedDocumentSequence method, XpsDocument can get XPS file content

            XpsDocument xps = new XpsDocument(@"D:\test.xps", System.IO.FileAccess.Read);

            documentViewer1.Document = xps.GetFixedDocumentSequence();

        }

    }

}

 

XPS document can be seen on DocumentViewer control
XPS document can be seen on DocumentViewer control

 

Tags:  
Categories:   .NET Framework
Actions:   E-mail | del.icio.us | Permalink | Comments (8) | Comment RSSRSS comment feed

Inserting Item to DataBounded DropDownList

Thursday, 11 December 2008 09:57 by ugur

In the conditions that we load the records to DropDownList control from the database, all of us want the first choice of the DropDownList control a user informative text. I am sure; it is a necessity for every application to display the first item like “Choose a city”, “Choose a category” etc. choices and then list other items coming from the database. Actually, it is not a logic way to add a “Choose a city” record to the Cities table in the database; thus; we must create a different solution way for this issue! If we want to solve this issue in SQL Server side, UNION terms can be used. However, in this post, I want to discuss another different solution way.      

Actually, this process is a very simple and useful solution way. First, we add the informative text to the DropDownList control as a ListItem from the designer part. We add the informative text to the items collection and then we change the AppendDataBoundItems property of the DropDownList as true. At last, the added choice will be listed as a DropDownList control item.

<asp:DropDownList ID="DropDownList1" runat="server" AppendDataBoundItems="True"

    DataSourceID="SqlDataSource1" DataTextField="SehirAdi" DataValueField="SehirId">

    <asp:ListItem Value="-1">Bir şehir seçin</asp:ListItem>

</asp:DropDownList>

AppendDataBoundItems

 This is an alternative way for UNION and UNION ALL terms and we should keep this in our minds as a hint…

ASP.NET MVC Beta Released

Monday, 20 October 2008 23:08 by ugur

ASP.NET MVC made an important progress towards being a product and was marketed as beta version. This structure that can be used especially in small and large scaled projects, in which database operations are commonly done, officially will be ready for ASP.NET developers with 1.0 version in forthcoming months. Of course, it can be needed to wait release of .NET Framework 4.0 for properly stabilized ASP.NET MVC.

The innovations that at first I come across are, some improvements related to speeding up coding in Visual Studio and the addition of JavaScript libraries like ASP.NET AJAX, jQuery etc. to the Scripts folder that is now added to the project pattern. If I am not wrong, JQuery first started to be formally come to the fore with a Microsoft product. (You can be sure that JQuery will be more commonly known in the future.)

You can follow this link to install ASP.NET MVC Beta to your computer and http://www.asp.net/mvc link to get more detailed information related to ASP.NET MVC Beta.

Insert, Update & Delete in LINQ to SQL

Saturday, 9 August 2008 23:07 by ugur

Insert, Update & Delete in LINQ to SQL As you know, we can make queries among collection based objects in .NET environment with the language integrated queries named LINQ. The main purpose of these queries is to simplify the complicated processes by writing simple Select queries just as in T-SQL; instead of using foreach loops and if-else control structures. The main issue point of LINQ is to provide making simple and practical queries among enormous and complicated data objects (entity objects) in an application and to reduce one of the heavy weights of the software developers in extended projects.

One of the most frequently asked questions related to LINQ to SQL is how to carry out Insert, Update and Delete processes. I think it is not needed to be said that; it can not be made queries with the key words Insert, Update and Delete in LINQ statementsJ Thus, if the purpose is to work with the data objects in LINQ to SQL, somehow we need to perform these kind of actions. LINQ to SQL Classes (.dbml) files provide us to transform the data objects from database straightly to our application as classes. For example, if we form Product table included in Northwind database as data class in our application, the Products property included in DataContext object will return the records in Products table as the collection. Some methods of this property will help us to perform query statements like Insert, Update and Delete.

With InsertOnSubmit statement we can perform Insert act and with DeleteOnSubmit statement we can perform Delete act. Okay, but where is Update act? We will perform Update act by changing the values of the record or records return as LINQ query result. In order to transfer the results of these three acts to the database, it will be adequate to execute the SubmitChanges method of DataContext object. If you examine the code lines given below, you can see how to insert, delete and update data in LINQ to SQL. The Northwind object included in code snippet is our DataContext object.

Product addPr = new Product() { ProductName="Acer Aspire 5100", UnitPrice=1290, UnitsInStock=15, CategoryID=9 };

northWind.Products.InsertOnSubmit(addPr); // A new product added to insert list

 

Product updatePr = northWind.Products.First(u => u.ProductID == 69);

guncellenecekUrun.ProductName = "HP Pavilion 3355"; // ProductName property has changed for selected product

 

Product deletePr = northWind.Products.First(u => u.ProductID == 79);

northWind.Products.DeleteOnSubmit(deletePr); // Selected product added to delete list

 

northWind.SubmitChanges(); // Changes(insert/update/delete) has sent to database

Tags:   ,
Categories:   C#
Actions:   E-mail | del.icio.us | Permalink | Comments (0) | Comment RSSRSS comment feed

Confirmation of GridView's Delete and Update Commands

Friday, 11 July 2008 00:37 by ugur

We may want user confirmation before the completion of the operation during the deletion and update processes in GridView control. If we consider that, especially deletion process can cause fatal errors that can’t be fixed; it would be extremely right not to allow user to trigger this kind of process by clicking a button mistakenly. We can obtain user confirmation for Update and Delete commands by doing two very simple changes in GridView control.

At first step, we need to convert the CommandField which contains Update and Delete buttons to the TemplateField. In this way, we can access Update and Delete buttons and we can add JavaScript code to the necessary client event. For this, we should first click Edit Columns link from smart tag icon of GridView, then select CommanField column in the new window and click Convert this field into a TemplateField link which is below the Properties window that is positioned in the right. By this way, we can have access to the control definitions of buttons like Update, Delete and Cancel from the HTML codes.

At second step, we need to add a small JavaScript code to OnClientClick event of the button. Thus, confirmation process will be done directly in client side instead of passing to server side and an unnecessary postback process will be prevented. The code snippet below shows how to request confirmation from the user when Delete button is clicked. 

<ItemTemplate>

    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit" Text="Değiştir"></asp:LinkButton>&nbsp;

    <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete" Text="Sil" OnClientClick="return confirm('Silmek istediğinizden emin misiniz?');"></asp:LinkButton>

</ItemTemplate>

GridView can become useful to some extent after these simple two processes. The result can be seen below…

Problem and Solution in the Installation of SQL Server 2005 with Visual Studio 2008

Tuesday, 20 May 2008 21:32 by ugur

Today, I reinstalled Windows XP on my computer. The installations of Visual Studio 2008 and SQL Server 2005 were done without any trouble, however when I saw that some tools like Management Studio and SQL Profiler has not been installed, I searched on the Internet about the reason of this problem and how to solve this. The problem and the solution is given below:

Problem: When you install Visual Studio 2008 before installing SQL Server 2005, even if SQL Server is installed on your computer as a server, the components coming with SQL Server do not installed.

Solution 1: First install SQL Server 2005 and then install Visual Studio 2008.

Solution 2: If you do the installation by following the opposite order given in solution 1; execute the setup.exe included in Tools directory in SQL Server installation CD by using the parameters given below in Command Prompt. Complete the update installation process in the installation window by choosing necessary components from the Client Components and Documentation, Samples and … choice.

The command that will be written in Command Prompt: start /wait setup.exe SKUUPGRADE=1

Necessary components will be installed to your computer.

PS: The installation is performed on Windows XP Professional operation system by using Visual Studio 2008 Team System Edition and SQL Server 2005 Developer Edition versions.

 
Add to Technorati Favorites