Posts

Showing posts with the label .Net Tips and Tricks

ASP.Net: How to Find Which Control Caused Postback?

At times in a webpage, we can have many controls that can possibly "Post-back". It can be a PITA to find which control actually posted back if the actual controls are dynamically created. So to find exactly which control posted back, a code snippet is given below which should be added to the page_load method of webpage: protected void Page_Load(object sender, EventArgs e) { if (Page.IsPostBack) { //Creates the dynamic button. the methods is given below addSaveButton(); string ctrlname = Page.Request.Params.Get("__EVENTTARGET"); Control control = null; control = Page.FindControl(ctrlname); if (control == saveButton) //Do whatever you what to do! } } void addSaveButton() { saveButton = new Button(); saveButton.Text = "Save"; saveButton.ID = "sb_1"; saveButton.UseSubmitBehavior = false; saveButton.Command += new CommandEventHandler(saveButton_Click); saveButton.CommandName = "saveButton"; } So that was a piece ...

C#: Converting a Datatable to XML

Given a .Net datatable filled with data and an explicitly defined name and columns, we can extract it's XML in the following manner.  public static XmlDocument ConvertToXML(DataTable dt)         {                     string sXML;            using (StringWriter sw = new StringWriter())            {                dt.WriteXml(sw);                sXML = sw.ToString();            }            //Althought the following code is also working fine            //MemoryStream mstr = new MemoryStream();            //dt.WriteXml(mstr, true);            //mstr.Seek(0, SeekOrigin.Begin);           ...

Getting JSON output from ASP.Net WebMethod

Althought ASP.Net Webservice provides a very convenient way to develop a Webservice but it just returns XML. At time we need this webservice to return JSON. Here is a tiny trick that makes an XML based webmethod to rueturn JSON. [ScriptMethod(ResponseFormat = ResponseFormat.Json)] [WebMethod(Description = "To gets a list of available customers")]     public string GetCustomerList(string strToken)     {         try         {             SPCustomers objCust = new SPCustomers();                       ArrayList arlstCusomers = new ArrayList();             arlstCusomers = objCust.GetList();             JavaScriptSerializer js = new JavaScriptSerializer(arlstCusomers);             string strJson = js.Serialize();         ...

Creating a Datatable out Array Rows in .Net

At certain point in time, we do come across a situation where we apply select filter on a Data table and get a array of rows. Now manipulating these rows is quite difficult for instance if we want these rows to act as a data source for a data grid then it becomes really trouble some kind of process. The function below FilterTable() gets data table and a filter string as an argument and returns a data table containing rows that satisfy the filter string. Consider the case below where I have a datagrid and FilterTable () with its agrugument at the other end of assignment operator.(The code below is in C#) dgSearch.DataSource = FilterTable(dsEvaluate.Tables[0], "IsValid like 'No' ") ; private DataTable FilterTable(DataTable dt, string filterString) { DataRow[] filteredRows = dt.Select(filterString); DataTable filteredDt = dt.Clone(); DataRow dr; foreach (DataRow oldDr in filteredRows) { ...

Adding a Report to the webpage

First of all there has to be crystal report behind the scene with a Store procedure working on its back. Now to dynamically provide Paramater values to the cystal report here is the code to be followed documentReport = New ReportDocument() reportPath = Server.MapPath("MonthlySalesReportCustomerwise.rpt") documentReport.Load(reportPath) Dim dtDocument As Data.DataTable dtDocument = Report.GetSales_CustomerWise(Common.GetDBDate(tbxFrom.Text), Common.GetDBDate(tbxTo.Text), ddlCustomerType.SelectedValue) documentReport.Database.Tables(0).SetDataSource(dtDocument) 'Get the collection of parameters from the report Dim crParameterFieldDefinitions1 As CrystalDecisions.CrystalReports.Engine.ParameterFieldDefinitions crParameterFieldDefinitions1 = documentReport.DataDefinition.ParameterFields Dim crParameterFieldDefinitions2 As CrystalDeci...

Show Progress bar on Mouse Cursor

asp : UpdateProgress ID ="UpdateProgress1" runat ="server" AssociatedUpdatePanelID =" upMain" DynamicLayout ="false" DisplayAfter ="0"> ProgressTemplate > div id ="updateDiv" style =" position : absolute"> asp : Image ID ="Image1" runat ="server" ImageUrl ="~/App_Themes/ Default/images/busy.gif" /> div > ProgressTemplate > asp : UpdateProgress > script type ="text/javascript" language ="javascript"> var IE = document.all? true : false // If NS -- that is, !IE -- then set up for mouse capture if (!IE) document.captureEvents(Event. MOUSEMOVE) // Set-up to use getMouseXY function onMouseMove document.onmousemove = getMouseXY; // Temporary variables to hold mouse x-y pos.s var tempX = 0 var tempY = 0 // Main function to retrieve mouse x-y pos.s function getMouseXY(e) { if (IE) { // grab the x-y pos.s if browser...

Code Project is the best

http://www.codeproject.com/KB/cs/CustomTaskManager.aspx

Wonders of Javascript

For the first I had to use javascript from Server side. this is how I did the job.I had a datatable fetching a rows from a stored procedure. these records where being added to the table control and then dtClaim = SCMERP.Sale.Claims_GetbyClaimID(ddlClaim.SelectedValue) If dtClaim.Rows.Count = 0 Then Else strClientScript &= "var TotalClaims = (" & dtClaim.Rows.Count & ");" & vbNewLine strClientScript &= "function UpdateDND(){" & vbNewLine strClientScript &= "var TotalDND= 0, i=0;" & vbNewLine strClientScript &= "for(i=0;i strClientScript &= "var tbxSaleable = document.getElementById('ctl00_BodyContent_tbxSaleable'+i ).value;" & vbNewLine strClientScript &= "var tbxQty = document.getElementById('ctl00_BodyContent_lblQuantity'+i ).value;" & vbNewLine ...

Working with Transaction

At times when you are inserting records into the database involving more than one table, it is possible that some errors occurs and records is not properly inserted. To prevent the damages, one needs define the transactions. So this is how I defined the transaction Dim connection As New SqlClient.SqlConnection(Common.GetConnectionString) connection.Open() Dim trans As SqlClient.SqlTransaction Try Dim dtDND As New DataTable dtDND = CType(ViewState("getDND"), DataTable) 'dtDND = SCMERP.Sale.getDND(tbxFromDate.Text, tbxToDate.Text) Dim ClaimID As String trans = connection.BeginTransaction Dim DocumentNo As String DocumentNo = SCMERP.Purchase.GetDocumentNumber(Common.DocumentTypes.Sale, Common.BranchID) If ddlStockType.SelectedValue = Common.StockType.DND Then ClaimID = SCMERP.Purchase.SetDocument(trans, 10, DocumentNo, "12/12/2008", Common.Bra...