簡體   English   中英

輸入正確格式的字符串。 VB.net - 新手需要幫助

[英]Input string in correct format. VB.net - New to this so help required

我在處理頁面和背后的代碼時遇到了一些問題。 請看代碼:

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

        Dim dtTable As DataTable = Nothing
        Dim intConnectionId As Integer = 0

        If  Page.IsPostBack Then

            ' Get user data from session
            With HttpContext.Current

                intConnectionId = CInt(result.Value)
            End With
        End If

        ' Remove connection
        modConnections.Delete(intConnectionId)

        ' Clear desks attahced to connection
        modDeskText.ClearUserDesk(intConnectionId)

        Using sqlCommand As New SqlCommand
            With sqlCommand
                .CommandText = "usp_connectsdetailedlist"
                .CommandType = CommandType.StoredProcedure
            End With

            Execute(sqlCommand, dtTable)
        End Using

        With Rep1
            .DataSource = dtTable.DefaultView
            .DataBind()
        End With

        If Not IsNothing(dtTable) Then
            dtTable.Dispose()
        End If

    End Sub

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

[InvalidCastException: Conversion from string "" to type 'Integer' is not valid.]
   Microsoft.VisualBasic.CompilerServices.Conversions.ToInteger(String Value) +238
   frmConnects2.Page_Load(Object sender, EventArgs e) in D:\Source Code\PTS\PTS Online .NET - Copy\PTS Online .NET\PTS Online .NET stu\frmConnects2.aspx.vb:17
   System.Web.UI.Control.OnLoad(EventArgs e) +95
   System.Web.UI.Control.LoadRecursive() +59
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +678

在線上:

intConnectionId = CInt(result.Value) 

我收到上述錯誤。 無論我做什么,我都無法將數據轉換為我需要工作的格式。 我現在明白這與右側代碼無關。

我的目標是從表中獲取數據,放入文本框 ( <input type="text" id="result" runat="server" /> ) id="result" 然后使用“id”在 SQL 中選擇的記錄的刪除連接。 見下表:

<ItemTemplate> 
                        <tr class="<%# ReturnValuesAsColor(Eval("online"), Eval("mobile"), Eval("server_proc"))%>">
                        <td><%# Eval("id").ToString%></td>
                        <td><%# Eval("user_id").ToString%></td>
                        <td><%# Eval("user_name").ToString%></td>
                        <td><%# Eval("workstation_name").ToString%></td>
                        <td><%# Eval("ip_address").ToString%></td>
                        <td><%# Eval("connect_date").ToString%></td>
                        <td><%# Eval("refresh_date").ToString%></td>
                        <td><%# Eval("app_ver").ToString%></td>
                        <td><%# Eval("app_date").ToString%></td>
                        <td><%# CBool(Eval("get_messages_flag")).ToString%></td>
                        <td><%# FixNumbers(Eval("message_type_flags"))%></td>
                        <td><%# Eval("online").ToString%></td>
                        <td><%# Eval("mobile").ToString%></td>
                        <td><%# Eval("group_name").ToString%></td>
                        <td><%# Eval("server_proc").ToString%></td>
                        <td><input type="submit" class="ui-button ui-widget ui-state-default ui-corner-all" value="delete connection" onclick="clearText(result); CopyId(<%# Eval("id").ToString%>);" /></td>
                        </tr>

                    </ItemTemplate>

使用的JS:

<script> 
                function CopyId(num) {
                    var txt = document.getElementById("<%= result.ClientID%>").value;
                    txt = num;
                    document.getElementById("<%= result.ClientID%>").value = txt;
                    }
 </script>

抱歉,如果我錯過了什么或缺少信息。 非常感謝任何幫助。

您尚未向我們展示resultresult.Value的定義,因此這是一個有根據的猜測。

嘗試測試數據庫中的空值:

If IsDbNull(result.Value) Then
    intConnectionId = 0 
Else
    intConnectionId = CInt(result.Value)
End If

在 VB 中,您需要檢查您的變量以確保它不是空的、空的和沒有的,並且不包含任何非數字字符。 我也會使用 CTYPE 和 Trim。 只有當字符串包含數字值時,轉換才會起作用。 如果您在填充字符串時遇到問題,那么您需要檢查填充它的代碼。 恐怕我不知道JS。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM