C#(Windows Forms): How to get the MAX column value from a database table (SQL)
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)
string connectionString = "path";
SqlConnection con;
con = new SqlConnection(connectionString);
con.Open();
SqlCommand cmd = new SqlCommand();
String query = "Select max(invoiceNo) from invoicedetails";
cmd.Connection = con;
cmd.CommandText = query;
int invNo = Convert.ToInt32(cmd.ExecuteScalar());
int newInv=invNo + 1;
textBoxInvoiceNo.Text = newInv.ToString();
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?
-
KirkMcD -
Thanks - 2 replies
{{ DiscussionBoard.errors[7426813].message }}-
forme -
Thanks
{{ DiscussionBoard.errors[7426896].message }} -
-
forme -
Thanks
{{ DiscussionBoard.errors[7426898].message }} -
-
-
CreateSoft -
Thanks
{{ DiscussionBoard.errors[7430647].message }} -
-
TrafficMystic -
Thanks
Signature[Link Alchemist] YOU ARE WASTING 80% Of Your SEO Efforts.. And you don't realise it!!!!{{ DiscussionBoard.errors[7464682].message }} -