RSS

How to use a simple AJAX Update Panel Control in Asp.Net


For using simple update panel in asp.net first need add the Script Manager, after that add the Update Panel control and add the <ContentTemplate> </ContentTemplate>, <Triggers>, </Triggers> tags.

- Inside the Content template please add the list of controls.
- Inside the Triggers add asp:AsyncPostBackTrigger for the event capture for the which controls event need to use the update panel.

The following codes are easy understand the AJAX Update panel control


<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>






Working with AJAX Update Panel With Script Manager











Enter Your Name:




Enter Your DOB :








The C# code behide is looks like :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
// Working with Ajax Update Panel
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblAge.Visible = false;
}
// When the txtDOB text change event fired we need to calculate the
//Age of the Give Person and display Age in Years with Days
protected void txtDOB_TextChanged(object sender, EventArgs e)
{
// TimeSpan class is used to find the time differnece and stored result temp
TimeSpan tsp=TimeSpan.MinValue;
try
{
DateTime dtDOB = Convert.ToDateTime(txtDOB.Text.Trim());
tsp = DateTime.Now - dtDOB;
}
catch (Exception ex)
{
lblAge.Visible = true;
lblAge.Text=ex.Message.Trim();
}
lblAge.Visible = true;
lblAge.Text = "Hi Mr/Ms." + TextBox1.Text.Trim() + ",
Your AGE:" + (tsp.Days / 365).ToString() + " Years " + (tsp.Days % 365).ToString() + " Days";
}
}

read comments Read User's Comments

Some of Sql Server Circumstance


1.DECLARE @RowName Varchar(20)
SET @ROWName='SiteName'
SELECT * FROM M_Site ORDER BY @ROWName
What will be the Result?
ANSWER :
Msg 1008, Level 16, State 1, Line 3
The SELECT item identified by the ORDER BY number 1 contains a variable as part of the expression identifying a column position. Variables are only allowed when ordering by an expression referencing a column name.

2.SELECT 'test' Where exists(Select *)
Is the above Statement will throw error or Not, if Not what will be the Result?
ANSWER : test

3. What is Result for SELECT * , SELECT COUNT(*) and what is difference between them.

4. What is use of ‘WITH SCHEMABINDING’ in Sql Server?
Binds the view to the schema of the underlying table or tables. When SCHEMABINDING is specified, the base table or tables cannot be modified in a way that would affect the view definition. The view definition itself must first be modified or dropped to remove dependencies on the table that is to be modified. When you use SCHEMABINDING, the select statement must include the two-part names (schema. object) of tables, views, or user-defined functions that are referenced. All referenced objects must be in the same database.

Views or tables that participate in a view created with the SCHEMABINDING clause cannot be dropped unless that view is dropped or changed so that it no longer has schema binding. Otherwise, the Microsoft SQL Server 2005 Database Engine raises an error. Also, executing ALTER TABLE statements on tables that participate in views that have schema binding fail when these statements affect the view definition.
SCHEMABINDING cannot be specified if the view contains alias data type columns.

What does this mean?
Well, to put it simply, once you create a view with schema binding, you cannot change the underlying tables in a way that would break the view. Examples of this would be removing columns or dropping tables that are specified in the view.
WITH SCHEMABINDING can be used in Views and T-SQL Functions, but not in Stored Procedures.

read comments Read User's Comments

Triggers And it's Type

Triggers in SQL Server

- Normally the Triggers are special kind of stored procedure, it will fire when some event occured in the sQL object like Table, View.
- The triggers will fire before/After Insert, UPdate, Delete operations made on the Talbes or Views.
- Those Trigger is Classified into 2 categories.
1. After Triggers and
2. Instead Triggers.
1. After(For) Triggers:
The After triggers are excuted the After the above set operations(eigther Insert, Upadate or Delete)
Example:

--After Insert and After Update
CREATE TRIGGER ToAlert
ON M_Customer
FOR INSERT -- (OR)
FOR UPDATE
AS
SELECT 'You Have Inserted a New Record!'
GO
-- After Delete Tigger
CREATE TRIGGER ToDelAlert
ON M_Customer
AFTER DELETE
AS
SELECT 'You have Deleted a Records!'
GO

2. Instead Of Triggers 

The instead of triggers are fired before the Operation what we given for the triggers (i.e., Insert, Update, Delete)


-- INSTED OF UPDATE/INSERT
CREATE TRIGGER ToAlert
ON M_Customer
INSTEAD OF INSERT -- (OR)
INSTEAD OF UPDATE
AS
SELECT 'You going to Make Insert a New Record or Update Operation! '
GO
-- INSTEAD DELETE
CREATE TRIGGER ToDelAlert
ON M_Customer
INSTEAD OF DELETE
AS
SELECT 'Are you going Delete a Records!'
GO

- Normally triggers are like a events in a programming language.
- We unable to call explicitly, this will fire while the event occured for the DB objects which its assigned for the particular operations.
- We unable to use triggers for Truncate Table, DROP table sql commands.

read comments Read User's Comments

Inserting bulk Data Using Sql's Bulk Insert and XP_CMDSHELL Jobs

- To upload the list of available CSV or ‘^’ separated files in the shared location to the Sql server using SQL Bulk Insert and the ‘XP_cmdshell’.
- The SQL Bulk insert command is used to upload the special character separated files into DB at a time with Line feeds terminate.
- The ‘XP_CmdShall’ is used to list the files available the directory and accessing the files for bulk insert.
o For Running this we need a admin privilege and also need reconfigure to execute this Job.
o ‘sp_configure’ is used to reconfiguration.
- For Processing and Tracking Purpose we need 3 tables
o T_FILEDETAIL
 This is used to Tracking the each files loaded and if made successfully then ‘FileStatus’ flage is to be ‘Y’, otherwise ‘N’.
o T_VikingData_Middleware
 This table is act as a middleware for storing each file details temporaraly and moved to the T_Viking data orignal table.
o T_VIKINGDATA
 This is the Main table to maintain all the values from the CSV or ‘^’ seprated files.
- The Table structures are:


CREATE PROCEDURE [dbo].[USP_BulkCSVUploader]
@DESTINATIONPATH VARCHAR(550)
AS
BEGIN
SET NOCOUNT ON;
DECLARE @FILENAMES VARCHAR(64);
DECLARE @CMDSCRIPT VARCHAR(8000);
-- DECLARE @DESTINATIONPATH VARCHAR(512);
DECLARE @MINROW INT;
DECLARE @MAXROW INT;
DECLARE @QRYCMD1 VARCHAR(1500);
DECLARE @QRYCMD2 VARCHAR(1500);
DECLARE @FILETABLE TABLE (ROWID INT IDENTITY, FILENAMES VARCHAR(128))
---** CREATING TEMP TABLE ** ---
CREATE TABLE #ORIGINALFILELIST
(
ROWID INT IDENTITY,
FILENAMES VARCHAR(1000) NULL
);

---** DESTINATION PATH WHERE RPT FILES ARE KEPT ** ---
-- SET @DESTINATIONPATH = '\\10.16.105.171\VIKINGDATA\UHG\DCNLOG\'
--SET @DESTINATIONPATH = '\\10.16.131.141\e\error\'
DELETE
FROM T_FILEDETAIL
WHERE FILESTATUS='N'
BEGIN TRY
---- To allow advanced options to be changed.
EXEC sp_configure 'show advanced options',
1
-- To update the currently configured value for advanced options.
RECONFIGURE
-- To enable the feature.
EXEC sp_configure 'xp_cmdshell',
1
-- To update the currently configured value for this feature.
RECONFIGURE
END TRY
BEGIN CATCH
PRINT ERROR_MESSAGE()
END CATCH
---** BUILD THE STRING TO CAPTURE THE FILE NAMES IN THE RESTORE LOCATION ** ---
SELECT @QRYCMD1 = 'MASTER.DBO.XP_CMDSHELL ' + CHAR(39) + 'DIR ' + @DESTINATIONPATH +'\*.* /L/B' + CHAR(39);

---** BUILD THE STRING TO POPULATE THE #ORIGINALFILELIST TEMPORARY TABLE ** ---
SELECT @QRYCMD2 = 'INSERT INTO #ORIGINALFILELIST(FILENAMES)' + CHAR(13) + 'EXEC ' + @QRYCMD1;

---** EXECUTE THE STRING TO POPULATE THE #ORIGINALFILELIST TABLE ** ---
EXEC (@QRYCMD2);
SELECT @DESTINATIONPATH,
FILENAMES ,
FILENAMES
FROM #ORIGINALFILELIST;

---** STORING THE RPT FOLDER FULL DETAILS
INSERT
INTO T_FILEDETAIL
(
FILENAMES ,
FILELOCATION,
FILESTATUS ,
FILEUPDATETIME
)
SELECT FILENAMES ,
@DESTINATIONPATH,
'N' ,
GETDATE()
FROM #ORIGINALFILELIST
---** STORING THE RPT FOLDER STATUS DETAILS ** ---
INSERT
INTO @FILETABLE
SELECT FILENAMES
FROM (SELECT FILENAMES,
FILESTATUS
FROM T_FILEDETAIL
WHERE (
FILENAMES LIKE '%.dcnlog'
OR FILENAMES LIKE '%.rpt'
)
AND FILESTATUS = 'N'
)
r
---** SELECTING ROWS ** ---
SELECT @MINROW = MIN(ROWID)
FROM @FILETABLE ;

SELECT @MAXROW = MAX(ROWID)
FROM @FILETABLE ;

IF EXISTS
(SELECT TOP 1
*
FROM @FILETABLE
)
BEGIN
WHILE @MINROW <= @MAXROW
BEGIN
TRUNCATE TABLE DBO.T_VikingData_Middleware;

SELECT @FILENAMES = FILENAMES
FROM @FILETABLE
WHERE ROWID = @MINROW;

UPDATE T_FILEDETAIL
SET FILESTATUS = 'W'
WHERE FILENAMES = @FILENAMES
---** INSERTING INTO INTERMEDIATE TABLE ** ---
BEGIN TRY
SET @CMDSCRIPT = 'BULK INSERT T_VikingData_Middleware FROM '''+@DESTINATIONPATH+ @FILENAMES+''' WITH (FIELDTERMINATOR = ''^'',ROWTERMINATOR = '''+CHAR(10)+''')'
EXEC(@CMDSCRIPT)
---** INSERTING INTO MAIN TABLE ** ---
INSERT
INTO T_VIKINGDATA
(
CUST ,
PROJ ,
JULIANDATE ,
FOLDER ,
DCN ,
STAGEMODE ,
USERID ,
COFNAME ,
STARTTIME ,
ENDTIME ,
VIKDOCUMENT ,
FIELDVISITED ,
KEYSTROKES ,
VALIDCHARS ,
VERCHAR ,
VERCHARCHANGE,
VERFLDCHANGE ,
TDIFF ,
EventON ,
LOC_ORIGINAL ,
location
)
SELECT CUST ,
PROJ ,
JULIANDATE ,
FOLDER ,
DCN ,
STAGEMODE ,
USERID ,
COFNAME ,
STARTTIME ,
ENDTIME ,
VIKDOCUMENT ,
FIELDVISITED ,
KEYSTROKES ,
VALIDCHARS ,
VERCHAR ,
VERCHARCHANGE ,
CAST(SUBSTRING(VerFldChange, 1, 1) AS INT),
DATEDIFF(SECOND,STARTTIME,ENDTIME) ,
GETDATE() ,
LOCATION ,
LOCATION
FROM T_VikingData_Middleware
UPDATE T_FILEDETAIL
SET FILESTATUS = 'Y'
WHERE FILENAMES = @FILENAMES
END TRY
BEGIN CATCH
PRINT 'IN ERR 1' +CONVERT(VARCHAR,ERROR_NUMBER()) + ERROR_MESSAGE()
UPDATE T_FILEDETAIL
SET ERR_REASON = ERROR_MESSAGE()
WHERE FILENAMES = @FILENAMES
END CATCH
SET @MINROW = @MINROW + 1;
END
UPDATE V
SET LOCATION=DBO.FN_SPLIT_N(LOCATION,'^')
FROM T_VIKINGDATA V
WHERE LOC_ORIGINAL LIKE '%^%'
END
END

read comments Read User's Comments

Removing Duplicate Records from the Table in SQL Server


If we need to delete or select the duplicated records from the table, we can do it in two ways.
1. If the tables is having identity column then use the Group By with MAx() to get the list of duplicated records.

  
-- The Bellow Sql Statement will Display the Duplicate Records Details
SELECT *
FROM M_Site
WHERE SEQID NOT IN
(SELECT MAX(SEQID)
FROM M_Site
GROUP BY SiteName
)
-- The Bellow SQL Statement will used to delete the Duplicated recoreds
DELETE
FROM M_Site
WHERE SEQID NOT IN
(SELECT MAX(SEQID)
FROM M_Site
GROUP BY SiteName
)



If the tables doesn't have identity column the we unable to use the group by With Max() clause alone.
So we need an alternative solutions. The bellow examples uses the ROW_NUMBER() Over Partition Clause to getting the
duplicated or tiriuplicated records from the DB.


Delete from a
from
(select Emp_Name, Company, Join_Date, Resigned_Date
,ROW_NUMBER() over (partition by Emp_Name, Company, Join_Date
,Resigned_Date
order by Emp_Name, Company, Join_Date
,Resigned_Date) RowNumber
from Emp_Details) a
where a.RowNumber > 1

-- For More Please visit:http://www.sqlservercentral.com/articles/T-SQL/70807/

read comments Read User's Comments