Stop/Prevent Browser from Caching the current Page

protected void StopPageCaching()
{
    HttpContext.Current.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
    HttpContext.Current.Response.Cache.SetValidUntilExpires(false);
    HttpContext.Current.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
    HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
    HttpContext.Current.Response.Cache.SetNoStore();
}
Advertisements

Unrecognized attribute ‘targetFramework’ error is encountered for Asp.Net 4.0 site while Deploying it to the IIS or GoDaddy

This error is usually thrown when you try to deploy a Asp.Net 4.0 to IIS whose App Pool is targeting the .Net 2.0. You can confirm that by checking the last line of your error page. This is where running the .net version is displayed.

To resolve it, just do the following

For IIS

Make sure .Net 4.0 is registered in IIS. To do that run the aspnet_regiis.exe tool from the command prompt having administrative rights.
The tool is located at C:\Windows\Microsoft.NET\Framework64\v4.0.30319.

Now in the command prompt run the command aspnet_regiis.exe -iru.

We’re using iru because it Installs this version of ASP.NET. If there are any existing applications that uses ASP.NET, it will not change IIS configuration to use this version. More Info

Next open IIS Manager. Go to your sites app pool. Select Basic Settings and change the framework from 2.0 to 4.0.

For GoDaddy

For GoDaddy, you have to select from the menu bar, More => IIS Management. There at the top you’ll see your sites current .net version with an option to modify.

Select it and change the .Net version from 2.0 to 4.0.

Now here’s an issue. Even if you see your version as 4.0/4.5 still, I recommend you to change the version first to 2.0 and then again back to 4.0. This issue has arisen in the past that the GoDaddy IIS is showing the .Net Framework as 4.0 but somehow internally, it’s still pointing to 2.0. You can confirm the version of the .net environment by looking at the last line of the error page [highlighted above] Just remember to manually recycle the app pool after each version change.

That’s it. The IIS is now targeting the framework .net 4.0.

Existing Tomcat server on port 80 gives error: BAD REQUEST after installing/starting IIS on another Port

This is an issue which has caused a lot of trouble to me. My understanding was that I can run Apache & IIS on the same server provided, they are occupying different ports. Though this statement is true, but you should make sure that Apache should be installed on any other port except 80 if you intended to run both Tomcat & IIS on the same server.

The problem is that though your .NET website will run on the port assigned to it, the HTTP service, which is used by the IIS to listen any incoming request, runs on port 80. Now this service blocks the Apache Tomcat server (if it’s also running on port 80)

As of now the only way that crosses my mind to run both the servers on the same machine is not to assign Tomcat server to port 80. Use any other port except 80.

*********************************************************************************
Following are the steps taken to set up a ASP.NET site on the IIS where TOMCAT has occupied the port 80. In case it hampers the existing TOMCAT server then look at the 2nd segment of this document.

1. If the HTTP SSL service is present & stopped then, start the HTTP SSL service (if it’s not running)
If you get the error 1068, Filed to Load the Dependency, then it means the HTTP service is down. To restart it type regedit in run. Then traverse to LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/HTTP. SET Start = 2; Refer: http://support.microsoft.com/kb/103000.
Then restart the server.
Now you will see HTTP SSL & World Wide Web Publishing services are on.

2. Start the World Wide Web Publishing service (if not on)

3. Inside IIS, select Web Service Extensions and allow ASP.NET v4.0 (Considering the sites target framework is 4.0 (for IIS 6.0 only. For IIS 7.0, select the ASP.NET version from the site App Pool.))

*********************************************************************************

If the localhost (TOMCAT) is throwing the BAD Request error for the TOMCAT server running on port 80, then do the following:

1. Stop the IIS site.

2. Stop the World Wide Web Publishing service

3. If you’re still getting the error then,
type regedit in run. Then traverse to LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/HTTP.
SET Start = 4; Refer: http://support.microsoft.com/kb/103000. Then restart the server.

*********************************************************************************

Set a String as Enum

There are many instances where, in an Enum we need a string literal. However you can only enter a single word (with no space) against an integer (+ve/-ve). That’s where we use the description attribute.
Look at the Enum declaration below:-


public enum States
{
	[System.ComponentModel.Description("New Delhi")]
	NewDelhi = 1,
	[Description("Uttar Pradesh")]
	UttarPradesh = 2
	[Description("West Bengal")]
	WestBengal = -1,
}

Normally this is how we bind the Enum to a DropDownList

DropDownList1.DataSource = Enum.GetNames(typeof(States));
DropDownList1.DataBind();

This is what we’ll get the output for our Enum States

To get the description value of the Enum, we’ll use the following method


public static string GetEnumDescription(Enum value) 
{ 
	System.Reflection.FieldInfo fi = value.GetType().GetField(value.ToString()); 
	if (fi != null) 
	{ 
		System.ComponentModel.DescriptionAttribute[] attributes = 	(System.ComponentModel.DescriptionAttribute[])fi.GetCustomAttributes(typeof(System.ComponentModel.DescriptionAttribute), false); 
		if (attributes != null && attributes.Length > 0) 
			return attributes[0].Description; else return value.ToString();
	}
	return value.ToString(); 
}

This is how we’ll do to bind the Enum description to a DropDownList


foreach (States value in Enum.GetValues(typeof(States)))
{
	DropDownList1.Items.Add(new ListItem(GetEnumDescription(value), value.ToString()));
}
DropDownList1.DataBind();

Final Output:

 

 

Happy Coding …

Search a DataRow from DataTable1 into DataTable2

Tip

The situation is something like this. A GridView is getting populated at the PageLoad event. Now one of the Column of the Grid contains the CheckBox to represent the True/False state for that Row. There are 2 tables in the SQL Database. Now, if a DataRow is present in both the tables then, that Row’s checkbox should be marked in the GridView. The GridView Columns which contains the checkbox is being added at the runtime i.e, that column is not present in the SQL DataTable.
The question is how can I search a single DataRow of a DataTable against all the DataRows of another DataTable. Look at the code below


DataTable dt = null;
DataTable dtParent = null;

dt = GetList(); // Get the selected List
dtParent = GetNameList(); //Get the Entire List

// Mark a Column(s) as Primary, which will be searched for the given value.
dtParent.PrimaryKey = new DataColumn[] { dtParent.Columns["ID"] };
foreach (DataRow dr in dt.Rows)
{
	//If that Row is Present in dtParent then, mark the row's checkbox as checked.
	dtParent.Rows.Find(dr["ID"])["chkCheckBox"] = true; 
}
gvStudentDetails.DataSource = dtParent;
gvStudentDetails.DataBind();

Happy Coding …

Change DropDownList SelectedIndex on Gridview SelectedIndexChanging Event

Tip

Well, here I would like to set the SelectedIndex of a DropDownList based on a Column’s Text Value from the GridView’s SelectedIndexChanging event. I guess, this can be achieved by many ways however, what I am trying to show here is to do the same in just 1 line.


ddlDropDown.SelectedIndex = ddlDropDown.Items.
								IndexOf(ddlDropDown.Items.
									FindByText(((Label)gvGridView.Rows[e.NewSelectedIndex].
										FindControl("lblLabel")).Text));

Happy Coding …

DataTable’s ImportRow & Clone methods [C#]

Discussion

Here, I will demonstrate the functionality of DataTable’s ImportRow & Clone methods and the difference between the Rows.Add & ImportRow.

Clone method will copy the structure of the existing table, only Columns and Schema, not the data. So in the following

DataTable dt = GetSomeVal();
DataTable dt1 = dt.Clone();
dt1 will have exactly the same Column type as of dt. However, dt1.Rows.Count will return 0.As for the other issue look at the following code::


DataTable dt1 = new DataTable();
DataRow dr1 = dt1.NewRow();
DataTable dt2 = new DataTable();

// will throw an error as dr1 already belongs to another datatable
dt2.Rows.Add(dr1); 

// will execute adding a new row to dt2 Table
dt2.ImportRow(dr1);

dt1.Rows.Add(dr1);

The above code is pretty much self-explanatory. If you have to add a DataRow from a different DataTable use ImportRow method otherwise use Rows.Add.

Happy Coding …

Make Single RadioButton Selection in Gridview using Javascript

Problem

Here, I will be adding two columns in a GridView. One will contain a RadioButton, and the other one will have some text. The user however, can select only one RadioButton at a time. Multi-select needs to be disabled. The problem is that each row contains a new RadioButton so the user was able to select all the RadioButtons.

Solution

Solution is only one thing, javascript. On click on one of the radio button, all other radiobuttons should be disabled. The javascript code is as follows:-


<script language="javascript" type="text/javascript">
	function SelectSingleRadiobutton(rdbtnid) {
		var rdBtn = document.getElementById(rdbtnid);
		var rdBtnList = document.getElementsByTagName("input");
		for (i = 0; i < rdBtnList.length; i++) {
			if (rdBtnList[i].type == "radio" && rdBtnList[i].id != rdBtn.id)
			{
				rdBtnList[i].checked = false;
			}
		}
	}
</script>

The definition of Grid is defined below. Note: the javascript call for the radio Button:-


<asp:GridView ID="gvdata" runat="server" CssClass="Gridview" AutoGenerateColumns="false" >
	<Columns>
		<asp:TemplateField>
			<ItemTemplate>
				<asp:RadioButton id="rdbUser" runat="server" OnClick="javascript:SelectSingleRadiobutton(this.id)" />
			</ItemTemplate>
		</asp:TemplateField>
		<asp:BoundField DataField="SomeData" HeaderText="Some Text"/>
	</Columns>
</asp:GridView>

 

Thanks…

 

Place a GridView in the Center of a DIV

To place a GridView in the center of a div one thing that we do is to use the property text-align:center. However as per the current css, it won’t work. What we need to do is to use the code margin-left: auto; margin-right: auto;. Either put that in the css Class of the GridView or you can directly put it inside the Grid like the following

<asp:GridView ID="grdExamSchedules" runat="server" ForeColor="#333333" 
              style="margin-left:auto; margin-right:auto" >

Thanks …