2011-05-22 70 views
0

我一直得到“出现FormatException是由用户代码未处理的” 以下是代码:如何通过ComboBox在C#.Net中检索第一个索引的值?

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Data.Sql; 
using System.Data.SqlClient; 
using System.Configuration; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace Eventmanagement 
{ 
    public partial class Registration : Form 
    { 
     SqlConnection aConnection; 
     string firstname= string.Empty; 
     string lastname= string.Empty; 
     int aid; 
     string date = DateTime.Now.ToShortDateString(); 
     SqlDataAdapter da = new SqlDataAdapter(); 
     DataTable dta; 


     public Registration(string fname, string lname, int attID) 
     { 
      this.firstname = fname; 
      this.lastname = lname; 
      this.aid = attID; 
      InitializeComponent(); 
     } 
     //--------------------------------------------// 

     private void Registration_Load(object sender, EventArgs e) 
     { 
      // TODO: This line of code loads data into the 'insertEventDataSet.Events' table. You can move, or remove it, as needed. 

      populateEventSalesPersonList(); 
      populateEventNameIdTypeList(); 

      //+++++++++++++++++++++++++++++++++++++++++++// 
      txtSalesTaxRate_Registration.Text = Convert.ToString(SalesTaxRate()); 
      txtRegistrationID_Registration.Text = regID().ToString(); 
      //+++++++++++++++++++++++++++++++++++++++++++// 

      //+++++++++++++++++++++++++++++++++++++++++++// 
      txtAttendee_Registration.Text = (this.firstname+" "+this.lastname); 
      txtRegistrationDate_Registration.Text = date.ToString(); 


     } 

     //--------------------------------------------// 

     public string getConnectionString() 
     { 
      try 
      { 
       string sConnection = ""; 

       // Get the mapped configuration file. 
       System.Configuration.ConnectionStringSettingsCollection ConnSettings = ConfigurationManager.ConnectionStrings; 
       sConnection = ConnSettings["DBConnectionString"].ToString(); 

       return sConnection; 


      } 
      catch (Exception err) 
      { 
       MessageBox.Show(err.Message); 
       return ""; 
      } 
     } 



     //--------------------------------------------// 
     public void populateEventNameIdTypeList() 
     { 
      try 
      { 
       cmbEvent_Registration.DataSource = getDataTable4(); 
       //---------------------------- 
       cmbEvent_Registration.ValueMember = "EventID"; 
       cmbEvent_Registration.DisplayMember = "EventName"; 




      } 
      catch (Exception err) 
      { 
       MessageBox.Show(err.Message); 
      } 


     } 


     //--------------------------------------------// 

     public void populateEventSalesPersonList() 
     { 
      try 
      { 

       cmbSalesPerson_Registration.DataSource = getDataTable5(); 
       cmbSalesPerson_Registration.ValueMember = "EmployeeID"; 
       cmbSalesPerson_Registration.DisplayMember = "SalesPerson"; 

      } 
      catch (Exception err) 
      { 
       MessageBox.Show(err.Message); 
      } 
     } 

     //-------------------------------------------// 

     public void populateFeeScheduleByEventList() 
     { 

      try 
      { 

       cmbFeeSchedule_Registration.DataSource = getDataTable6(); 
       cmbFeeSchedule_Registration.ValueMember = "FeeScheduleID"; 
       cmbFeeSchedule_Registration.DisplayMember = "Fee"; 




       //--------------------------------------------------// 

       //saleTax(); 
       //txtSalesTax_Registration.Text = Convert.ToString(saleTax()); 
       //txtTotalCharges_Registration.Text = Convert.ToString(totalCharges()); 
       //txtAmountDue_Registration.Text = Convert.ToString(amountDue()); 

       //--------------------------------------------------// 
       } 

      catch (Exception err) 
      { 
       MessageBox.Show(err.Message); 
      } 

     } 

     //------------------------------------------// 

     //------------------------------------------// 



     private void btnclose_Registration_Click(object sender, EventArgs e) 
     { 
      this.Close(); 
     } 

     private void btnInsert_Registration_Click(object sender, EventArgs e) 
     { 
      try 
      { 
       aConnection = new SqlConnection(getConnectionString()); 
       aConnection.Open(); 

       //Calling the Stored Procedure 

       da.InsertCommand = new SqlCommand("RegistrationInsert", aConnection); 
       da.InsertCommand.CommandType = CommandType.StoredProcedure; 

       //da.InsertCommand.Parameters.Add(@"RegistrationID", SqlDbType.Int).Value = Convert.ToInt16(txtRegistrationID_Registration.Text); 
       da.InsertCommand.Parameters.Add(@"AttendeeID", SqlDbType.Int).Value = Convert.ToInt16(aid); 
       da.InsertCommand.Parameters.Add(@"RegistrationDate", SqlDbType.SmallDateTime).Value = Convert.ToDateTime(txtRegistrationDate_Registration.Text=date.ToString()); 
       da.InsertCommand.Parameters.Add(@"PurchaseOrderNumber", SqlDbType.VarChar).Value = txtPoNumber_Registration.Text; 
       // da.InsertCommand.Parameters.Add(@"SalesPerson", SqlDbType.Int).Value = Convert.ToInt64(cmbSalesPerson_Registration.SelectedValue.ToString()); 
       da.InsertCommand.Parameters.Add(@"EventID", SqlDbType.Int).Value = cmbEvent_Registration.SelectedValue.ToString(); 
       da.InsertCommand.Parameters.Add(@"FeeScheduleID", SqlDbType.Int).Value = cmbFeeSchedule_Registration.SelectedValue.ToString(); 
       da.InsertCommand.Parameters.Add(@"RegistrationFee", SqlDbType.Money).Value = txtRegistrationFee_Registration.Text; 
       da.InsertCommand.Parameters.Add(@"EmployeeID", SqlDbType.Int).Value = Convert.ToInt16(cmbSalesPerson_Registration.SelectedValue.ToString()); 
       da.InsertCommand.Parameters.Add(@"SalesTaxRate", SqlDbType.Float).Value = txtSalesTaxRate_Registration.Text; 
       //da.InsertCommand.Parameters.Add(@"EndTime", SqlDbType.SmallDateTime).Value = Convert.ToDateTime(txtEndTime.Text.ToString()); 
       da.InsertCommand.ExecuteNonQuery(); 


       MessageBox.Show("Inserted successfully!!!"); 

       aConnection.Close(); 
       da.InsertCommand.Dispose(); 

      } 
      catch (Exception err) 
      { 
       MessageBox.Show(err.Message); 
      } 


     } 

     //-------------------------------------------// 

     public DataTable getDataTable4() 
     { 
      try 
      { 
       dta = new DataTable(); 
       aConnection = new SqlConnection(getConnectionString()); 
       aConnection.Open(); 
       da = new SqlDataAdapter(); 
       da.SelectCommand = new SqlCommand("EventsSelectAll", aConnection); 
       da.SelectCommand.CommandType = CommandType.StoredProcedure; 
       da.SelectCommand.ExecuteNonQuery(); 

       da.Fill(dta); 
       aConnection.Close(); 
       return dta; 

      } 
      catch (Exception err) 
      { 
       MessageBox.Show(err.Message); 
       return null; 
      } 
     } 
     public DataTable getDataTable5() 
     { 
      try 
      { 
       dta = new DataTable(); 
       aConnection = new SqlConnection(getConnectionString()); 
       aConnection.Open(); 
       da = new SqlDataAdapter(); 
       da.SelectCommand = new SqlCommand("sp_RegistrationSalesPerson", aConnection); 
       da.SelectCommand.CommandType = CommandType.StoredProcedure; 
       da.SelectCommand.ExecuteNonQuery(); 
       dta.Clear(); 
       da.Fill(dta); 
       aConnection.Close(); 
       return dta; 

      } 
      catch (Exception err) 
      { 
       MessageBox.Show(err.Message); 
       return null; 
      } 
     } 
     public DataTable getDataTable6() 
     { 
      try 
      { 
       dta = new DataTable(); 
       da = new SqlDataAdapter(); 
       aConnection = new SqlConnection(getConnectionString()); 

       aConnection.Open(); 

       da.SelectCommand = new SqlCommand("sp_FeeScheduleSelectAllByEventID", aConnection); 
       da.SelectCommand.Parameters.Add("EventID", SqlDbType.Int).Value = Convert.ToInt32(cmbEvent_Registration.SelectedValue.ToString()); 
       da.SelectCommand.CommandType = CommandType.StoredProcedure; 


       da.SelectCommand.ExecuteNonQuery(); 
       da.Fill(dta); 
       aConnection.Close(); 
       return dta; 
      } 
      catch (Exception err) 
      { 
       MessageBox.Show(err.Message); 
       return null; 
      } 


     } 



     private void cmbEvent_Registration_SelectedIndexChanged(object sender, EventArgs e) 
     { 
       populateFeeScheduleByEventList(); 

       txtRemainingSeats_Registration.Text = Convert.ToString(spaces()); 
     } 




     public double saleTax() 
     {        
      double regFee = Convert.ToDouble(cmbFeeSchedule_Registration.SelectedText); 
      double sTR = Convert.ToDouble(SalesTaxRate()); 
      double sr = regFee * (sTR/100); 
      return sr; 
     } 
     public int totalRegisteredAttendees() 
     { 
      da = new SqlDataAdapter(); 
      aConnection = new SqlConnection(getConnectionString()); 
      da.SelectCommand = new SqlCommand("RegistrationSelectAllByEventID", aConnection); 
      da.SelectCommand.CommandType = CommandType.StoredProcedure; 
      da.SelectCommand.Parameters.Add("EventID", SqlDbType.Int).Value = Convert.ToInt32(cmbEvent_Registration.SelectedValue.ToString()); 

      aConnection.Open(); 
      int val = (int)da.SelectCommand.ExecuteScalar(); 
      aConnection.Close(); 
      return val; 
     } 
     public int spaces() 
     { 
      da = new SqlDataAdapter(); 
      aConnection = new SqlConnection(getConnectionString()); 
      da.SelectCommand = new SqlCommand("EventsSelect", aConnection); 
      da.SelectCommand.CommandType = CommandType.StoredProcedure; 
      da.SelectCommand.Parameters.Add("EventID", SqlDbType.Int).Value = Convert.ToInt32(cmbEvent_Registration.SelectedValue.ToString()); 
      aConnection.Open(); 

      int spaces = Convert.ToInt16(da.SelectCommand.ExecuteScalar()); 
      aConnection.Close(); 

      int value = totalRegisteredAttendees(); 
      int totalspaces = spaces - value; 
      return totalspaces; 

     } 

     public double SalesTaxRate() 
     { 
      da = new SqlDataAdapter(); 
      aConnection = new SqlConnection(getConnectionString()); 
      da.SelectCommand = new SqlCommand("CompanyInfo", aConnection); 
      da.SelectCommand.CommandType = CommandType.StoredProcedure; 

      aConnection.Open(); 
      double sale = Convert.ToDouble(da.SelectCommand.ExecuteScalar()); 
      aConnection.Close(); 
      return sale; 
     } 


     public double totalCharges() 
     { 
      double tc = Convert.ToDouble(txtRegistrationFee_Registration.Text) + (saleTax()); 
      return tc; 
     } 
     public double amountDue() 
     { 
      double aD; 
      if (txtTotalPaid_Registration.Text == string.Empty) 
      { 
       aD = Convert.ToDouble(txtTotalCharges_Registration.Text.ToString()); 
      } 
      else 
      { 
       double a = Convert.ToDouble(txtTotalPaid_Registration.Text.ToString()); 
       double b= (Convert.ToDouble(txtTotalCharges_Registration.Text.ToString())); 
       aD = b-a; 
      } 
       return aD; 
     } 

     public int regID() 
     { 
      da = new SqlDataAdapter(); 
      aConnection = new SqlConnection(getConnectionString()); 
      da.SelectCommand = new SqlCommand("RegistrationID", aConnection); 
      da.SelectCommand.CommandType = CommandType.StoredProcedure; 

      aConnection.Open(); 
      int id = ((int)da.SelectCommand.ExecuteScalar())+1; 

      aConnection.Close(); 
      return id; 
     } 

     private void cmbFeeSchedule_Registration_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      saleTax(); 
     } 

     //------------------------------------------// 
    } 
} 

当我打电话SaleTax()方法在下列

private void cmbFeeSchedule_Registration_SelectedIndexChanged(object sender, EventArgs e) 
     { 
      saleTax(); 
     } 

我得到“出现FormatException是用户代码未处理“错误 我调试了代码并发现它正在发生,因为cmbFeeSchedule_Registration没有选择第一个索引,因此它给出了e RROR。我很困惑该怎么办? 我如何解决这个问题?

编辑: 那么我解决了这个问题。这是问题的'公共无效populateFeeScheduleByEventList(){

 try 
     { 

      cmbFeeSchedule_Registration.DataSource = getDataTable6(); 
      cmbFeeSchedule_Registration.ValueMember = "FeeScheduleID"; 
      cmbFeeSchedule_Registration.DisplayMember = "Fee"; 
     catch (Exception err) 
     { 
      MessageBox.Show(err.Message); 
     } 

    }` 

SaleTax()叫,指针离开DisplayMember and ValueMember。因此我重新调整了代码,并在 cmbFeeSchedule_Registration.DataSource = getDataTable6();之前放置了cmbFeeSchedule_Registration.ValueMember = "FeeScheduleID";cmbFeeSchedule_Registration.DisplayMember = "Fee";并解决了问题。 :)

+0

你可以发布你的StackTrace吗? – 2011-05-22 18:13:51

回答

2

如果要求索引0在你的组合框被选中,那么你需要做一些基本的检查。在您的SelectedIndexChanged事件处理线,你需要添加:

if (cmbFeeSchedule_Registration.SelectedIndex == 0) { 
    salesTax(); 
} 

此外,如果用户能够更改文本在ComboBox,你是不是做任何错误处理,以处理将导致的出现FormatException尝试转换非可解析的非double值时Convert.ToDouble。

double regFee = Convert.ToDouble(cmbFeeSchedule_Registration.SelectedText);    
double sTR = Convert.ToDouble(SalesTaxRate()); 

而且,你可以实际使用double.Parsedouble.TryParse代替的ConvertTo,这似乎更恰当。在另一个说明中,如果用户无法编辑ComboBox中的文本(假设您将其设置为DropDownList),则应使用SelectedObject或SelectedValue属性而不是SelectedText,具体取决于它是否为DataBound。

底线是您需要确保在调用salesTax()或任何可能会引发异常的操作之前检查是否选择了正确的索引,并执行错误检查以确保您的值是他们应该。如果regFee不是双重的,因为没有选择正确的索引,ConvertTo将会失败。 (反正使用double.Parse/TryParse)

发布问题时的一个建议是只发布受影响的代码段,并在有可用时提供StackTrace。在这里导航所有的代码非常困难,并且使用StackTrace很容易中断。

0

你必须做出的selectedIndex = 0的形式

0

只是这样做:

if(((ComboBox)sender).SelectedIndex > -1) saleTax(); 

代替。如果要在表单加载时选择第一个项目,请在Load事件中设置SelectedIndex