2013-05-01 66 views
0

請有人可以幫助我我不斷收到這個錯誤,它告訴我它的格式不正確,但我把它列爲一個整數。如果你能告訴我我是一個愚蠢的人,你將成爲我的英雄。我確信感激任何人的幫助,是的,我剛剛在asp.net中有點出發,所以請原諒我。關於在asp.net c中說錯格式的錯誤代碼#

該頁面是一項調查,可從客戶ID獲取事件。它使用sqlconnection,並罰款直到我點擊提交按鈕,然後錯誤。

這是我不斷收到的錯誤代碼,我想我只是沒有看到它。我不明白爲什麼它導致這個錯誤,我認爲這將是一個選定的值的問題。對於它的價值,我希望在這個問題之前加上if語句,但顯然不是。

Input string was not in a correct format. 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System. 

FormatException: Input string was not in a correct format. 

Source Error: 

Line 56:    s.CustomerID = Convert.ToInt32(txtCustomerID.Text); 
Line 57:    if (lstIncident.SelectedIndex > -1) 
Line 58:     s.IncidentID = Convert.ToInt32(lstIncident.SelectedValue); 
Line 59:    if (rblResponse.SelectedIndex != -1) 
Line 60:    { 

的頁面

public partial class CustomerSurvey : System.Web.UI.Page 
{ 
private DataView IncidentsTable; 
protected void Page_Load(object sender, EventArgs e) 
{ 
    txtCustomerID.Focus(); 
} 
protected void btnGetIncidents_Click(object sender, EventArgs e) 
{ 

    IncidentsTable = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty); 
    IncidentsTable.RowFilter = "CustomerID = " + Convert.ToInt32(txtCustomerID.Text) + " AND DateClosed is Not Null"; 

    if (IncidentsTable.Count > 0) 
    { 
     this.DisplayIncidents(); 
     this.Enable(true); 
     lstIncident.Focus(); 
    } 
    else 
    { 
     lblIncidentCount.Text = "I'm sorry there are no incidents that can be surveyed at this time."; 
     this.Enable(false); 
    } 
} 

private void DisplayIncidents() 
{ 
    lstIncident.Items.Add(new ListItem("--Select an Incident--")); 
    for (int j = 0; j < IncidentsTable.Count; j++) 
    { 
     DataRowView row = IncidentsTable[j]; 
     Incident i = new Incident(); 
     i.IncidentID = Convert.ToInt32(row["IncidentID"]); 
     i.ProductCode = row["ProductCode"].ToString(); 
     i.DateClosed = Convert.ToDateTime(row["DateClosed"]); 
     i.Title = row["Title"].ToString(); 
     lstIncident.Items.Add(i.CustomerIncidentDisplay()); 
    } 
    lstIncident.SelectedIndex = 0; 
    lblIncidentCount.Text = ""; 
} 
protected void btnSubmit_Click(object sender, EventArgs e) 
{ 
    if (Page.IsValid) 
    { 
     Survey s = new Survey(); 
     s.CustomerID = Convert.ToInt32(txtCustomerID.Text); 
     if (lstIncident.SelectedIndex > -1) 
      s.IncidentID = Convert.ToInt32(lstIncident.SelectedValue); 
     if (rblResponse.SelectedIndex != -1) 
     { 
      s.ResponseTime = Convert.ToInt32(rblResponse.SelectedValue); 
     } 
     if (rblTechEfficiency.SelectedIndex != -1) 
     { 
      s.TechEfficiency = Convert.ToInt32(rblTechEfficiency.SelectedValue); 
     } 
     if (rblProblemResolution.SelectedIndex != -1) 
     { 
      s.Resolution = Convert.ToInt32(rblProblemResolution.SelectedValue); 
     } 
     s.Comments = txtComments.Text; 
     if (ckbContactMe.Checked) 
     { 
      s.Contact = true; 
      if (rdoContactEmail.Checked) 
      { 
       s.ContactBy = "Email"; 
      } 
      else 
      { 
       s.ContactBy = "Phone"; 
      } 
      Session.Add("Contact", true); 
     } 
     else 
     { 
      s.Contact = false; 
      Session.Add("Contact", false); 
     } 
     Response.Redirect("SurveyComplete.aspx"); 
    } 
} 
private void Enable(bool e) 
{ 
    lstIncident.Enabled = e; 
    lblIncidents.Enabled = e; 
    lblResponse.Enabled = e; 
    lblProblemResolution.Enabled = e; 
    lblTechEfficiency.Enabled = e; 
    lblComments.Enabled = e; 
    rdoContactEmail.Enabled = e; 
    rdoContactPhone.Enabled = e; 
    rblProblemResolution.Enabled = e; 
    rblResponse.Enabled = e; 
    rblTechEfficiency.Enabled = e; 
    lblContact.Enabled = e; 
    txtComments.Enabled = e; 
    ckbContactMe.Enabled = e; 
    btnSubmit.Enabled = e; 
} 

}

下面的代碼背後,是我的類中調用事件

public class Incident 
{ 
public int IncidentID { get; set; } 
public int CustomerID { get; set; } 
public string ProductCode { get; set; } 
public int TechID { get; set; } 
public string DateOpened { get; set; } 
public DateTime DateClosed { get; set; } 
public string Title { get; set; } 
public string Description { get; set; } 

    public string CustomerIncidentDisplay() 
    { 
    return "Incident for product " + ProductCode + " closed " + DateClosed.ToShortDateString() + 
     " (" + Title + ")"; 
    } 
} 
    public Incident() 
{ 

} 

這是一個母版頁,但這裏是

頁面
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server"> 
    <label>Enter your customer ID:</label>&nbsp;&nbsp; 
    <asp:TextBox ID="txtCustomerID" runat="server" Width="183px"></asp:TextBox>&nbsp;&nbsp; 
    <asp:Button ID="btnGetIncidents" runat="server" Text="Get Incidents" OnClick="btnGetIncidents_Click" ValidationGroup="CustomerID" /> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtCustomerID" ErrorMessage="You must enter a customer ID" ForeColor="Red" ValidationGroup="CustomerID" Display="Dynamic"></asp:RequiredFieldValidator> 
    <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="txtCustomerID" ErrorMessage="Customer ID must be a positive whole number" ForeColor="Red" Operator="DataTypeCheck" Type="Integer" ValidationGroup="CustomerID" Display="Dynamic"></asp:CompareValidator> 
    <br /> 
    <asp:Label ID="lblIncidentCount" runat="server" ForeColor="Red"></asp:Label> 
    <br /> 
    <asp:ListBox ID="lstIncident" runat="server" Width="622px" Enabled="False" AutoPostBack="True"></asp:ListBox> 
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TechSupportConnectionString %>" SelectCommand="SELECT [IncidentID], [CustomerID], [ProductCode], [TechID], [DateOpened], [DateClosed], [Title] FROM [Incidents] ORDER BY [DateClosed]"></asp:SqlDataSource> 
    <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="lstIncident" ErrorMessage="You must select an incident" ForeColor="Red" ValidationGroup="Incidents" Display="Dynamic" ></asp:RequiredFieldValidator> 
    <br /> 
    <asp:Label ID="lblIncidents" runat="server" Text="Please rate this incident by the following categories: " Enabled="False"></asp:Label><br /> 
    <table id="tblIncidents"> 
     <tr> 
      <td> <asp:Label ID="lblResponse" runat="server" Text="Response Time:" Enabled="False"></asp:Label></td> 
      <td><asp:RadioButtonList ID="rblResponse" runat="server" Font-Names="Arial" Font-Size="Small" RepeatDirection="Horizontal" Enabled="False"> 
       <asp:ListItem Value="1">Not Satisfied</asp:ListItem> 
       <asp:ListItem Value="2">Somewhat Satisfied</asp:ListItem> 
       <asp:ListItem Value="3">Satisfied</asp:ListItem> 
       <asp:ListItem Value="4">Completely Satisfied</asp:ListItem> 
      </asp:RadioButtonList></td> 
     </tr> 
     <tr> 
      <td><asp:Label ID="lblTechEfficiency" runat="server" Text="Technician Efficiency:" Enabled="False"></asp:Label></td> 
      <td><asp:RadioButtonList ID="rblTechEfficiency" runat="server" RepeatDirection="Horizontal" Font-Names="Arial" Font-Size="Small" Enabled="False"> 
       <asp:ListItem Value="1">Not Satisfied</asp:ListItem> 
       <asp:ListItem Value="2">Somewhat Satisfied</asp:ListItem> 
       <asp:ListItem Value="3">Satisfied</asp:ListItem> 
       <asp:ListItem Value="4">Completely Satisfied</asp:ListItem> 
      </asp:RadioButtonList></td> 
     </tr> 
     <tr> 
      <td><asp:Label ID="lblProblemResolution" runat="server" Text="Problem Resolution:" Enabled="False"></asp:Label></td> 
      <td><asp:RadioButtonList ID="rblProblemResolution" runat="server" RepeatDirection="Horizontal" Font-Names="Arial" Font-Size="Small" Enabled="False"> 
       <asp:ListItem Value="1">Not Satisfied</asp:ListItem> 
       <asp:ListItem Value="2">Somewhat Satisfied</asp:ListItem> 
       <asp:ListItem Value="3">Satisfied</asp:ListItem> 
       <asp:ListItem Value="4">Completely Satisfied</asp:ListItem> 
      </asp:RadioButtonList></td> 
     </tr> 
    </table> 
    <asp:Label ID="lblComments" runat="server" Text="Additional Comments:" Enabled="False"></asp:Label> 
    <asp:TextBox ID="txtComments" runat="server" Height="81px" Width="551px" Enabled="False" Rows="4" TextMode="MultiLine"></asp:TextBox> 
     <br /> 
     <br /> 

    <asp:CheckBox ID="ckbContactMe" runat="server" Enabled="False" /> 
    <asp:Label ID="lblContact" runat="server" Text="Please contact me to discuss this incident" Enabled="False"></asp:Label> 
    <br /> 
    <asp:RadioButton ID="rdoContactEmail" runat="server" GroupName="ContactBy" Enabled="False" Text="Contact By Email" /><br /> 
    <asp:RadioButton ID="rdoContactPhone" runat="server" Enabled="False" GroupName="ContactBy" Text="Contact By Phone" /> 

    <br /> 
    <br /> 
    <asp:Button ID="btnSubmit" runat="server" OnClick="btnSubmit_Click" Text="Submit" Enabled="False" ValidationGroup="Incidents" Width="123px" /> 
</asp:Content> 
+2

你在那個文本框中輸入什麼值? – 2013-05-01 19:39:25

+0

我想要+1這個問題,但它只是充滿了不重要的代碼和博覽會。也許如果你做了一個簡單的例子,有相同的問題.. – 2013-05-01 19:47:01

+0

-1壞的問題。請縮小問題範圍,也許我會刪除downvote。 – 2013-05-01 20:15:41

回答

1

在嘗試檢查頁面是否有效之前,請致電Page.Validate()

Page.Validate(); 
if (Page.IsValid) 
{ 
    ... 
}