有关在asp.net c中说错格式的错误代码#

时间:2013-05-01 19:37:40

标签: c# asp.net

请有人帮助我,我一直收到这个错误,它告诉我它的格式不正确,但我把它列为整数。如果你能告诉我自己是个白痴,那么你将成为我真正的英雄。我肯定感谢任何人的帮助,是的,我只是在asp.net开始,所以请原谅我。

该页面是一项调查,可以从客户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;
}

}

这是我的班级名为Incident

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>

1 个答案:

答案 0 :(得分:1)

在您尝试检查网页是否有效之前调用Page.Validate()

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