How to access the EmptyDataTemplate controls from OnRowCommand in GridView

For example – inserting value in the empty gridview:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
 if (e.CommandName == "Insert") {
  TextBox txt  =
    (((Control)e.CommandSource).NamingContainer).FindControl("txtTest") as TextBox;

    //do insert action
 }
}

Another way to do this is by getting EmptyDataTemplate table and its controls:

if (e.CommandName == "Insert")

{
   Table table = (Table)GridView1.Controls[0];
   TextBox txtTest= (TextBox)table.Rows[0].FindControl("txtTest");

   //do insert actions
}

Error while trying to run project: Unable to start debugging on the web server

Open your Internet Information Services manager and from the [your machine]/Web Sites/ Default Web Site node select the virtual folder which stores your project. Right Click and select Properties to select the ASP.NET tab page. Make sure the correct version ASP.NET Version is selected.

I have VS.NET 2003 and 2005 beta 1 running side-by-side on my box and noticed ASP.NET Version 2.0 is targeted by default, even for VS.NET 2003 projects.

Change datagrid cell properties using ASP.NET C#

public void dgBoundItems(object sender, DataGridItemEventArgs e) 
{ 
    foreach(TableCell cell in e.Item.Cells) 
    { 
        ///e.Item.DataSetIndex = row 
        ///e.Item.Cells.GetCellIndex(cell) = column
        if (e.Item.Cells.GetCellIndex(cell) == 2 && e.Item.DataSetIndex > 0) 
        {
            ///If the salary is less than 5000 the color must be red. 
            if (Convert.ToInt32(cell.Text.Trim()) < 5000) 
            { 
                cell.ForeColor = Color.Red; 
            } 
        } 
    } 
}

Inline IF’ Statement

C#

string s; 
s = (true ? "True" : "False"); //s will be "True"

VB.NET

Dim s As String
s = If(True, "True", "False") 's will be "True"
'In the following you cannot rely on a particular function 
'not being called if the other argument is selected by Expression
'(notice IIf vs. If)
s = IIf(True, "True", "False") 's will be "True"

JavaScript

var s = (true) ? 'True' : 'False'; //s will be "True"

DateTime Format String

DateTime now = new DateTime(2006, 9, 07, 15, 06, 01, 08, DateTimeKind.Local);

now.ToString();      //"09/27/2006 15:06:01"

Year

now.ToString("%y");   //"6"

now.ToString("yy");   //"06"

now.ToString("yyy");  //"2006"

now.ToString("yyyy"); //"2006"

Month

now.ToString("%M");    //"9"

now.ToString("MM");    //"09"

now.ToString("MMM");   //"Sep"

now.ToString("MMMM");  //"September"

Day

now.ToString("%d");    //"7"

now.ToString("dd");    //"07"

now.ToString("ddd");   //"Thu"

now.ToString("dddd");  //"Thursday"

Hour

now.ToString("%h");    //"3"

now.ToString("hh");    //"03"

now.ToString("hhh");   //"03"

now.ToString("hhhh");  //"03"

now.ToString("%H");    //"15"

now.ToString("HH");    //"15"

now.ToString("HHH");   //"15"

now.ToString("HHHH");  //"15"

Minutes

now.ToString("%m");    //"3"

now.ToString("mm");    //"03"

now.ToString("mmm");   //"03"

now.ToString("mmmm");  //"03"

Seconds

now.ToString("%s");    //"1"

now.ToString("ss");    //"01"

now.ToString("sss");   //"01"

now.ToString("ssss");  //"01"

Milliseconds

now.ToString("%f");    //"0"

now.ToString("ff");    //"00"

now.ToString("fff");   //"008"

now.ToString("ffff");  //"0080"

now.ToString("%F");    //""

now.ToString("FF");    //""

now.ToString("FFF");   //"008"

now.ToString("FFFF");  //"008"

Kind

now.ToString("%K");    //"-07:00"

now.ToString("KK");    //"-07:00-07:00"

now.ToString("KKK");   //"-07:00-07:00-07:00"

now.ToString("KKKK");  //"-07:00-07:00-07:00-07:00"

(Source)

Problems with MagicAjax

I added what is below to my web.config file:

<httpModules>
<add name="MagicAjax" type="MagicAjax.MagicAjaxModule, MagicAjax"/>
<add name="ScriptModule" type="Microsoft.Web.Services.ScriptModule"/>
</httpModules>

And I got:

Could not load file or assembly ‘MagicAjax’ or one of its dependencies.
The system cannot find the file specified. (C:\Inetpub\wwwroot\TheTracker\web.config line 48)

The solution I found that worked was adding the MagicAjax assembly to your /bin folder. And that fixed the problem.

Try Catch in .NET

The Try word means “Try to execute this code”. The Catch word means “Catch any errors here”. The ex is a variable, and the type of variable it is an Exception object.

Example

Public Function Delete(ByVal fileName As String) As Boolean
	Try
		File.Delete(String.Format("{0}\{1}", ConfigurationManager.AppSettings("ImageDirectory"), fileName))
		Return True
	Catch ex As Exception
		Return False
	End Try
End Function