C#(Windows Forms): How to get the MAX column value from a database table (SQL)

by 5 replies
8
I have a table named invoicedetails in my database.
I have to get the max value of the invoiceNo column and display it after incremented that value by 1. I searched hours on the net and found several solutions. This is what I tried,

(no problem with the connection)


I have put this code to the form load event. But the InvoiceNo text box displays nothing. And this
exception displays:-"Object cannot be cast fromDBNull to other types".
Where's the problem?
#programming #c#.net #c#windows #column #database #forms #max #max value #sql #table
  • Max(InvoiceNo) will be null if there are no values in the table.
    • [2] replies
    • Well, there are few records in the table.
    • Well, there are few records in the table.
  • From a quick glance, I don't see anything wrong, so I would suggest testing your query using a query analyzer and see if it really is returning data.
  • have you put an index and primary key on the table?

    I suspect not.. add them and retry..

Next Topics on Trending Feed