本文介绍了索引超出范围的异常未得到处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 存储程序 -------------------- STORED PROCEDURE--------------------ALTER PROCEDURE USP_Get_Invoice_NoASBEGIN SELECT MAX(invoice_no) from tbl_cleaning_purchase_categoriesEND CLASS LIBRARY(使用N层架构) ------------------ -------------------------- CLASS LIBRARY(USING N-TIER ARCHITECTURE)--------------------------------------------// 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-------------------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(); }} 推荐答案 1)你绝对确定 invoice_no 是要返回的列/键的正确名称?如果在密钥集合中找不到该项目,则 .ToString()调用将触发您收到的错误。 2)看到你可以尝试这种方式: 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: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(); }} 以Marcus的解决方案为基础: 难道不是存储过程别名 MAX(invoice_no)的结果如: Building on Marcus'' solution:Shouldn''t the Stored Procedure alias the result of the MAX(invoice_no) like:ALTER PROCEDURE USP_Get_Invoice_NoASBEGIN SELECT MAX(invoice_no) as "Max_invoice_no" from tbl_cleaning_purchase_categoriesEND 然后使用Max_invoice_no作为列/ key: and then use "Max_invoice_no" as the column/key: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();} 另一种可能性,如果你不能改变存储过程,就是获得中的第0个索引drInvoiceNo 因为Max是存储过程返回的唯一值。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. 这篇关于索引超出范围的异常未得到处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-16 12:07