1

だから私は大学の授業のために小さなオンラインストアを作ろうとしていますが、私には理解できないこのエラーに遭遇しました。まず、注文ページのコードです。

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)  

        Dim strSessionID As String
        Dim strItem As String
        Dim strQuantity As String
        strSessionID = Session.SessionID
        strItem = Session("Item")
        strQuantity = Session("Quantity")
        tbxItem.Text = strItem
        tbxQuantity.Text = strQuantity

    End sub

    Protected Sub btnAddOrder_Click(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim strItem As String
        Dim strQuantity As String
        Dim strFullName As String
        Dim strAddressLine As String
        Dim intPhoneNumber As Integer
        Dim intCardNumber As Integer
        Dim strNameOnCard As String
        Dim strExpirationDate As String

        strItem = tbxItem.Text
        strQuantity = tbxQuantity.Text
        strFullName = tbxFullName.Text
        strAddressLine = tbxAddressLine.Text
        intPhoneNumber = tbxPhoneNumber.Text
        intCardNumber = tbxCardNumber.Text
        strNameOnCard = tbxNameOnCard.Text
        strExpirationDate = tbxExpirationDate.Text

        Dim strDatabaseNameAndLocation As String
        strDatabaseNameAndLocation = Server.MapPath("pcland.mdb")
        Dim strSQLCommand As String
        strSQLCommand = "INSERT INTO orders(Item, Quantity, FullName, AddressLine, PhoneNumber, CardNumber, NameOnCard, ExpirationDate) " & _
            "Values ('" & strItem & "','" & strQuantity & "','" & strFullName & "', '" & strAddressLine & "', '" & intPhoneNumber & "', '" & intCardNumber & "', '" & strNameOnCard & "', '" & strExpirationDate & "');"
        Dim objOleDbConnection As System.Data.OleDb.OleDbConnection
        objOleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & strDatabaseNameAndLocation)
        objOleDbConnection.Open()
        Dim objOleDbCommand As System.Data.OleDb.OleDbCommand
        objOleDbCommand = New System.Data.OleDb.OleDbCommand(strSQLCommand, objOleDbConnection)
        objOleDbCommand.ExecuteNonQuery()
        objOleDbConnection.Close()        
        strSQLCommand = "SELECT orders.* FROM orders ORDER BY orders.Price DESC;"
        objOleDbConnection = New System.Data.OleDb.OleDbConnection("Provider=Microsoft.Jet.Oledb.4.0; Data Source=" & strDatabaseNameAndLocation)       
        tbxItem.Text = ""
        tbxQuantity.Text = ""
        tbxFullName.Text = ""
        tbxAddressLine.Text = ""
        tbxPhoneNumber.Text = ""
        tbxCardNumber.Text = ""
        tbxNameOnCard.Text = ""
        tbxExpirationDate.Text = ""

        lblConfirmationMessage.Text = "Thank you for order."

    End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>PC land | Cart</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link href="#" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <form id="webform" runat="server">
            <div>
                <table border="0">
                <tr>
                    <td>Item:</td>
                    <td><asp:TextBox ID="tbxItem" ReadOnly="True" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Quantity:</td>
                    <td><asp:TextBox ID="tbxQuantity" ReadOnly="True" runat="server"></asp:TextBox></td>
                </tr>
                <tr>
                    <td>Full Name:</td>
                    <td><asp:TextBox ID="tbxFullName" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvFullName" runat="server" ControlToValidate="tbxFullName" 
                    Display="Dynamic">You must enter your full name</asp:RequiredFieldValidator></td>
                </tr>
                <tr>
                    <td>Address Line:</td>
                    <td><asp:TextBox ID="tbxAddressLine" TextMode="MultiLine" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvAddressLine" runat="server" ControlToValidate="tbxAddressLine" 
                    Display="Dynamic">You must enter your address</asp:RequiredFieldValidator></td>
                </tr>
                    <td>Phone Number:</td>
                    <td><asp:TextBox ID="tbxPhoneNumber" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvPhoneNumber" runat="server" ControlToValidate="tbxPhoneNumber" 
                    Display="Dynamic">You must enter your phone number (No spaces or dashs)</asp:RequiredFieldValidator></td>
                </tr>
                    <td>Card Number:</td>
                    <td><asp:TextBox ID="tbxCardNumber" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvCardNumber" runat="server" ControlToValidate="tbxCardNumber" 
                    Display="Dynamic">You must enter the card number (No spaces or dashs)</asp:RequiredFieldValidator></td>
                </tr>
                    <td>Name On Card:</td>
                    <td><asp:TextBox ID="tbxNameOnCard" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvNameOnCard" runat="server" ControlToValidate="tbxNameOnCard" 
                    Display="Dynamic">You must enter the full name that is on the card </asp:RequiredFieldValidator></td>
                </tr>
                    <td>Expiration Date:</td>
                    <td><asp:TextBox ID="tbxExpirationDate" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvExpirationDate" runat="server" ControlToValidate="tbxExpirationDate" 
                    Display="Dynamic">You must enter the expiration date (Example format: 01/02/90)</asp:RequiredFieldValidator></td>
                </tr>
                <tr>
                    <td><asp:Button id="btnAddRecord" runat="server" onclick="btnAddOrder_Click" postbackurl="confirm.aspx" text="Submit" /></td><br />
                </tr>
                </table>
                <asp:Label ID="lblConfirmationMessage" runat="server"></asp:Label><br />
            </div>
        </form>
        <div id="footer">Copyright &copy; 2012 PC Land - All Rights Reserved</div>
    </body>
</html>

これが確認ページのコードで、ユーザーが注文ページで選択した選択肢が表示されます。

<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        Dim tbxItem As TextBox
        Dim tbxQuantity As TextBox
        Dim tbxFullName As TextBox
        Dim tbxAddressLine As TextBox
        Dim tbxPhoneNumber As TextBox
        Dim tbxCardNumber As TextBox
        Dim tbxNameOnCard As TextBox
        Dim tbxExpirationDate As TextBox

        Dim strItem As String
        Dim strQuantity As String
        Dim strFullName As String
        Dim strAddressLine As String
        Dim intPhoneNumber As Integer
        Dim intCardNumber As Integer
        Dim strNameOnCard As String
        Dim strExpirationDate As String


        tbxItem = CType(Me.PreviousPage.FindControl("tbxItem"), TextBox)
        tbxQuantity = CType(Me.PreviousPage.FindControl("tbxQuantity"), TextBox)
        tbxFullName = CType(Me.PreviousPage.FindControl("tbxFullName"), TextBox)
        tbxAddressLine = CType(Me.PreviousPage.FindControl("tbxAddressLine"), TextBox)
        tbxPhoneNumber = CType(Me.PreviousPage.FindControl("tbxPhoneNumber"), TextBox)
        tbxCardNumber = CType(Me.PreviousPage.FindControl("tbxCardNumber"), TextBox)
        tbxNameOnCard = CType(Me.PreviousPage.FindControl("tbxNameOnCard"), TextBox)
        tbxExpirationDate = CType(Me.PreviousPage.FindControl("tbxExpirationDate"), TextBox)

        strItem = tbxItem.Text
        strQuantity = tbxQuantity.Text
        strFullName = tbxFullName.Text
        strAddressLine = tbxAddressLine.Text
        intPhoneNumber = tbxPhoneNumber.Text
        intCardNumber = tbxCardNumber.Text
        strNameOnCard = tbxNameOnCard.Text
        strExpirationDate = tbxExpirationDate.Text

        lblItem.Text = "You said your item was " & strItem
        lblQuantity.Text = "You said your quantity was " & strQuantity
        lblFullName.Text = "You said your full Name was " & strFullName
        lblAddressLine.Text = "You said your Address was " & strAddressLine
        lblPhoneNumber.Text = "You said your item was " & intPhoneNumber
        lblCardNumber.Text = "You said your item was " & intCardNumber
        lblNameOnCard.Text = "You said the name on your card was " & strNameOnCard
        lblExpirationDate.Text = "You said the expiration date was " & strExpirationDate

    End Sub

</script>
<html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title>PC land | Cart</title>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
        <link href="#" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <form id="webform" runat="server">
            <div>
                <p><asp:Label ID="lblItem" runat="server"></asp:Label></p>
                <p><asp:Label ID="lblQuantity" runat="server"></asp:Label></p>
                <p><asp:Label ID="lblFullName" runat="server"></asp:Label></p>
                <p><asp:Label ID="lblAddressLine" runat="server"></asp:Label></p>
                <p><asp:Label ID="lblPhoneNumber" runat="server"></asp:Label></p>
                <p><asp:Label ID="lblCardNumber" runat="server"></asp:Label></p>
                <p><asp:Label ID="lblNameOnCard" runat="server"></asp:Label></p>
                <p><asp:Label ID="lblExpirationDate" runat="server"></asp:Label></p>
            </div>
        </form>
        <div id="footer">Copyright &copy; 2012 PC Land - All Rights Reserved</div>
    </body>
</html>

私が得るエラーは次のとおりです。

Server Error in '/' Application.
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 37:         strFullName = tbxFullName.Text
Line 38:         strAddressLine = tbxAddressLine.Text
Line 39:         intPhoneNumber = tbxPhoneNumber.Text
Line 40:         intCardNumber = tbxCardNumber.Text
Line 41:         strNameOnCard = tbxNameOnCard.Text


Source File: D:\SOC Students Web Site\students\insc\insc209\project\confirm.aspx    Line: 39

Stack Trace:

[FormatException: Input string was not in a correct format.]
   Microsoft.VisualBasic.CompilerServices.Conversions.ParseDouble(String Value, NumberFormatInfo NumberFormat) +201
   Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) +66

[InvalidCastException: Conversion from string "" to type 'Integer' is not valid.]
   Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) +246
   ASP.students_insc_insc209_project_confirm_aspx.Page_Load(Object sender, EventArgs e) in D:\SOC Students Web Site\students\insc\insc209\project\confirm.aspx:39
   System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +14
   System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +35
   System.Web.UI.Control.OnLoad(EventArgs e) +99
   System.Web.UI.Control.LoadRecursive() +50
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +627

それはデータベースに情報を入れていますが、明らかに私はエラーを取り除きたいです。

4

1 に答える 1

1

通常、次のエラー メッセージが表示される場合:

入力文字列は、正しい形式ではありませんでした

あるタイプから別のタイプへのデータの変換に関係しており、変換は成功しませんでした。コードを見ると、intPhoneNumbertype として宣言されていますInteger:

Dim intPhoneNumber As Integer

VB.Net は自動型強制を提供するため、この行は電話番号テキスト ボックスの内容の文字列値を取得し、それを数値に変換しようとしています。

intPhoneNumber = tbxPhoneNumber.Text

最も可能性が高いのは、電話番号が のような形式で入力(123) 456-7890され、整数に変換できないことです。本当にユーザーに有効な数値を入力してもらいたい場合は、防御的なコーディング プラクティスとして検証とエラー チェックを提供する必要があります。

If IsNumeric(tbxPhoneNumber.Text) Then
    intPhoneNumber = CInt(tbxPhoneNumber.Text)
Else
    MessageBox("Error: Please enter phone as 9-digit number.")
End If

編集:RequiredFieldValidator電話番号に適用されているようです。それは良いことですが、それが入力されたことを検証するだけです。9 桁の数字であることも確認したい場合は、RegularExpressionValidator. 両方のバリデーターを、実際に検証しているものの種類に応じた適切なエラー メッセージで作成することをお勧めします。

<asp:RequiredFieldValidator ID="rfvPhoneNumber" runat="server"
    ControlToValidate="tbxPhoneNumber" 
    Display="Dynamic">
    You must enter your phone number (9 digits, no spaces or dashes)
</asp:RequiredFieldValidator>

<asp:RegularExpressionValidator" ID="revPhoneNumber" runat="server"
    ControlToValidate="tbxPhoneNumber"
    Display="Dynamic" ValidationExpression="\d{9}">
    Please enter 9-digit phone number without spaces or dashes
</asp:RegularExpressionValidator>
于 2012-04-21T03:15:23.717 に答える