Control c = GetPostBackControl(Page);
        if ((c != null))
        {
            if (c.ID.ToString() == "grdTemplate" || c.ID.ToString() == "btnAddTemplate")
            {
                       // Do  code whatever you want to action while clicked this button.
            }            
        }  
////////////////////////Method to get the Postback button Id///////////////
private Control GetPostBackControl(System.Web.UI.Page page)
    {
        Control control = null;
        string ctrlname = page.Request.Params.Get("__EVENTTARGET");
        if ((ctrlname != null) & ctrlname != string.Empty)
        {
            control = page.FindControl(ctrlname);
        }
        else
        {
            foreach (string ctl in page.Request.Form)
            {
                Control c = page.FindControl(ctl);
                if (c is System.Web.UI.WebControls.Button)
                {
                    control = c;
                    break; // TODO: might not be correct. Was : Exit For
                }
            }
        }
        return control;
    }
For More Information  Click Me 
Wednesday, September 22, 2010
Tuesday, September 14, 2010
Show client side alert message from server side code even when update panel is used
Developers might want to display client side message with information from server side to their users when they have completed some execution at server side. People may try in different way for this purpose.
For example:
Response.Write() method with JavaScript code inside the method:
string mes = "Hello Dhaka";
Response.Write("< script language=”javascript” type=”text/javascript”>alert(’” + mes + “‘);</script>");
or ClientScript.RegisterStartupScript() method:
string message = "";
if (!ClientScript.IsStartupScriptRegistered("mes"))
{
ClientScript.RegisterStartupScript(this.GetType(), "mes", message);
}
But these code doesn't work when you use update panel in your page. So better solution is to use ScriptManager.RegisterStartupScript() method. This works whether update panel is used or not. So let's see the code snippet below:
string message = string.IsNullOrEmpty(TextBox1.Text) ? "Please give a text to textbox." : "You have given: " + TextBox1.Text.Trim();
string script = "";
ScriptManager.RegisterStartupScript(Page, this.GetType(), "AlertMessage", script, false);
OR
With this code snippet you can display a modal message that their data was saved or updated successfully or not.
For example:
Response.Write() method with JavaScript code inside the method:
string mes = "Hello Dhaka";
Response.Write("< script language=”javascript” type=”text/javascript”>alert(’” + mes + “‘);</script>");
or ClientScript.RegisterStartupScript() method:
string message = "";
if (!ClientScript.IsStartupScriptRegistered("mes"))
{
ClientScript.RegisterStartupScript(this.GetType(), "mes", message);
}
But these code doesn't work when you use update panel in your page. So better solution is to use ScriptManager.RegisterStartupScript() method. This works whether update panel is used or not. So let's see the code snippet below:
string message = string.IsNullOrEmpty(TextBox1.Text) ? "Please give a text to textbox." : "You have given: " + TextBox1.Text.Trim();
string script = "";
ScriptManager.RegisterStartupScript(Page, this.GetType(), "AlertMessage", script, false);
OR
System.Text.StringBuilder sb = new System.Text.StringBuilder();sb.Append(@"<script language='javascript'>");sb.Append(@"var lbl = document.getElementById('lblDisplayDate');");sb.Append(@"lbl.style.color='red';");sb.Append(@"</script>");if (!ClientScript.IsStartupScriptRegistered("JSScript")){ClientScript.RegisterStartupScript(this.GetType(), "JSScript", sb.ToString());}
With this code snippet you can display a modal message that their data was saved or updated successfully or not.
Monday, July 26, 2010
Find all The User Defined Procedures in Sql
There is two ways to get the result:-
1. Using Comination of System define Procedure table and Schema table
Select * From sys.procedures As P
Inner Join sys.schemas As S On S.schema_id = P.schema_id
Where P.type = 'P' And is_ms_shipped = 0 And P.name Not Like 'Sp[_]%diagram%'
2. Using System define Object table
Select * From sys.objects
Where type = 'P' And is_ms_shipped=0 And name Not Like 'Sp_%diagram%'
Note:- is_ms_shipped Equals Zreo Means Show all the User defined events
1. Using Comination of System define Procedure table and Schema table
Select * From sys.procedures As P
Inner Join sys.schemas As S On S.schema_id = P.schema_id
Where P.type = 'P' And is_ms_shipped = 0 And P.name Not Like 'Sp[_]%diagram%'
2. Using System define Object table
Select * From sys.objects
Where type = 'P' And is_ms_shipped=0 And name Not Like 'Sp_%diagram%'
Note:- is_ms_shipped Equals Zreo Means Show all the User defined events
Tuesday, July 20, 2010
Validate number in textbox using Javascript
function fnSubmit(){
var str = "ab126";
var ValidStr = "0123456789.";
var flag;
for(var i=0;i                 if(ValidStr.indexOf(str.charAt(i)) > 0)
flag = true;
else
flag = false;
}
}
var str = "ab126";
var ValidStr = "0123456789.";
var flag;
for(var i=0;i
flag = true;
else
flag = false;
}
}
Tuesday, June 22, 2010
Copy Entire Folder from one location to another in .Net
Copying the entire file under the folder there is two ways
1. Using component
2. Using Programatically
=======================================================================================
1) Add this line of code and pass your source folder path and destination folder path, then this component will copying all the files from source to desc.
Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(fromDirectory, toDirectory);
2. Via Programatically
first you need to add namesapce System.IO on the top of the page
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo SourceLoc = new DirectoryInfo(@"D:\Projects\SitePages\common");
DirectoryInfo TargetLoc = new DirectoryInfo(@"D:\Projects\Production\common");
CopyAllFiles(SourceLoc, TargetLoc);
}
///
/// Copy All files from the source directory into the target directory
///
///
///
public void CopyAllFiles(DirectoryInfo Source, DirectoryInfo target)
{
//Validate the existance of the target dir, if fail then create the dir
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
//Copy individual file into the new directory
foreach (FileInfo file in Source.GetFiles())
{
file.CopyTo(Path.Combine(target.ToString(), file.Name), true);
}
//Recursion loop to copy each sub directory
foreach (DirectoryInfo diSource in Source.GetDirectories())
{
DirectoryInfo nexttarget = target.CreateSubdirectory(diSource.Name);
CopyAllFiles(diSource, nexttarget);
}
}
1. Using component
2. Using Programatically
=======================================================================================
1) Add this line of code and pass your source folder path and destination folder path, then this component will copying all the files from source to desc.
Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(fromDirectory, toDirectory);
2. Via Programatically
first you need to add namesapce System.IO on the top of the page
protected void Page_Load(object sender, EventArgs e)
{
DirectoryInfo SourceLoc = new DirectoryInfo(@"D:\Projects\SitePages\common");
DirectoryInfo TargetLoc = new DirectoryInfo(@"D:\Projects\Production\common");
CopyAllFiles(SourceLoc, TargetLoc);
}
///
/// Copy All files from the source directory into the target directory
///
///
///
public void CopyAllFiles(DirectoryInfo Source, DirectoryInfo target)
{
//Validate the existance of the target dir, if fail then create the dir
if (Directory.Exists(target.FullName) == false)
{
Directory.CreateDirectory(target.FullName);
}
//Copy individual file into the new directory
foreach (FileInfo file in Source.GetFiles())
{
file.CopyTo(Path.Combine(target.ToString(), file.Name), true);
}
//Recursion loop to copy each sub directory
foreach (DirectoryInfo diSource in Source.GetDirectories())
{
DirectoryInfo nexttarget = target.CreateSubdirectory(diSource.Name);
CopyAllFiles(diSource, nexttarget);
}
}
Sunday, June 20, 2010
Create Constraints/Delete Foreign Key linked record
Suppose you want to delete the one record that is associate/linked to some other table with foreign key reference then we need to set/alter cascade command on the table then it will delete/update the record which is associated to the same table/column
For Reference:-
1. Create One Table Called tblFirst With Primary Key
2. Create another table called tblSecond with foreign key.make sure your column key column doesn't contain any identity
3. While creating the foreign key you need to set the cascade. This cascade word delete/update the all records that linked to the same row.
Create table tblFirst(Id Int Identity(1,1) Primary Key, Name varchar(50))
Create table tblSecond(Id Int Primary Key, Phone varchar(50), Foreign Key (ID) References tblFirst(Id) On Delete Cascade On Update Cascade)
INsert into tblFirst(Name) values('Murli')
INsert into tblFirst(Name) values('Anup')
INsert into tblSecond(Id,Phone) values(1,'9323399705')
INsert into tblSecond(Id,Phone) values(2,'9000000000')
Delete From tblFirst Where Id = 1
For Reference:-
1. Create One Table Called tblFirst With Primary Key
2. Create another table called tblSecond with foreign key.make sure your column key column doesn't contain any identity
3. While creating the foreign key you need to set the cascade. This cascade word delete/update the all records that linked to the same row.
Create table tblFirst(Id Int Identity(1,1) Primary Key, Name varchar(50))
Create table tblSecond(Id Int Primary Key, Phone varchar(50), Foreign Key (ID) References tblFirst(Id) On Delete Cascade On Update Cascade)
INsert into tblFirst(Name) values('Murli')
INsert into tblFirst(Name) values('Anup')
INsert into tblSecond(Id,Phone) values(1,'9323399705')
INsert into tblSecond(Id,Phone) values(2,'9000000000')
Delete From tblFirst Where Id = 1
Monday, June 14, 2010
Create Linked Server From Query in Sql Server
EXEC sp_addlinkedserver 
@server='MY_TEMP_linkServer',
@srvproduct='',
@provider='SQLNCLI',
@datasrc='MYDATASOURCE\SQLEXPRESS2005'
EXEC sp_addlinkedsrvlogin @rmtsrvname = 'MY_TEMP_linkServer'
, @useself = 'false'
, @locallogin = 'sa'
, @rmtuser = 'sa'
, @rmtpassword = 'admin123'
Create Linked Server From Query:-
For Link the Two or More Server You need to follow this two steps
A) First Need to create/assign/add server name(Which you can use in program) in linked server list
B) Then set your user id and password to same linked list.
Note :- i) Server Name could be anything which you want to associate in Query.
ii) Need to assign Provider
iii) User names are must be permission to view the table/database.
Syntax :-
EXEC SP_ADDLINKEDSERVER @server=N'SOMESERVER', @srvproduct=N'', @provider=N'SQLOLEDB', @datasrc=N'IP/HOST of server'
GO
EXEC SP_ADDLINKEDSRVLOGIN 'SOMESERVER', 'false', 'remoteuser', 'remoteuser', 'remotepassword'
GO
For Example for Sql Server :-
A)
EXEC sp_addlinkedserver
@server='my_temp_inventory',
@srvproduct='',
@provider='SQLNCLI',
@datasrc='INDIGO49\SQLEXPRESS'
B)
EXEC sp_addlinkedsrvlogin 'my_temp_inventory',
'true', 'my_db_username', 'my_db_username', 'murli'
Another Example :-
EXEC sp_addlinkedserver @server='my_temp_inventory',@srvproduct='',@provider='SQLNCLI',@datasrc='mutli89\SQLEXPRESS2008'
EXEC sp_addlinkedsrvlogin 'my_temp_inventory','true', 'sa', 'sa', 'admin123'
sp_linkedservers --SHOW ALL linked server list
Select * from LINKEDSERVERNAME.DATABASENAME.OWNER.TABLENAME --Execute records
SELECT * FROM OPENQUERY(LINKED_SERVER, ' DROP TABLE DB.dbo.TABLE SELECT NULL') -- drop table
Best Example :-
http://technet.microsoft.com/en-us/library/ms190479.aspx
http://networking.ringofsaturn.com/SQL/linkedservers.php 
@server='MY_TEMP_linkServer',
@srvproduct='',
@provider='SQLNCLI',
@datasrc='MYDATASOURCE\SQLEXPRESS2005'
EXEC sp_addlinkedsrvlogin @rmtsrvname = 'MY_TEMP_linkServer'
, @useself = 'false'
, @locallogin = 'sa'
, @rmtuser = 'sa'
, @rmtpassword = 'admin123'
Create Linked Server From Query:-
For Link the Two or More Server You need to follow this two steps
A) First Need to create/assign/add server name(Which you can use in program) in linked server list
B) Then set your user id and password to same linked list.
Note :- i) Server Name could be anything which you want to associate in Query.
ii) Need to assign Provider
iii) User names are must be permission to view the table/database.
Syntax :-
EXEC SP_ADDLINKEDSERVER @server=N'SOMESERVER', @srvproduct=N'', @provider=N'SQLOLEDB', @datasrc=N'IP/HOST of server'
GO
EXEC SP_ADDLINKEDSRVLOGIN 'SOMESERVER', 'false', 'remoteuser', 'remoteuser', 'remotepassword'
GO
For Example for Sql Server :-
A)
EXEC sp_addlinkedserver
@server='my_temp_inventory',
@srvproduct='',
@provider='SQLNCLI',
@datasrc='INDIGO49\SQLEXPRESS'
B)
EXEC sp_addlinkedsrvlogin 'my_temp_inventory',
'true', 'my_db_username', 'my_db_username', 'murli'
EXEC sp_addlinkedserver @server='my_temp_inventory',@srvproduct='',@provider='SQLNCLI',@datasrc='mutli89\SQLEXPRESS2008'
EXEC sp_addlinkedsrvlogin 'my_temp_inventory','true', 'sa', 'sa', 'admin123'
sp_linkedservers --SHOW ALL linked server list
Select * from LINKEDSERVERNAME.DATABASENAME.OWNER.TABLENAME --Execute records
SELECT * FROM OPENQUERY(LINKED_SERVER, ' DROP TABLE DB.dbo.TABLE SELECT NULL') -- drop table
Best Example :-
http://technet.microsoft.com/en-us/library/ms190479.aspx
http://networking.ringofsaturn.com/SQL/linkedservers.php
Subscribe to:
Comments (Atom)