4
Answers

Dataset in ASP.NET

Sushant Torankar

Sushant Torankar

Mar 03
170
1

Hello,

I'm working in c# asp.net and oracle as database. I am binding 10 records in  my Dataset ds order by Created_Date desc.

I wanto skip first record to get bind to Dataset ds. How to do it ?

If possible if I can handle it Proc, it is also fine.

My Proc :
Select * from Table T1 where CoverNote = '123456' order by Created_Date desc;

Thank You in advance !

Answers (4)
1
Jignesh Kumar

Jignesh Kumar

30 39.5k 2.9m Mar 04

Hello,

You can achieve this in two ways 

  • Database Side
SELECT *
FROM (
    SELECT T1.*, ROW_NUMBER() OVER (ORDER BY Created_Date DESC) AS RowNum
    FROM Table T1
    WHERE CoverNote = '123456'
)
WHERE RowNum > 1;  -- Skips the first row

 

  • C# side
using System;
using System.Data;
using System.Data.OracleClient;

public void GetRecords()
{
    string connectionString = "Your Oracle Connection String";
    string query = "SELECT * FROM Table T1 WHERE CoverNote = '123456' ORDER BY Created_Date DESC";
    
    using (OracleConnection conn = new OracleConnection(connectionString))
    {
        OracleDataAdapter da = new OracleDataAdapter(query, conn);
        DataSet ds = new DataSet();
        da.Fill(ds);

        // Skip the first record using condition
        if (ds.Tables[0].Rows.Count > 1)
        {
            // Remove the first row
            ds.Tables[0].Rows.RemoveAt(0);
        }

        
        // Example: GridView.DataSource = ds;
    }
}
0
Sushant Torankar

Sushant Torankar

1.4k 304 36.4k Mar 04

Hello All,

Than you for your wonderful replies, that helped me to fix my issue

0
Sangeetha S

Sangeetha S

261 7.3k 315.2k Mar 04

Instead of binding all 10 records and then skipping the first one, you can adjust your SQL query to fetch records starting from the second one. Here’s an example:

string query = "SELECT * FROM (SELECT * FROM your_table ORDER BY Created_Date DESC) 
WHERE ROWNUM <= 10 AND ROWNUM > 1";
0
Daniel Wright

Daniel Wright

769 1.1k 573 Mar 03

To skip the first record when binding data to a Dataset in ASP.NET, you can make use of the `Skip` method along with LINQ. Assuming you have already fetched the records from your database and stored them in a DataTable within your Dataset `ds`, here's how you can skip the first record:


// Assuming dt is the DataTable where your records are stored in the Dataset ds
DataTable dt = ds.Tables[0];

// Skip the first record
var recordsToBind = dt.AsEnumerable().Skip(1).CopyToDataTable();

// Now you can use recordsToBind for further processing or binding

This code snippet uses LINQ to skip the first record in your DataTable and creates a new DataTable `recordsToBind` that excludes the first row. You can then use this new DataTable for your binding operations.

If you prefer to handle this logic in a stored procedure, you can adjust your SQL query to exclude the first record. One way to achieve this is by using ROW_NUMBER() function in Oracle SQL. Here's an example:


SELECT * 
FROM (
    SELECT t.*, ROW_NUMBER() OVER (ORDER BY Created_Date DESC) AS rn
    FROM Table T1 
    WHERE CoverNote = '123456'
)
WHERE rn > 1;

By adding the ROW_NUMBER() in a subquery and filtering where `rn > 1`, you effectively skip the first record in the result set before returning the data to your ASP.NET application.

I hope this explanation helps you achieve the desired outcome in skipping the first record when binding data to your Dataset in ASP.NET. If you have any further questions or need more clarification, feel free to ask!