posts - 5 , comments - 1 , trackbacks - 0

Wednesday, November 9, 2016

Reposition DataTable Columns

After data bound to DataTable (say dtUser is DataTable name), if we would like to interchange FirstName (first column) and LastName (second column) columns position then we have to do like below

dtUser.Columns["FirstName"].SetOrdinal(1);  

dtUser.Columns["LastName"].SetOrdinal(0); //Initial ordinal position is ZERO 

Now LastName will be the first column and FirstName will be the second column in data table

--Karthick Raju



Posted On Wednesday, November 9, 2016 4:54 AM | Comments (0) | Filed Under [ Asp.Net C# ]

Friday, November 4, 2016

Reset Identity column in SQL table

To reset Identity from 1

Below would reset identity column of SQL table and start Identity column value from 1

DBCC CHECKIDENT (TABLE_NAME, reseed, 0)


To reset Identity from the specified value 

Say, If records are already deleted (from 31 to the end record ) and if user would like to create record from 31 then below will work

DBCC CHECKIDENT (TABLE_NAME, reseed, 30)

– Karthick Raju

Posted On Friday, November 4, 2016 6:44 AM | Comments (1) | Filed Under [ SQL SQL Server ]

How to execute Dynamic SQL query

Below SQL script would help to run simple dynamic query

DECLARE @SQLQuery NVARCHAR(500) — To see max length for NVARCHAR
SET @SQLQuery = N’SELECT * FROM TABLE_NAME’ — To understand why N’ in this line
EXEC(@SQLQuery)

Ensure you execute dynamically framed query like EXEC (@SQLQuery) not like EXEC @SQLQuery. If you execute like later then you would get below error

Msg 2812, Level 16, State 62, Line 3
Could not find stored procedure ‘SELECT * FROM TABLE_NAME’.

-Karthick Raju

Posted On Friday, November 4, 2016 6:43 AM | Comments (0) | Filed Under [ SQL SQL Server Dynamic Query ]

ASP.Net Prevent navigation on Back Space button

Web sites will usually redirect user to previous page, if Back Space button is pressed. I have recently got the requirement to prevent user from navigating during this scenario.

For this, we have to prevent back space button’s event fire on key down. But, if focus is on text box/text area then Back Space should work.

Below code will work if you have this as a requirement:

// Prevent the backspace key from navigating back.
$(document).keydown(function (e) {
var doPrevent;
if (e.keyCode == 8) {
var d = e.srcElement || e.target;
if (d.tagName.toUpperCase() == ‘TEXT’ || d.tagName.toUpperCase() == ‘TEXTAREA’ ) {
doPrevent = d.readOnly || d.disabled;
}
else
doPrevent = true;
}
else
doPrevent = false;

if (doPrevent)
e.preventDefault();
});
 

-Karthick Raju

Posted On Friday, November 4, 2016 6:38 AM | Comments (0) | Filed Under [ Asp.Net JavaScript ]

Sort listbox items using jQuery in ASP.Net

First of all, we have to refer jQuery on the page. Kindly down the latest jQuery from below url


Below is the function, will sort list box items in the ascending order

function SortListbyAscending(listname) {
var $r = $(listname + ” option”);
$r.sort(function (a, b) {
return parseInt(a.value) == parseInt(b.value) ? 0 : parseInt(a.value) < parseInt(b.value) ? -1 : 1;

});

$($r).remove();

$(listname).append($($r));

}

Pass listbox as a parameter to run the above function. My requirement is to do it on button click. Please find below how to call

$(“#btnSubmit”).bind(“click”, function () {

SortListbyAscending(“[id*=lstBoxId]”);

});

That’s it.

– Karthick Raju

Posted On Friday, November 4, 2016 6:32 AM | Comments (0) | Filed Under [ Asp.Net JavaScript jQuery ]

Powered by: