2016-04-25 18 views
-1

我有一些问题,页面和我正在处理的代码。请参阅代码:以正确的格式输入字符串。 VB.net - 新的这个,所以需要帮助

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”,然后使用SQL中选择的记录的“id”删除连接。参见下表:使用

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

很抱歉,如果我错过了什么或者是缺乏信息的。任何帮助非常感谢。

+0

问题很明显'result.Value'不是整数。这是填充在哪里? –

+0

原始来自sql server的数字为6位数字,它是表中的“id”列,如果我正确理解您的问题。我可以看到,result.value不是一个整数,但我似乎无法将其转换为一个.. – BarryWhite

+0

对不起,这个问题并不完美,但为什么downvote?很明显地说,我是新的,阅读问题的规则,并确保我把所有东西。 请解释唐纳? – BarryWhite

回答

0

您没有向我们展示resultresult.Value的定义,因此这是一个有教养的猜测。数据库中的空值

尝试测试:

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

按照您所说的进行测试,因为我认为“”。“”为result.value。看起来这个问题是在我想要捕捉的价值观之前发生的。 我仍然必须注释掉你问的后半部分,因为它只是炸弹了,否则与相同的错误 从result.ClientID我得到“cphMain_result”这是正确的,但应该是一个6位数字,所以我是可能在某处丢失了转换? – BarryWhite

0
在VB

你需要检查你的变量,以确保它不是空的,空,&不信邪,不包含任何非数字字符。我也会使用CTYPE和修剪。只有当字符串包含数字值时,转换才会起作用。如果您在填充字符串时遇到问题,那么您需要检查代码的填充位置。我害怕我不知道JS。

+0

是的,谢谢你,我以为我要去香蕉!我得到了别人建议的价值。只要我拿起我需要的东西,就开始工作。感谢所有人的意见。 – BarryWhite

+0

无*值道歉 – BarryWhite