Thursday, April 2, 2009

Restrict Duplicate Record Insertion On Page Refresh

The two ways which I believe is pretty simple to prevent duplicate record insertion on page refresh.

1. Using ViewState and Session


public partial class _Default : System.Web.UI.Page
{
private bool _refreshState;
private bool _isRefresh;
public bool IsRefresh
{
get
{
return _isRefresh;
}
}
protected override void LoadViewState(object savedState)
{
object[] allStates = (object[])savedState;
base.LoadViewState(allStates[0]);
_refreshState = (bool)allStates[1];
_isRefresh = _refreshState == (bool)Session["__ISREFRESH"];
}

protected override object SaveViewState()
{
Session["__ISREFRESH"] = _refreshState;
object[] allStates = new object[2];
allStates[0] = base.SaveViewState();
allStates[1] = !_refreshState;
return allStates;
}

protected void Button2_Click(object sender, EventArgs e)
{
if(!IsRefresh)
Response.Write("Thanx for visiting");
}
}




2. Use Response.Redirect


public partial class _Default : System.Web.UI.Page
{

protected void Page_Load(object sender, EventArgs e)
{
Boolean blnRefreshed = true;
}
protected void Button1_Click(object sender, EventArgs e)
{
//code to save your data here
Response.Redirect("default.aspx");
}

}


The second one has a performance overhead. Let’s check how.
Put break point on Page_Load and click on button. The break points on Page_Load will be hit twice intead of one due to Response.Redirect, the second hit is overhead.

The second hit can be avoided by using the first method.

Happy Coding :)

ArrayList to DataTable

From last couple of days, i found few programmer in asp.net forum wanted to convert ArrayList to DataTable. So i thought to put it here for reference.

Below is the simple example of converting double dimensional array list into data table.

Dim arrList As ArrayList = New ArrayList()
Dim dt As New DataTable()
Dim intCnt As Integer = 0
dt.Columns.Add(New DataColumn("Name", System.Type.GetType("System.String")))
dt.Columns.Add(New DataColumn("Age", System.Type.GetType("System.String")))
arrList.Add(New String() {"Richards", "25"})
arrList.Add(New String() {"Marie", "30"})
arrList.Add(New String() {"Sandy", "35"})
arrList.Add(New String() {"Michael", "40"})
For Each item As Object In arrList
Dim dr As DataRow
Dim arrItem As String() = DirectCast(item, String())
dr = dt.NewRow()
dr("Name") = arrItem(0)
dr("Age") = arrItem(1)
dt.Rows.Add(dr)
Next

Happy Coding :)

Saturday, March 28, 2009

SQLDataReader and DataSet

In terms of memory, We always say that we should avoid use of DataSet unless it is very much required. If the requirement can be accomplished with SQLDataReader never use DataSet.

The common answer which we know is DataSet takes more memory than SQLDataReader.

I thought why not find out how much more memory DataSet consumes than SQLDataReader. I ran CLR profiler and I found significant difference between two.

DataSet take 60% more memory than SQLDataReader.

I just created object of SQLDataReader( SqlDataReader dr = new SqlDataReader();), the memory consumption is 22,218 bytes.

And when I created objet of DataSet (DataSet ds = new DataSet();) , the memory consumption is 35,920 bytes.


Use DataSet carefully :)

Happy Coding :)


SQL 2008 Date

SQL 2000 and 2005 don't have option to enter only date in DateTime column. Even if we don't provide time while inserting date in DateTime column, it enters the date along with time like the below screen shot.




SQL 2008 has the feature to store only date, as it has the datatype of Date only rather tahn DateTime.

DECLARE @datetest DATE
SELECT @datetest = '3/28/2009'
SELECT @datetest as [Date]

Output:

Date
----------
2009-03-28

Monday, March 23, 2009

SQL Database information

SQL Server with plenty of databases. Get the space occupied, the location of data and log etc of each database.

Change the execute mode to Text






Execute the below query you will get all the databases name in the result pane

select 'sp_helpdb ' + name + '' + CHAR(10) + 'go' from sysdatabases



The output will be





Copy the above output and execute you will get the details about all the database at one shot.

Friday, March 20, 2009

Latest AjaxControlToolKit

Download Latest AjaxControlToolkit from the below location

http://www.codeplex.com/pks/Release/ProjectReleases.aspx?ReleaseId=16812"

Happy Coding :)

ComboBox set font style


Fill Combo with the available fonts in the system

public void fillFontCombo()
{
FontFamily[] families = FontFamily.Families;
//Loop Through System Fonts
foreach (FontFamily family in families)
{
//Set Current Font's Style To bold
FontStyle style = FontStyle.Bold;
//These Are Only Available In Italic, Not In "Regular",
//So Test For Them, Else, Exception!!
if (family.Name == "Monotype Corsiva" || family.Name == "Brush Script MT"
|| family.Name == "Harlow Solid Italic" || family.Name == "Palace Script MT" ||
family.Name == "Vivaldi")
{
//Set Style To Italic, To Overt "Regular" & Exception
style = style | FontStyle.Italic;
}
Font FCFont = new Font(family.Name, 10, style, GraphicsUnit.Point);
//Display The Font Combo Items
comboBox1.Items.Add(FCFont.Name);
}
comboBox1.SelectedIndex = 1;
}



Add drawitem eventhandler for the combobox to paint the combobox

private void combobox1_drawitem(object sender, DrawItemEventArgs e)
{
//If the index is invalid, do nothing and exit.
if (e.Index == -1 || e.Index >= comboBox1.Items.Count)
return;
//Draw the background of the item.
e.DrawBackground();
// Draw the focus rectangle
if ((e.State & DrawItemState.Focus) != 0)
e.DrawFocusRectangle();
Brush b = null;
try
{
// Create Background Brush.
b = new SolidBrush(e.ForeColor);
// Draw the item.
FontStyle style = FontStyle.Bold;
string strfont = comboBox1.Items[e.Index].ToString();
if (strfont == "Monotype Corsiva" || strfont == "Brush Script MT"
|| strfont == "Harlow Solid Italic" || strfont == "Palace Script MT"
|| strfont == "Vivaldi")
{
//Set Style To Italic, To Overt "Regular" & Exception
style = style | FontStyle.Italic | FontStyle.Regular;
}

Font nfont = new Font(strfont, 10, style);
e.Graphics.DrawString(
strfont,
nfont,
b,
e.Bounds
);

} // End try
finally
{
// Dispose the brush
if (b != null)
b.Dispose();
b = null;
} // End finally
}


Happy Coding :)

Site Meter