2012-04-21 23 views
1

私はユニ・コースワークのための小さなオンラインストアを作ろうとしています。私はこのエラーを理解できませんでした。まずここに注文ページのコードがあります。今ここにSystem.FormatException:入力文字列の形式が正しくありません

<%@ 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 

はそれです情報をデータベースに入れても、明らかにエラーを取り除きたいのです。このエラーメッセージが表示されたら

答えて

1

は、一般的に:

入力文字列は、それがある型から別の型にデータを変換に関与している

正しい形式ではありませんでした、との変換は成功しませんでした。コードを見ると、あなたはタイプIntegerとしてintPhoneNumberを宣言した:

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> 
+0

テストは番号など123523. Iのだけのシンプルなラインだったとき、私は電話番号フィールドに入力された情報:私は、彼らが実際に検証しているものの種類、例えばに適切なエラーメッセージで両方のバリデータを作る推薦します利用可能な検証があることを知っていますが、私はこの問題をまず解決してから、何かを追加したいと思っています。 –

+0

私の例で示したように、 'CInt(tbxPhoneNumber.Text)'コードを追加してみてください。 – mellamokb

関連する問題