.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Post New Web Links

How to write good code?

Posted By:      Posted Date: October 21, 2010    Points: 0   Category :ASP.Net

These are several general questions about writing code behind.

I already read some best practices for asp.net

The questions are:

1. Do I need to use interfaces and/or delegates for simple applications? I just cannot get in simple terms WHEN to use them. (I assume that delegates are not necessary is Asp.net, because they are by default)

2. Do we need to turn off viewstate whenever possible? Is it crucial for perfomance?

3. If I retrieve value from database on page load, how do I pass this value to other functions? For example:

protected void Page_Load(object sender, EventArgs e)
        int GameID = db.Games.Where(p => p.IsActive == true).Select(p => p.GameID).SingleOrDefault();

Do I just copy this statement to different function? for example: 

protected void Button1_Click(object sender, EventArgs e)
        int GameID = db.Games.Where(p => p.IsActive == true).Select(p => p.GameID).SingleOrDefault();

Or I use session variables for this? Or I make this Int GameID Global variable?

4. When do i need to create separate files for classes? How many events should I have? Is it crucial to use classes in separate files?

5. Is enabling/disabling/hiding controls from code b

View Complete Post

More Related Resource Links

Share Code: Write Code Once For Both Mobile And Desktop Apps


If you're building .NET client apps already, target them to Windows Mobile using the same skills and toolsets.

Daniel Moth

MSDN Magazine July 2007

Wandering Code: Write Mobile Agents In .NET To Roam And Interact On Your Network


In artificial intelligence, an agent is a logical entity that has some level of autonomy within its environment or host. A mobile agent has the added capability that it can move between hosts. In this article Matt Neely brings mobile agents from the halls of academia to a dev shop near you.

Matt Neely

MSDN Magazine February 2006

{ End Bracket }: What Makes Good Code Good?


When MSDN Magazine asked me to write a page on something I care about, I said, "You mean, like abolishing taxes, car phones, and SUVs?" Alas, they meant something to do with programming. Well! After pondering, I realized that something I care about is writing good code.

Paul DiLascia

MSDN Magazine July 2004

C++: Write Faster Code with the Modern Language Features of Visual C++ 2005


The next version of Visual C++ has a new syntax that is both elegant and powerful. It has new optimization technology that has improved the speed of Microsoft. It has new compilation modes that ensure Common Language Infrastructure compliance and verifiability for the .NET Framework, and it has new models for interop. In this article Stephen Toub explains these and other improvements to Visual C++.

Stephen Toub

MSDN Magazine May 2004

Is CTP4 good enough to be used in production code



Can we use CTP4 in production code?

When will it be included in .net framework 4.0?

Code write file asp.net 2.0 is working in firefox but IE not working.

If Not IsDBNull(ext) Then                                        ext = LCase(ext)                                    End If                                    Select Case ext                                        'Case ".htm", ".html"                                        '    type = "text/HTML"                   


HI. I NEED LITTLE HELP. I have values 1000,2000,3000, wich located in numValue. Now I have sql table CUSTOM1 with column CUSTOM_ID where located numbers 1000,1001,2005,2006 .. Now if I choose( NumValue == 1000) go to sql table and find Maximum number wich begin with 1000 and add 1 = Result must be 1002 there is my code what change I must do in order to receive the num max + 1?       DataTable dt = new DataTable();         SqlConnection con = new SqlConnection("Data Source=DYS-PC;Initial Catalog=Veolia;Integrated Security=True");         SqlCommand cmd = new SqlCommand("Select Count(*) " + "FROM [MHGROUP].CUSTOM1 " + "WHERE CUSTOM_ALIAS = '" + comboBox2.Text + "'", con);         SqlDataAdapter sqlDa2 = new SqlDataAdapter();         sqlDa2.SelectCommand = cmd;         sqlDa2.Fill(dt);      

how to write a code for browser compatible in asp.net 2.0

how to write a code for browser compatible in asp.net 2.0 How to write a code in asp.net for all browser compatible  

Where to write the code for mail in master page

below is my code. please anyone tell me where to write vbscript to send email after validating the fields<%@ Page Language="VB" MasterPageFile="~/Master.master" AutoEventWireup="false" CodeFile="Enquiry.aspx.vb" Inherits="Enquiry" title="Enquiry - MTM Active" %><asp:Content ID="Content1" ContentPlaceHolderID="ConPage" Runat="Server"><script type="text/javascript" src="Js/Blank.js"></script><script type ="text/javascript">function CmdSubmit_onclick(){if (fnValidate()==true)return true;else return false;}function fnValidate() {if(IsBlank(" Name",document.getElementById ("TxtName").value)==false)        {            document.getElementById ("TxtName").focus();            return false;         }        if(IsBlank(" Email",document.getElementById ("TxtEmailID").value)==false)        {            document.getElementById ("TxtEmailID").focus();            return false;    &n

HOW can i write code for scrolling images in asp.net

plz help me in this topic in one perticular area the image should scroll for every 45 sec's

write server side code in External JS files

Salam To All,for my JS code, some times i need to organize my code, so i prefer to put some of javascript functions in external js files and  include it in my page as well.but i am face a problem if i need to write some .Net code especially to Get the Client ID for any of .net controls.so do you have a way to write the following code in External JS file?('#<%= this.ddlMake.ClientID %>').change(function(

how write code in my javascript function and modify it?


hi there
i have a function in my javascript code:

   <script language="Javascript">
  function showAddress(address)

          if (geocoder) 

actually, i set GeoMap variable from asp.net
the problem is that when GeoMap change value, the script does not change!
so, the button who change this value is in a UpdatePanel, does this got problem?

how can i do this?

how to write solid code for this case


here is a common scenario:

insert into table A

select xx, xx, xx

from table B -- where B has the rows we want to insert, but need to join to table C to get some more info

inner join table C

on ......

the problem is, if there is any missing data in table C, we might lose data.  because of the missing data. but using left join will also cause issues for possible NULLs etc.

is there a good coding practice that will do the insertion and check for possible issues, for example, break and raise error if i find missing data becuase of the joins?



Any better than jquery dynamically write html code?


I am creating a page which contains one grid showing the content from a very slow data source. There are other faster contents to be displayed too.

I know I can use jquery to dynamically generate the html code from a dedicated controller for the slow data source. 

However, it's not intuitive to read the jquery code for the grid. is there any better way?

How to re-write this code... Using Array instead of double[]


This is a partial piece of code that I am using and would like to re-write it so that it can use Array instead of double[].  Please assist.

Original Code

public double NormalizeValues(double[] _equivTable, double _userResult) //takes the Equivalence Tables(array) and User result(integer)

            //determine if it is a descending table 
            //For when a better score is = to a lower numbered result (i.e. running a race)
            double _linearInterpolation;

            if (_equivTable[0] > _equivTable[_equivTable.Length - 1])
                for (int i = 10; i >= 0; i--)
                    if (_userResult <= _equivTable[i])
                        if (i == 10) //This is here in case the $i_UserResult's Magnitude (Score/$aEquivTable[$iCC]) is worth a 10
                            return _linearInterpolation = 10;
                            return _linearInterpolation = LinearInterpolation(i, _equivTable, _userResult);
                return _linearInterpolation = 0; //Not Sure why this is here, but I think it is needed, in case it iterates past 0

How to write JDBC code with UDT?


Exception in thread "main" com.microsoft.sqlserver.jdbc.SQLServerException: The conversion from UNKNOWN to UNKNOWN is unsupported.

      at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:171)

      at com.microsoft.sqlserver.jdbc.DataTypes.throwConversionError(

How do I write code to show message box when data is not available in database


Hello everyone,

I have successfully developed the C# code that will enable me search the SQL database for text box text and display the result in a datagrid view. When the text box text is not found in the database, an empty row is displayed in the datagrid view.

However, I am at cross roads on how to write and add C# code such that a message box will pop-up if and when the text box text is not found in a data base table.

Below is my code so far:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlTypes;

namespace RidersCard1
  public partial class SearchDatabase : Form
    #region Global Declarations

    // Global declaration for SQL 
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend