Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

Hi again it's me,

 

My teacher is horrible in that she has barely touched on DB syntax yet she wants us to do all of this crazy advanced stuff. Anyway, what is the syntax for retrieving a value that's in the last row of a DB.

 

What I need to do is go to the last row of a Transactions table and retrieve the value of the Ending balance field for a specific user.

 

I think my sql string would be something like "SELECT * FROM Transactions WHERE Username='" & strUsername

 

That would allow me to get the rows for only the username entered. Is it possible to then go to the last row entered by that user assuming an autonumber as the primary key, so I can then retrieve the ending balance?

 

I will need the ending balance when the form loads so I can output it to a label.

 

Thanks

Disasterpiece

  • *Experts*
Posted

Did she mention where/how you have to find it?

 

If you return ALL rows, you can use the Rows.Count property of the DataTable to get to the last row. Make sure you sort first:

// Assume you added "ORDER BY ID" to your SELECT statement (ascending)
DataRow row = ds.Tables["Table1"].Rows[ds.Tables["Table1"].Rows.Count-1];
// Grab the value:
decimal endingBalance = (decimal)row["EndingBalance"];

 

You can also use:

// Does NOT assume you added "ORDER BY" to your SELECT statement
// Sort by column "ID" descending
DataRow[] rows = ds.Tables["Table1"].Select(string.Empty, "ID desc");

// Grab the first row - sorted descending works nice
DataRow row = rows[0];

// Grab the value:
decimal endingBalance = (decimal)row["EndingBalance"];

 

Or, you can write the SQL to just grab the last row:

"SELECT TOP 1 EndingBalance FROM Transactions WHERE Username='" & strUsername & "' ORDER BY ID desc"

 

The ORDER BY will sort in descending order. The "TOP 1" returns only one row. Together, they'll give you the last record in the table.

 

-Ner

"I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Posted

Well I don't just need the last row, I need all rows that go with a certain username. And that means that all rows for a username won't necessarily have IDs that are in order. So what if I do a SQL string that just gets the rows for a particular username, and then sort it by Date, would that work do you think?

 

I'm having trouble following your C# code as I've never been exposed to it before. It seems almost like VB but then on a second look I'm lost *lol*

  • *Experts*
Posted

Hey dp,

We know you know how to create and fill a dataset.

Been there, done that.

 

Use the sql you mentioned above and your transaction table as your datasource to create a new dataset.

The dataset will include all the transactions from that user.

You can sort by ID if the IDs are added sequentially.

Then, like Ner said, get the row count and go to the last row added.

Get the item of interest from that row and you should be done.

 

Jon

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...