Tuesday, October 27, 2009

Transcation in Sql Query in SQL server

Many times it occurs that we are writing a sql query and executing it to check what is it affect to table.
for example Delete statement or update statement
We write a update / Delete statemet in Query bulider of Sql server enviroment and execute it
Suddenly we find it has Affected multiple records which should not be occured.

And then we suffer the data lost.

Instead while doing R&D on delete/update query in SQL query builder in sql server, start a new
transaction in sql server .

Example:
declare @EmpID int
set @EmpID = '47'
Begin Tran
DELETE FROM [Employee]
WHERE Employee_ID = @EmpID
--COMMIT TRAN
--ROLLBACK




and if it affects multiple records which we don't aspect to happen
just execute
ROLLBACK statement

Otherwise it we get expected result of update/delete Just Execute COMMIT TRAN

Comfirm code of Javascript To Confirm Deletion of record

<html>
<head>
<script type="text/javascript">
<!--
function confDelete()
{
var answer = confirm("Do you really want to delete the Record ?");

if (answer){
return true;
}
else{
return false;
}
}

//-->
</script>
</head>
<body>
<form>
<input type="button" onclick="confDelete()" value="Delete record">
</form>
</body>
</html>

Saturday, October 10, 2009

How to access a control in Header and footer in DataList

This is Technique used to access the header and footer controls of Datalist

//in design---------------------

<asp:DataList ID="DataList1"
runat="server"
Width="100%"
OnItemCommand="DataList1_ItemCommand1"
HorizontalAlign="Left">
<HeaderTemplate>
<table class="DatalistHeader">
<tr>
<th align="center" style="width: 10%;">Sr.no</th>
<th align="center" style="width: 30%;">Emp Name</th>
<th align="center" style="width: 20%;">Designation</th>
<th align="center" style="width: 8%;">Department</th>
<th align="center" style="width: 10%;">Post</th>
<th align="center" style="width: 12%;">Salary <br /> In Rs..</th>
<th align="center" style="width: 10%;">
<asp:LinkButton ID="lnkCheck_head" runat="server">Check</asp:LinkButton>
</th>
</tr>
</table>
</HeaderTemplate>
<AlternatingItemStyle BackColor="#F1F8FE"></AlternatingItemStyle>
<ItemTemplate>
....
....
....
....
</ItemTemplate> <FooterTemplate>
<table class="DatalistHeader">
<tr>
<th align="center" style="width: 10%;">Sr.no</th>
<th align="center" style="width: 30%;">Emp Name</th>
<th align="center" style="width: 20%;">Designation</th>
<th align="center" style="width: 8%;">Department</th>
<th align="center" style="width: 10%;">Post</th>
<th align="center" style="width: 12%;">Salary <br /> In Rs..</th>
<th align="center" style="width: 10%;">
<asp:LinkButton ID="lnkCheck_foot" runat="server">Check</asp:LinkButton>
</th>
</tr>
</table>
</FooterTemplate>
</asp:DataList>


//----------------------------------

In Code behind..........................


//this is used to get the contents of HEADER like Compare link button
LinkButton Comp_header = (LinkButton) DataList1.Controls[0].Controls[0].FindControl("lnkCheck_head");

int footerIndex = DataList1.Controls[0].Controls.Count - 1;

//this is used to get the contents of FOOTER like Compare link button

LinkButton Comp_footer = (LinkButton) DataList1.Controls[0].Controls[footerIndex].FindControl("lnkCheck_foot");




if (Comp_header != null)
{
//do something

}

if (Comp_footer != null)
{
//do something
}

Thursday, October 8, 2009

Most Important and everytime requiring regular expressions for validators

lots of regular validator:
ValidationExpression="^\d{6,6}$" -- limit only 6 numeric value
ValidationExpression="^\d{10,10}$" -- limit only 10 numeric value
ValidationExpression="^((\+)?(\d{2}))$" -- accept +91 or +01

ValidationExpression="^[a-zA-Z\s]{2,25}" -- accept alphabets small and big caps min 2 to 25 character and space
ValidationExpression="^[a-zA-Z0-9\s]{2,25}" -- accept alphanumeric small and big caps min 2 to 25 character

email
ValidationExpression="\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$" -- email validation

for upload
ValidationExpression="^.+(.jpg|.gif|.jpeg|.png|.JPG|.PNG|GIF)$" -- accept only xyz.jpg or any file name as .xxx
for accepting first name
ValidationExpression="[A-Z a-z\.\ ]{2,24}" -- accept alhanumeric with . and space

Login ID
ValidationExpression="[1-9A-Za-z\.\\_\-\@]{2,30}" -- accept alhanumeric with symbol like . _ - @
with min 2 and max 30 character

Password:
ValidationExpression="[a-zA-Z0-9\,\-\=\\+\/\@\#\$\*\&\%\^]{6,30}" -- accept alhanumeric with symbol like . _ - @
and all symbols given in bracket

Sunday, October 4, 2009

Sign in from any content page and navigate to same or differnt page as required.

- There are many actions on web page that requires login of a user.
like Comments, alerts, downloads etc.
- Thus make a login-popup by modalpopupextender of ajax tool kit.
- Put it in master page and Attached (TargetControlID) it to a temporary
link button which is kept in a hidden div
as


IN MASTER page:

<div visible="false" style="display:none" >
<cc1:ModalPopupExtender ID="modalpopSignin" OnCancelScript="javascript: return clearSigninPopup();" BackgroundCssClass="modalBackground"
runat="server" TargetControlID="lnk_click" CancelControlID="lnk_BtnClose" PopupControlID="pnl_LoginCommon">
</cc1:ModalPopupExtender>
<asp:LinkButton ID="lnk_click" runat="server">a</asp:LinkButton>
</div>


1. OnCancelScript="javascript: return clearSigninPopup();"
this javascript function: clearSigninPopup is used to clear all textbox and error message(validators, lables)
so that when popup panel is displayed it is totally clear


2. BackgroundCssClass="modalBackground"
to make background as gray with some opacity to show pop ha came with background blur.
make css and put class as:

.modalBackground {
filter: alpha(opacity=70); background-color: gray; opacity: 0.7;
}
add refernce of that css in that webpage.

3. TargetControlID="lnk_click" : attach it to a dummy linkbutton as it is mandatory for a modalpoup extender to attach to one control to run.

4. CancelControlID="lnk_BtnClose" : in panel ther is a cross link buuton to close that popup. give that linkbtn id here

5. PopupControlID="pnl_LoginCommon" : This is most important step.
it is the pael which is to be shown on pop up.
Remember please give a backgroung color to popup .. it is must to show poup window properly
ex:

<asp:Panel ID="pnl_LoginCommon"
runat="server" Height="271px" Width="570px" DefaultButton="lnk_LogIn">
<table style="border: #034a61 5px solid; background-color: white; width: 100%;" cellspacing="0" >
<tr>
......
<tr>
</table>
</asp:Panel>

In Content Page:

1. make 2 hidden feild:- To store user is a login or not , and to what page it has to redirect

<input id="hidLoginId" runat="server" type="hidden" />
<input id="hidRedirect" runat="server" type="hidden" />

adding runat="server" is mandatory

In its code behind's page_load methord write this:

protected void Page_Load(object sender, EventArgs e)
{
if (Session["LoginId"] != null)
{
//user is logined
hidLoginId.Value = Session["LoginId"].ToString();
}
else
{ //user is not logined
hidLoginId.Value = "Unknown";
}
}


2.Create a link button to write comment , download or any thing.
example:
<asp:LinkButton ID="lnkPersAlert"
OnClientClick="javascript:return signInPop()" onClick="lnkPersAlert_on_Click"
runat="server" >
<img src="DownloadMP3.gif"
style="border:none ;width:190px; margin-left:5px;"
title="Download this mp3" />
</asp:LinkButton>

OnClientClick link button calls a given javascript and onClick it postback to server
for given event handler"lnkPersAlert_on_Click"


3.write a javascript : to check if user is login

a. if user login then return true and execut server side function :-
b. if user is not logined then show modal popup of sign (in master page).


function signInPop()
{
//hidden feild stores is user a loginin or not
var hidLogin= document.getElementById('<%=hidLoginId.ClientID%>');

//hidden feild stores page name where to navigate ex: Comments_main.aspx
var hidRedirect = document.getElementById('<%=hidRedirect.ClientID%>');

var IsLogined = hidLogin.value;
// alert('hidLogin ' + hidLogin +'IsLogined '+ IsLogined);

if( IsLogined == "Unknown")
{
//user is not login show popup

$find('ctl00_modalpopSignin').show();
//this 'ctl00_modalpopSignin' is taken by rendering page in IE browser
//and foundthe client id of the modalpopupextender

hidRedirect.value ="Comments_main.aspx";

return false; // do not past back and execute sever side link btn code.
}
else
{ return true; }
}

4. when user either create a login or sign in by that popupmodal extender.

after login or new registartion .. in code behind function of master page.
write code as:

ContentPlaceHolder cph1 = main;
HtmlInputHidden hidContent = (HtmlInputHidden)cph1.FindControl("hidRedirect");

if (hidContent == null)
{
// if no information where to navigate
Response.Redirect("LoginPage2.aspx");

}
else
{
string strRedirect = hidContent.Value;
Response.Redirect(strRedirect);

}

this is to get a hidden feild from content page , get its value and redirect it there..

that's it....
any problem email me: suyash123@gmail.com

Saturday, October 3, 2009

If u Get this Error :

{System.InvalidCastException: Unable to cast object of type 'System.Web.UI.HtmlControls.HtmlInputHidden' to type 'System.Web.UI.WebControls.HiddenField'}

the Cause is :

HiddenField hid = (HiddenField)cph_Main.FindControl("hidControl");

in asp.net there are bascially 2 types of controls
1. HtmlControls : in namespace :- System.Web.UI.HtmlControls
2. Webcontrols : in namespace: - System.Web.UI.WebControls

now if 'hidControl' is a html input field then it can't be convertes to
asp.net webcontrol's hidden feild.

Solution is very simple:

HtmlInputHidden hidContent = (HtmlInputHidden)cph1.FindControl("hidRedirect");