Sunday, September 25, 2011

Difference between Web.Config and Machine.Config File

Two types of configuration files supported by ASP.Net.
Configuration files are used to control and manage the behavior of a web application.

i) Machine.config
ii)Web.config

Difference between Machine.Config and Web.Config
Machine.Config:
i) This is automatically installed when you install Visual Studio. Net.
ii) This is also called machine level configuration file.
iii)Only one machine.config file exists on a server.
iv) This file is at the highest level in the configuration hierarchy.

Web.Config:
i) This is automatically created when you create an ASP.Net web application project.
ii) This is also called application level configuration file.
iii)This file inherits setting from the machine.config

Friday, September 9, 2011

Restricting access to page or directory in ASP.NET










You have to use Forms Authentication.
http://support.microsoft.com/kb/301240

How to subtract 1 from a orginal count in an ASP.NET gridview

I have a gridview that contains a count (whic is Quantity) were i have a button that adds a row under the orginal row and i need the sub row's count (Quantity) to subtract one from the orgianl row Quantity.
EX: Before button click
Orgianl row = 3
After click
Orginal row = 2
Subrow = 1
Code:
ASP.NET




// FUNCTION : Adds a new subrow
protected void gvParent_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName.Equals("btn_AddRow", StringComparison.OrdinalIgnoreCase))
{
// Get the row that was clicked (index 0. Meaning that 0 is 1, 1 is 2 and so on)
// Objects can be null, Int32s cannot not.
// Int16 = 2 bytes long (short)
// Int32 = 4 bytes long (int)
// Int64 = 8 bytes long (long)
int i = Convert.ToInt32(e.CommandArgument);

// create a DataTable based off the view state
DataTable dataTable = (DataTable)ViewState["gvParent"];

for (int part = 0; part < dataTable.Rows.Count; part++) { int oldQuantity = Convert.ToInt32(dataTable.Rows[i]["Quantity"]); string partNumber = dataTable.Rows[i]["ProductDescription"].ToString(); string description = dataTable.Rows[i]["Description"].ToString(); string dateOrdered = dataTable.Rows[i]["GTRI_DateSubmittedtoPurchasing"].ToString(); string estShipDate = dataTable.Rows[i]["Gtri_EstShipDate"].ToString(); string actualShipDate = dataTable.Rows[i]["Gtri_ActualShipDate"].ToString(); string trackingNumb = dataTable.Rows[i]["GTRI_TrackingNumbers"].ToString(); string serialNumb = dataTable.Rows[i]["Gtri_SerialNumber"].ToString(); int oldQuantitySubtract = Convert.ToInt32(dataTable.Rows[part]["Quantity"]); string curentPartNumbers = dataTable.Rows[part]["ProductDescription"].ToString(); string currentDescription = dataTable.Rows[part]["Description"].ToString(); string currentDateOrdered = dataTable.Rows[part]["GTRI_DateSubmittedtoPurchasing"].ToString(); string currentEstShipDate = dataTable.Rows[part]["Gtri_EstShipDate"].ToString(); string currentActualShipDate = dataTable.Rows[part]["Gtri_ActualShipDate"].ToString(); string currentTrackingNumb = dataTable.Rows[part]["GTRI_TrackingNumbers"].ToString(); string currentSerialNumb = dataTable.Rows[part]["Gtri_SerialNumber"].ToString(); if (partNumber.Equals(curentPartNumbers, StringComparison.OrdinalIgnoreCase) && oldQuantitySubtract > 1)
{
dataTable.Rows[part]["Quantity"] = oldQuantitySubtract - 1;

// Instert a new row at a specific index
DataRow dtAdd = dataTable.NewRow();

for (int k = 0; k < dataTable.Columns.Count; k++)

dtAdd[k] = dataTable.Rows[part][k];
dataTable.Rows.InsertAt(dtAdd, i + 1);

break;

//dataTable.Rows.Add(dtAdd);
}
}
// Rebind the data
gvParent.DataSource = dataTable;
gvParent.DataBind();
}
}

What are "Expression Trees" in C# ?

URL explains Expression Trees with examples in C# and VB.

http://msdn.microsoft.com/en-us/library/bb397951.aspx

Integrating ASP.NET MVC 3 into existing upgraded ASP.NET 4 Web Forms applications

http://www.hanselman.com/blog/IntegratingASPNETMVC3IntoExistingUpgradedASPNET4WebFormsApplications.aspx

As per above article I follow the steps to integrate WebApp with MVC application. I am successfully integrated MVC project into WebApp(C#) and also VB.NET MVC and VB.NET WebApp also I am able to successfully integrated.

The problem is If I choose WebApp as VB.NET project, and integrated with C# MVC project. In this case the request is not routing to corresponding MVC files.

What could be the reason not routing to MVC. Do we need to plug some extra dlls?

How to access values of dynamically created TextBoxes

If one adds controls dynamically to a page and wants to get their information after PostBack, one needs to recreate these elements after the PostBack. Let's consider the following idea: First you create some controls:

for(int i=0;i<10;i++) {
TextBox objBox = new TextBox();
objBox.ID = "objBox" + i.ToString();
this.Page.Controls.Add(objBox);
}

After PostBack, you want to retrieve the text entered in the third TextBox. If you try this:

String strText = objBox2.Text;

you'll receive an exception. Why? Because the boxes have not been created again and the local variable objBox2 simply not exists.

How to retrieve the Box?

You'll need to recreate the box by using the code above. Then, you may try to get its value by using the following code:

TextBox objBox2;
objBox2 = this.Page.FindControl("objBox2") as TextBox;
if(objBox2 != null)
Response.Write(objBox2.Text);