Register Advertise with usHelp Desk Today's Posts Search

Closed Thread
Thread Tools Search this Thread
Unread 4th Dec 2012, 03:23 AM   #1
Warrior Member
 
Join Date: 2012
Posts: 16
Thanks: 33
Thanked 0 Times in 0 Posts
Post
C#(Windows Forms): How to get the MAX column value from a database table (SQL)
Share on: 
fb share twitter share gplus share more share

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)
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?
forme is offline  
Unread 4th Dec 2012, 06:13 AM   #2
VIP Warrior
War Room Member
 
Join Date: 2006
Location: , , USA.
Posts: 1,795
Thanks: 70
Thanked 287 Times in 259 Posts
Default
Re: C#(Windows Forms): How to get the MAX column value from a database table (SQL)
Share on: 
fb share twitter share gplus share more share

Max(InvoiceNo) will be null if there are no values in the table.
KirkMcD is offline  
Unread 4th Dec 2012, 06:39 AM   #3
Warrior Member
 
Join Date: 2012
Posts: 16
Thanks: 33
Thanked 0 Times in 0 Posts
Default
Re: C#(Windows Forms): How to get the MAX column value from a database table (SQL)
Share on: 
fb share twitter share gplus share more share

Well, there are few records in the table.
forme is offline  
Unread 4th Dec 2012, 06:40 AM   #4
Warrior Member
 
Join Date: 2012
Posts: 16
Thanks: 33
Thanked 0 Times in 0 Posts
Default
Re: C#(Windows Forms): How to get the MAX column value from a database table (SQL)
Share on: 
fb share twitter share gplus share more share

Originally Posted by KirkMcD View Post

Max(InvoiceNo) will be null if there are no values in the table.
Well, there are few records in the table.
forme is offline  
Unread 4th Dec 2012, 10:50 PM   #5
Greg Miranda
War Room Member
 
CreateSoft's Avatar
 
Join Date: 2011
Location: San Diego, CA, USA
Posts: 25
Thanks: 1
Thanked 14 Times in 11 Posts
Default
Re: C#(Windows Forms): How to get the MAX column value from a database table (SQL)
Share on: 
fb share twitter share gplus share more share

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.

Downline built on Auto Pilot. No recruiting required. Click to find out how!
CreateSoft Group - Software Development Services
CreateSoft is offline  
Unread 12th Dec 2012, 03:45 AM   #6
Steve Hawkins
War Room Member
 
TrafficMystic's Avatar
 
Join Date: 2009
Location: Whitleybay, uk
Posts: 1,880
Thanks: 67
Thanked 1,125 Times in 221 Posts
Blog Entries: 3
Default
Re: C#(Windows Forms): How to get the MAX column value from a database table (SQL)
Share on: 
fb share twitter share gplus share more share

have you put an index and primary key on the table?

I suspect not.. add them and retry..

TrafficMystic is offline  
Closed Thread


Bookmarks

Tags
c#.net, c#windows, column, database, forms, max, max value, sql, table


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT -6. The time now is 08:33 AM.