从数据集中获取价值

时间:2013-11-09 06:25:24

标签: asp.net vb.net

我正在尝试从我的数据库中获取值并自动添加它以进行计算。我想我只是缺少了升技。请帮忙。感谢

SCRIPT     <%@ Import Namespace =“System.Data”%>     <%@ Import Namespace =“System.Data.Odbc”%>     <%@ Import Namespace =“Microsoft.Data.Odbc”%>     <%@ Page Language =“VB”AutoEventWireup =“false”CodeFile =“web.aspx.vb”Inherits =“_ default”%>

<HTML>

<HEAD>

<SCRIPT Language="VB" Runat="server">

Sub Page_Load(Source as object, e as EventArgs)

        Dim connectionstring As String = "DRIVER={MySQL ODBC 5.2 Unicode Driver}; SERVER=localhost; DATABASE=mydb; UID=andrew; PASSWORD=kh12345; OPTION=3"

        Dim connectme As OdbcConnection = New OdbcConnection(connectionstring)

        Dim sqlquery As String = "SELECT f_price from tbl_picture WHERE f_ID = 1"

        Dim ODBCdataadapter As OdbcDataAdapter = New OdbcDataAdapter(sqlquery, connectme)

        Dim ODBCdataset As DataSet = New DataSet()

        ODBCdataadapter.Fill(ODBCdataset,"f_price")

        amount.Text = ODBCdataset.Tables("f_price").Rows(0).Item(0)
End Sub

</SCRIPT >

</HEAD>

<BODY>

    <form id="form1" runat="server">

<h1>Table</h1>

        <table width="234" border="0
                    " cellspacing="0" cellpadding="0">
                          <tr>
                            <td width="234" height="30" align="left"><h3>&nbsp;Car Loan Calculator</h3></td>
                          </tr>
                          <tr>
                            <td width="234" height="160"><table width="230" border="0" align="center" cellpadding="0" cellspacing="0">
                                <tr>
                                  <td width="135" height="40">&nbsp;Total Amount</td>
                                  <td width="95" height="40">RM

        <asp:Label ID="amount" runat="server" Text="Label"></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                  <td width="135" height="40">&nbsp;Down Payment</td>
                                  <td width="95" height="40"><asp:DropDownList ID="Down" AutoPostBack="true" runat="server" OnSelectedIndexChanged="Down_SelectedIndexChanged">
                                      <asp:ListItem Value="10">10%</asp:ListItem>
                                      <asp:ListItem Value="20">20%</asp:ListItem>
                                      <asp:ListItem Selected="True" Value="30">30%</asp:ListItem>
                                      <asp:ListItem Value="40">40%</asp:ListItem>
                                      <asp:ListItem Value="50">50%</asp:ListItem>
                                  </asp:DropDownList></td>
                                </tr>
                                <tr>
                                  <td width="135" height="40">&nbsp;Interest Rate %</td>
                                  <td width="95" height="40"><asp:DropDownList ID="Interest" AutoPostBack="true" runat="server" OnSelectedIndexChanged="Interest_SelectedIndexChanged">
                                      <asp:ListItem Selected="True" Value="2">2%</asp:ListItem>
                                      <asp:ListItem Value="2.5">2.5%</asp:ListItem>
                                      <asp:ListItem Value="3.0">3.0%</asp:ListItem>
                                      <asp:ListItem Value="3.5">3.5%</asp:ListItem>
                                      <asp:ListItem Value="4.0">4.0%</asp:ListItem>
                                  </asp:DropDownList></td>
                                </tr>
                                <tr>
                                  <td width="135" height="40">&nbsp;Period Of Years</td>
                                  <td width="95" height="40"><asp:DropDownList AutoPostBack="true" ID="Year" runat="server" OnSelectedIndexChanged="Year_SelectedIndexChanged" >
                                      <asp:ListItem Value="1">1</asp:ListItem>
                                      <asp:ListItem Value="2">2</asp:ListItem>
                                      <asp:ListItem Value="3">3</asp:ListItem>
                                      <asp:ListItem Value="4">4</asp:ListItem>
                                      <asp:ListItem Value="5">5</asp:ListItem>
                                      <asp:ListItem Value="6">6</asp:ListItem>
                                      <asp:ListItem Value="7">7</asp:ListItem>
                                      <asp:ListItem Value="8">8</asp:ListItem>
                                      <asp:ListItem Selected="True" Value="9">9</asp:ListItem>
                                  </asp:DropDownList></td>
                                </tr>
                            </table></td>
                          </tr>
                          <tr>
                            <td width="234" height="40" align="center"><h4>Monthly :
                              <asp:Label ID="Payment" runat="server" Text="Label"></asp:Label>
                            </h4></td>
                          </tr>
                      </table>
    <p>

        &nbsp;</p>
    </form>

</BODY>

</HTML>

BACKSCRIPT

Partial Class _Default
    Inherits System.Web.UI.Page



    Private Sub CalculatePayment()
        Dim c_loan_amount, c_Payment

        c_loan_amount = amount.Text - (amount.Text * Down.Text * 0.01)

            c_Payment = ((c_loan_amount * (Interest.Text * 0.01) * Year.Text) + c_loan_amount) / (Year.Text * 12)

            Payment.Text = "RM " & Format(c_Payment, "#,##0.00")

    End Sub

    Sub Page_Load(ByVal Sender As Object, ByVal e As EventArgs) Handles Me.Load

        Call CalculatePayment()

    End Sub


    Protected Sub Year_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Year.SelectedIndexChanged
        Call CalculatePayment()
    End Sub

    Protected Sub Interest_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Interest.SelectedIndexChanged
        Call CalculatePayment()
    End Sub

    Protected Sub Down_SelectedIndexChanged(sender As Object, e As EventArgs) Handles Down.SelectedIndexChanged
        Call CalculatePayment()
    End Sub
End Class

错误是System.FormatException:输入字符串的格式不正确。

1 个答案:

答案 0 :(得分:0)

我可以假设您在本地浮点值(例如“1,13”)中收到了amount.Text的值,并且它不是VB的浮点数。 所以你必须要: a)将字符串格式化为float http://msdn.microsoft.com/en-us/library/dwhawy9k(v=vs.110).aspx b)更好的方法,解析amount.Text浮动,然后在计算中处理它。