Wednesday, November 9, 2016
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
Friday, November 4, 2016
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
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
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
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