.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal
Post New Web Links

How to query previous and next record?

Posted By:      Posted Date: September 25, 2010    Points: 0   Category :ASP.Net

hi there,

  i would like to query out the next record and previous record guid from the current record guid.But i don't want to use the ID + 1 or ID - 1  because the ID is not secure for query like this. I am using MS SQL.

May i know how can i write the sql query?  Please give me a guidance.



hope can hear from you all soon. Thank You

View Complete Post

More Related Resource Links

Need Help in LINQ query for group By with chunks of record


I am assigning and unique id [strShipperIdSequence] on my List on bases of some properties which are grouped together uniquely.
Now what i needed is that my group should be further break down to some maximum amount of chunks.[Let say 10]
that mean's even i am having Same value in 12 records i should get 2 groups[I of 10 items and other of 2 items]

var uniqueGroups = objMdbContentInfoList.GroupBy(p => new
}).Select(g => g.First()).ToList();
foreach (var objUnique in uniqueGroups)
string strShipperIdSequence = APIGlobalMethods.GetShipperRequestID();
foreach (MdbContentInfo obj in objMdbContentInfoList.FindAll(h => (h.CON_ENTRY_POINT == objUnique.CON_ENTRY_POINT &&
h.APPTType == objUnique.APPTType &&

query: last login record without a matching logoff record


Hello. I am creating a login/logoff audit program to track / restrict users on our network.

I have a table containing log on/off records so : loginid, userid, logindatetime, state, computername (state is either: 1 logon, -1 logoff or 0 attempted logon)

its probably quite simple but i can't quite get my head around it, i need a query to tell me who is currently logged on, ie the most recent logon (state=1) which has no logoff (state=-1) to match. the trouble is that this must cater for users being able to log on to multiple computers so somewhere the computer names must match too when eliminating records.

i currently have the following query for selecting the most recent logon, but can't think how to eliminate the ones which have a matching logoff

with cte as (
	select *, ROW_NUMBER() over (partition by UserID order by a.LoginDateTime desc) as seq from LoginAuditTable a where a.[State] = 1

Succeeding loop of the same query takes longer than the previous one


Hi, I'm having trouble with an itermittent problem I'm facing with our site. See I'm running an sql query that accepts date range as variable. When I run the query in sql manager it runs in less than a second no matter what date range I put in. I have a function that loops a dozen times to get records for the past days getting a whole day's record for every loop. It would usually work fine but there are times that the succeeding loops slows down. I made it print out the duration for each loop and this is what I got in seconds.

start: 10/5/2010 12:00:00 AM|End: 10/5/2010 11:59:59 PM|0.5460009 <-- should be more or less the normal speed of each loop
start: 10/4/2010 12:00:00 AM|End: 10/4/2010 11:59:59 PM|14.3208252
start: 10/3/2010 12:00:00 AM|End: 10/3/2010 11:59:59 PM|14.5860256
start: 10/2/2010 12:00:00 AM|End: 10/2/2010 11:59:59 PM|14.1024248
start: 10/1/2010 12:00:00 AM|End: 10/1/2010 11:59:59 PM|4.6956082
start: 9/30/2010 12:00:00 AM|End: 9/30/2010 11:59:59 PM|5.4444096
start: 9/29/2010 12:00:00 AM|End: 9/29/2010 11:59:59 PM|8.3304146
start: 9/28/2010 12:00:00 AM|End: 9/28/2010 11:59:59 PM|11.0760195
start: 9/27/2010 12:00:00 AM|End: 9/27/2010 11:59:59 PM|20.1708354
start: 9/26/2010 12:00:00 AM|End: 9/26/2010 11:59:59 PM|20.8884367
start: 9/25/2010 12:00:00 AM|End: 9/25/2010 11:59:59 PM|15.444027

Sql query to put data in one sngle record


i have one parent table and one child table.

so in parent table only one records will stay for one bunch of records and remaing records will pushed into child table.so scenario may be like this in MSATER table for one record there may be 3 reords in child table

2-in MSATER table for one record there may be 5 reords in child table

3-in MSATER table for one record there may be 10 reords in child table and so on..

parent table                                                 Child table

id  Student_name   age   phone                 id  Student_rollno   Student_name  FatherNAme
1     manoj        23  42846209862            1       34             manoj           Deepak
						    1       23             ramesh          Kailash
						    1       45             raja              tara	

I want data in this format 	    
id  Student_name   age   phone            FatherNAme1  FatherNAme2 FatherNAme3	
1           manoj    23    42846209862        Deepak     Kailash		tara		

But currently we have 3 records but if I have 10 records then also tha data comes in similar fashion means(	FatherNAme1   coloumns repeat 10 time)
How can I do this.I need to store data in a single row.						


Query Timed Out But Record Inserted



I have a parameterized stored procedure that inserts one record into a table at any one execution. The query timed out but the record was inserted. My question, is it possible for a query to time out and record gets inserted at the same time?

Thanks in advance.


Yong Hwee 

Need Help in a query... How to Display Last Record For a Given Month


Hi All,

I have a table DBGROWTHTABLE with below structure and data.

USE [ibmdba]


/****** Object: Table [dbo].[DBGROWTHTABLE] Script Date: 10/22/2010 11:54:02 ******/








	[TableEntryID] [int] IDENTITY(1,1) NOT NULL,

	[EntryDate] [smalldatetime] NOT NULL,

	[MachineName] [varchar](100) NULL,

	[InstanceName] [varchar](100) NULL,

	[DBNAME] [varchar](100) NULL,

	[DBsizeMB] [bigint] NULL,

	[unallocMB] [bigint] NULL,

	[reservedMB] [bigint] NULL,

	[dataMB] [bigint] NULL,

	[indexMB] [bigint] 

Calculating a time difference between two record and print as time_difference in MS Sql Query


Hi to all,

i dont think so its a big task but due to some reason i could not get a proper return value its returning me null which seems to be some thing is wrong in query entries that was not executing in proper way me just writing my query here could you please check where i am wrong.

dbo.ProjectA.Datestamp as Datetime1,
DateDiff(second, dbo.ProjectA.Datestamp, (select top 1 dbo.ProjectA.Datestamp from dbo.ProjectA where datepart(minute,dbo.ProjectA.Datestamp) > Dateadd(Minute,1, datepart(minute,dbo.ProjectA.Datestamp) ))) as time_difference

from dbo.ProjectA

Where (dbo.ProjectA.Datestamp between CONVERT(Datetime, '2010-09-09 06:00:00', 102)and CONVERT(Datetime, '2010-09-09 18:00:00', 102))

Group by LineID,dbo.ProjectA.Datestamp, dbo.ProjectA.ProductCod

Having (dbo.ProjectA.LineID = '1' )

order by  dbo.ProjectA.Datestamp

LineID    ProductCod             Datestamp                  time_difference
1         prdAitmq4673     2010-09-09 06:05:57.090    NULL
1     &nb

Need Query to find out the latest record insert in database


Here is my table contain the following fields

   ID    UID    OID   Date  

   1      20       1    2010-11-03 12:22:44.217

   2      30       1     2010-11-03 12:01:09.090  

   3      20      2     2010-11-03 12:22:44.217

   4      40      2   2010-11-03 12:01:09.090

i need the result like

UID   OID    Date

 30     1        2010-11-03 12:01:09.090  

 40     2         2010-11-03 12:01:09.090  

Please help me

20 2 2010-11-03 12:22:44.217

Getting First record in Nextpage and last record of previous page of gridview



I am using paging to my  gridview (pagesize=4) . now i am in  (X) page and i want to get  (X+1) page top record or (X-1)page  bottom record , how i can get .


MS SQL Server: Search All Tables, Columns & Rows For Data or Keyword Query

If you need to search your entire database for specific data, this query will come in handy.

So when a client needs a custom report or some sort of custom development using Great Plains, most of the time I will have to track down the data in the system by running this query and find the table(s) it is in.

GridView inline Master/Detail record display

This code drop is part of a smash and grab series. If you're in a rush, you can just grab this code and insert it into your application, no understanding is required. When you have some time (ya, right) you may want to review the source code.

How to Encrypt Query String Parameters in ASP.NET

Encrypt Query String Parameters in ASP.NET.u can send secure data one page another page u can also use query string to encrypt

Content Query Web Part missing in SharePoint 2010

If you don't see content query web part listed in the web parts list, this is because you have not enabled "Search Server Web Parts" feature in site collection features. Enable this feature and content query web part will show in the list of web parts.

Get Top 1 Record from Dataset

Many of them in need of getting the Top 1 or Top 10 records from a Dataset without going to SQL query. It is possible to do from Code Behind. I have given the code below,
The below code is to get single record from a Dataset. Also I have given for getting 8 records at Green Color.

Enterprize library 4.1 getting output parameter after adding record in db, plz guide



I need to get output parameter (flag) from db after saving record in database. I am using Microsoft Enterprise Library 4.1 for DB. I am inserting record using I Data reader.

Please guide


Retrieving a variable from a previous page



I'm looking to have a search page where a user will select an item from a listview, then then taken to another page that wil display data based on their original selection.

A user will type in a string and be presented with a list of usernames that are like that string, when they select a user name who's profile they would like to view I would like the page that they're taken to, to be that users profile.

How would I transfer the variable from the search page to the profile page, and when I work on the profile page, how can I access that variable?


Gridview Edit on Searching of Record


using Asp.net c#

sql server

i am searching a record from my database and displaying it in gridview now i want the comments should be updated as per the record which is being searched

For E.g if record No.3 is searched then only i can update record no.3 and i don't want rest of data to be shown

now what is happening when i click on edit my whole table is being displayed

this is bcuz i made one method gridfill() which i m calling everytime if i m doing grid row editing,row cancelling and row updating

how i can resolve this thing

my code

 protected void GridView2_RowEditing(object sender, GridViewEditEventArgs e)
        GridView2.EditIndex = e.NewEditIndex;


 protected void GridView2_RowUpdating(object sender, GridViewUpdateEventArgs e)
        string id = GridView2.Rows[e.RowIndex].Cells[0].Text;
        TextBox t;
        t = (TextBox)GridView2.Rows[e.RowIndex].Cells[2].Controls[0];
        string query = "update user1 set comments=@comments where id=@id";
        SqlConnection con = new SqlConnection(connstr);
        SqlCommand cmd = new SqlCommand(query, con);
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  Silverlight  Others  All   

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend