Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
1.40/5 (2 votes)
See more:
STORED PROCEDURE
--------------------
SQL
ALTER PROCEDURE USP_Get_Invoice_No

AS

BEGIN
         SELECT MAX(invoice_no) from tbl_cleaning_purchase_categories


END


CLASS LIBRARY(USING N-TIER ARCHITECTURE)
--------------------------------------------
C#
// GET INVOICE NO
  private const string USP_Get_Invoice_No = "USP_Get_Invoice_No"
  
  public IDataReader GET_INVOICE_NO()
        {
            DbCommand com = db.GetStoredProcCommand(USP_Get_Invoice_No);
            return db.ExecuteReader(com);
        }

CODE FILE
-------------------
C#
public void LoadInvoiceNo()
       {

int InvoiceNO;
            IDataReader drInvoiceNo;
            drInvoiceNo = obj.GET_INVOICE_NO();
                while(drInvoiceNo.Read())
                {
                    string val = drInvoiceNo["invoice_no"].ToString(); ------------error
                    if (val == "")
                    {
                        InvoiceNO = Convert.ToInt32(txt_InvoiceNo.Text);
                        InvoiceNO = 1;
                    }
                    else
                    {
                        InvoiceNO = Convert.ToInt32(drInvoiceNo["invoice_no"].ToString());
                        txt_InvoiceNo.Text = (InvoiceNO + 1).ToString();

                    }
}
Posted
Updated 4-Apr-13 8:42am
v3
Comments
Prathap Gangireddy 4-Apr-13 14:46pm    
when you run this query you get only the Max Invoice_no but not the column name as it will be empty.

Try this

SELECT MAX(invoice_no) as [invoice_no] from tbl_cleaning_purchase_categories

1) Are you absolutely sure that invoice_no is the correct name for the column/key being returned? If that item cannot be found in the keys collection, then the .ToString() call will trigger the error you are receiving.
2) Seeing that you are allowing for a non return option, you could try this way:
C#
drInvoiceNo = obj.GET_INVOICE_NO();
while(drInvoideNo.Read())
{
   if( string.IsNullOrEmpty(drInvoiceNo["invoice_no"] )
   {
      InvoiceNO = Convert.ToInt32(txt_InvoiceNo.Text);
      // Why are you overriding what you set in the previous line here?
      InvoiceNO = 1;  
   }
   else
   {
      // A better way to handle the conversion.
      if( !Int32.TryParse( drInvoiceNo["invoice_no"].ToString(), out InvoiceNO ) )
      {
         InvoiceNO = ??;// Set some default value here because conversion has failed.
      }
      txt_InvoiceNo.Text = (InvoiceNO + 1).ToString();
   }
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 4-Apr-13 15:12pm    
Sure, a 5.
—SA
fjdiewornncalwe 4-Apr-13 15:25pm    
Thanks, Sergey.
Building on Marcus' solution:
Shouldn't the Stored Procedure alias the result of the MAX(invoice_no) like:
SQL
ALTER PROCEDURE USP_Get_Invoice_No
AS
BEGIN
         SELECT MAX(invoice_no) as "Max_invoice_no" from tbl_cleaning_purchase_categories
END

and then use "Max_invoice_no" as the column/key:
C#
drInvoiceNo = obj.GET_INVOICE_NO();
while (drInvoiceNo.Read())
{
   var max_invoice_no = drInvoiceNo["Max_invoice_no"].ToString(); 
   if ( string.IsNullOrEmpty(max_invoice_no) )
   {
      max_invoice_no = txt_InvoiceNo.Text;
   }
   // A better way to handle the conversion.
   if ( !Int32.TryParse( max_invoice_no, out InvoiceNO ) )
   {
      InvoiceNO = ??;// Set some default value here because conversion has failed.
   }
   txt_InvoiceNo.Text = (InvoiceNO + 1).ToString();
}

Another possibility, if you cannot change the stored procedure, would be to just get the 0-th index in drInvoiceNo since the Max is the only value returned from the stored procedure.
 
Share this answer
 
v2
Comments
Prathap Gangireddy 4-Apr-13 15:01pm    
Hi Matt,

I have already advised the same stuff as you can see my comment.
Matt T Heffron 4-Apr-13 15:04pm    
You posted while I was typing... :)
Sergey Alexandrovich Kryukov 4-Apr-13 15:12pm    
5ed.
—SA
Prathap Gangireddy 4-Apr-13 15:14pm    
:-)
fjdiewornncalwe 4-Apr-13 15:26pm    
+5. Nice addition.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900