Posts

Insert Update & Delete using Linq to sql classes

I have learnt last week Linq to sql and i amazed by its features.I would like to share my knowledge with you.To insert,update and delete you need to write lesser amount of code hence it saves development time. Following is the step you need to follow in order to insert,update and delete data of database using Linq to sql in Asp.Net. Open Visual Studio 2008, Click File >Website and choose ASP.Net Website. Choose the language of your choice and name your website according to your need. In solution explorer, right the project and select “Add New Item”. Select “LINQ to SQL classes” as shown in below figure. I have named it as DataClasses1. This will add an EmployeeInfo.dbml file inside “App_Code” folder.    In Visual Studio,   DataClasses1.dbml will have 2 panes. The left pane is for deigning the data objects and the right pane can be used for creating methods that operates on the data objects.The Visual Studio toolbar will contain a new toolbar for designing LINQ to SQL obje

Get LINQ to SQL results into a DataTable in asp.net

Linq to sql is a microsofts powerful tool to develop critical application faster.In Linq to sql the query result is in Generic list of generic  ienumerable type.  If you want result set in DataTable you need to convert this generic list result into datatable. To convert LINQ to SQL results into a DataTable use following Code DataClassesDataContext dc=new DataClassesDataContext(); var results = dc.products.ToList(); DataTable dt = new DataTable(); List<product> lst = results; PropertyInfo[] props= typeof(product).GetProperties(); foreach (var prop in props) { dt.Columns.Add(prop.Name); } foreach (var item in lst) { var rowa = new object[props.Length]; for (int i = 0; i < props.Length; i++) { rowa[i] = props[i].GetValue(item, null); } dt.Rows.Add(rowa); } Gr

Add Comma automatically while entering amounts in textbox using Javascript

Use following JavaScript function to add comma in numeric input in textbox function Comma(Num) { Num += ''; Num = Num.replace(/,/g, ''); x = Num.split('.'); x1 = x[0]; x2 = x.length > 1 ? '.' + x[1] : ''; var rgx = /(\d)((\d)(\d{2}?)+)$/; while (rgx.test(x1)) x1 = x1.replace(rgx, '$1' + ',' + '$2'); return x1 + x2; } Use following event of input textbox to call above function onkeyup = "javascript:this.value=Comma(this.value);"

Restore the lost focus of Auto post back controls in asp.net update Panel control

Problem: We put  autopostback controls like textbox, DropDownList controls inside in update panel to reducing the flickering of the Page.but when user uses tab for entering the details  the focus of the control losts. Web site users who prefer to use keyboard need to use mouse to activate appropriate input box or press TAB multiple times. Solution: Step 1: Save following in js file give any name to it (like MyUpdatePanelFocus.js ) var lastFocusedControlId = ""; function focusHandler(e) { document.activeElement = e.originalTarget; } function appInit() { if (typeof (window.addEventListener) !== "undefined") { window.addEventListener("focus", focusHandler, true); } Sys.WebForms.PageRequestManager.getInstance().add_pageLoading(pageLoadingHandler); Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(pageLoadedHandler); } function pageLoadingHandler(sender, args) { lastFocusedControlId = typeof (document.acti

Prompt user before navigate away from current page where user typed some text using javascript

In your application suppose user putting the data, then user accidently click the outside link or clode button of browser. In this situation user may loose his entered data. To avoid this we can propmt user about his unsaved data by using symple javascript code. This code will prompt the user about unsaved data in current page. following is the javascript code snippet  <html> <head> <title>window.onbeforeunload example</title> <script type="text/javascript"> var unsaved = false; window.onbeforeunload = function() { if (unsaved) { return "You have unsaved data. Are you sure you wish to leave this page."; } }; </script> </head> <body> <input type="text" id="input1" onkeydown="unsaved = true;"/> <input type="submit" id="input1" onclick="unsaved = true;"/> </body> </html> Try your self and let me know your c

Send bulk Email in asp.net using excel file

When you want to send the mail to multiple person email id and your data exists in the excel file like email address and his name. you can accomplish this task using asp.net and c#. In C# under System.Net.Mail and System.Net  namespace you can use classes which can help us to send mail effortlessly. I am giving some sample code to understand the concept Following is aspx design Page Sample < %@ Page Language =" C# " AutoEventWireup =" true " CodeFile =" LogDatails.aspx.cs " Inherits =" LogDatails " % > < !DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN " " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " > < html xmlns =" http://www.w3.org/1999/xhtml " > < head runat =" server " > < title > Untitled Page < / title > < / head > < body > < form id =" form1 " runat =" server " >

404 error on report viewer contorol in asp .net while hosting on IIS 7 issue solved

If you are facing the problem rendering report viewer elements while loading the report viewer on browser  Why it happens? The answer is IIS7 Handler Mappings does not contain Reserved.ReportViewerWebControl.axd httpHandler, and therefore unable to render the ReportViewer elements needed by the JavaSript.  What is the Solution? Open  Internet Information Services (IIS) Manager  and select your Web application. Under  IIS  area, double-click on  Handler Mappings  icon. At the  Action  pane on your right, click on  Add Managed Handler . At the  Add Managed Handler  dialog, enter the following: Request path:  Reserved.ReportViewerWebControl.axd Type:  Microsoft.Reporting.WebForms.HttpHandler Name:  Reserved-ReportViewerWebControl-axd Click  OK . What happens to your web application after you do above setting in IIS ? Reserved-ReportViewerWebControl-axd handler is now added to your Handler Mappings list. Notice that the following line has also been added to your

Encrypt web.Config sections using c# in asp.net

To give secuity to to your web application you need to make your applications secured you can encrypt the certain sections of web.config . To accomplish this task microsoft has provided library in the namespace System.Web.Configuration . To Encrypt the web.config section you can use the following code using System.Configuration.Provider; using System.Configuration.Assemblies; using System.Web.Configuration; Configuration confi = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); ConfigurationSection section = confi.ConnectionStrings; section.SectionInformation.ProtectSection("DataProtectionConfigurationProvider"); To Decrypt the web.config section you can use the following code using System.Configuration.Provider; using System.Configuration.Assemblies; using System.Web.Configuration; Configuration confi = WebConfigurationManager.OpenWebConfiguration(Request.ApplicationPath); ConfigurationSection section = confi.ConnectionStrings; section.Sec

Regular Expression for numeric field in Asp.net 2.0 & 3.5

Any new developer needed to understand importance of the validation in their application to avoid future errors or bug in application. For this purpose developer must ensure the user input to their application is proper and well handled to avoid future bug or error in the application. In the list of validation the important validation is numeric validation. in numeric validation user must enter or application must prompt the user to enter only numeric field. In asp.net we/developer normally use the Regular Expression Validation but proper regulion expression is as follows ^\d*[0-9](|.\d*[0-9]|,\d*[0-9])?$ Use this to restrict the user to enter Only numeric field give me ur feedback if any Enjoy

Javascript print() method not printing Images ? then no problem i have a solution

If you are using window.Print() method of javascript to print the page but it prints only letters and does not print images of the page then use following solution for this problem firstly try to use absolute url path in images src attribute like http://something.com/imger.jpg then it will be render correctly secondly copy and paste below code in script tag  function printContent(id){ var str=document.getElementById(id).innerHTML newwin=window.open('','printwin','left=0,top=0,width=1,height=1,menubar=no') newwin.document.write('<HTML>\n<HEAD>\n') newwin.document.write('\n') newwin.document.write('<script>\n') newwin.document.write('function chkstate(){\n') newwin.document.write('if(document.readyState=="complete"){\n') newwin.document.write('window.close()\n') newwin.document.write('}\n') newwin.document.write('else{\n') newwin.document.write('setTime