2012-01-05 6 views
2

これで、VB.Netで同様の名前のフォームコントロールをループする方法を学習して、このフォームの一部のユーザーから助けを得ました。私は現在、このアプリケーション全体をWebアプリケーションに変換する作業をしています。私は同じ位置に止まっています。誰でも私がここで間違っていることを理解するのを助けることができますか?私はそれが何かシンプルでなければならないことを知っているが、それは私を暗示している。私はVB.Netで動作するコードを添付しています:ここでASP.NET VB.NET同様の名前付きフォームコントロールをループする

Public Conn As ADODB.Connection 
Public Rs As ADODB.Recordset 
Public Sql As String 

Public Sub ConnOpenClose() 

    Conn = New ADODB.Connection 
    Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & databaseLocation & "" 
    Conn.Open() 
End Sub 


ConnOpenClose() 

Rs = New ADODB.Recordset 
Sql = "SELECT * FROM INVOICES WHERE CurrentStatus<>'Checked-Out' ORDER BY INVOICES.CurrentStatusNum, INVOICES.OpenedDate,OpenedTime;" 
SortCategory = "" 
Rs.Open(Sql, Conn) 
If Rs.EOF = False Then 
    Rs.Move(moveIndex) 
    For a = 1 To 20 
     If Rs.EOF = True Then 
      Me.Controls("lblStatus" & a).Text = "" 
      Me.Controls("lblStatus" & a).Visible = False 
      Me.Controls("lblInvoice" & a).Text = "" 
      Me.Controls("lblInvoice" & a).Visible = False 
      Me.Controls("lblSystemMakeModel" & a).Text = "" 
      Me.Controls("lblSystemMakeModel" & a).Visible = False 
      cmdScrollDown.Enabled = False 
     Else 
      If SortCategory <> Rs.Fields("CurrentStatus").Value Then 
       SortCategory = Rs.Fields("CurrentStatus").Value 
       tempInteger = a 
       If Rs.Fields("CurrentStatus").Value = "Checked In" Then 
        Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value & " - Total: " & Me.Controls("lblCheckedIn").Text 
       ElseIf Rs.Fields("CurrentStatus").Value = "On Bench" Then 
        Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value & " - Total: " & Me.Controls("lblOnBench").Text 
       ElseIf Rs.Fields("CurrentStatus").Value = "Update" Then 
        Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value & " - Total: " & Me.Controls("lblUpdate").Text 
       ElseIf Rs.Fields("CurrentStatus").Value = "Contact Us" Then 
        Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value & " - Total: " & Me.Controls("lblContactUs").Text 
       ElseIf Rs.Fields("CurrentStatus").Value = "Finished" Then 
        Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value & " - Total: " & Me.Controls("lblFinished").Text 
       ElseIf Rs.Fields("CurrentStatus").Value = "To Be Scrapped" Then 
        Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value & " - Total: " & Me.Controls("lblToBeScrapped").Text 
       Else 
        Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value 
       End If 
       Me.Controls("lblStatus" & a).Visible = True 
       Me.Controls("lblInvoice" & a).Text = "" 
       Me.Controls("lblInvoice" & a).Visible = False 
       Me.Controls("lblSystemMakeModel" & a).Text = "" 
       Me.Controls("lblSystemMakeModel" & a).Visible = False 
      Else 
       Me.Controls("lblStatus" & a).Text = Rs.Fields("CurrentStatus").Value 
       Me.Controls("lblStatus" & a).Visible = False 
       Me.Controls("lblInvoice" & a).Text = Rs.Fields("InvoiceID").Value & " - " & Rs.Fields("CustomerFName").Value & " " & Rs.Fields("CustomerLName").Value & " (" & DateDiff("d", Rs.Fields("OpenedDate").Value, Now()) & " days)" 
       Me.Controls("lblInvoice" & a).Visible = True 
       If Rs.Fields("LastStopSystem").Value = True Then 
        If Rs.Fields("ReturningSystem").Value = True Then 
         Me.Controls("lblSystemMakeModel" & a).Text = "LS - " & Rs.Fields("TypeOfSystem").Value & " - " & Rs.Fields("SystemMakeModel").Value & " RETURN" 
         Me.Controls("lblSystemMakeModel" & a).ForeColor = Color.Red 
        Else 
         Me.Controls("lblSystemMakeModel" & a).Text = "LS - " & Rs.Fields("TypeOfSystem").Value & " - " & Rs.Fields("SystemMakeModel").Value 
         Me.Controls("lblSystemMakeModel" & a).ForeColor = Color.DarkGreen 
        End If 
        Me.Controls("lblSystemMakeModel" & a).Visible = True 
        Me.Controls("lblSystemMakeModel" & a).Font = New Font(Me.Controls("lblSystemMakeModel" & a).Font, FontStyle.Bold) 
       Else 
        If Rs.Fields("ReturningSystem").Value = True Then 
         Me.Controls("lblSystemMakeModel" & a).Text = Rs.Fields("TypeOfSystem").Value & " - " & Rs.Fields("SystemMakeModel").Value & " RETURN" 
         Me.Controls("lblSystemMakeModel" & a).ForeColor = Color.Red 
        Else 
         Me.Controls("lblSystemMakeModel" & a).Text = Rs.Fields("TypeOfSystem").Value & " - " & Rs.Fields("SystemMakeModel").Value 
         Me.Controls("lblSystemMakeModel" & a).ForeColor = Color.Black 
        End If 
        Me.Controls("lblSystemMakeModel" & a).Visible = True 
        Me.Controls("lblSystemMakeModel" & a).Font = New Font(Me.Controls("lblSystemMakeModel" & a).Font, FontStyle.Regular) 
       End If 
       Rs.MoveNext() 
      End If 
     End If 
    Next 
    If Rs.EOF = False Then cmdScrollDown.Enabled = True 
End If 

は、私がこれまで持っているものであり、いくつかの点で、それが働いているが、私は仕事にフォーム要素をループを取得することはできません。私を助けてください。あなたが最初に必要なもの

Public Conn As OleDbConnection 
Public Rs As OleDbDataAdapter 
Public DS As New DataSet 
Public Sql As String 
Public oControl As Label 

Public Sub ConnOpenClose() 
    'If Conn.State = ConnectionState.Open Then Conn.Close() 
    Conn = New OleDbConnection 
    Conn.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;data source=C:\inetpub\CompTracking.mdb" 
    Conn.Open() 
End Sub 

Public Function FindAControl(ByVal controls As ControlCollection, ByVal toFind As String) As Control 

    If controls IsNot Nothing Then 
    For Each oControl As Control In controls 
     If oControl.ID.Equals(toFind, StringComparison.InvariantCultureIgnoreCase) Then 
     Return oControl 
     ElseIf oControl.HasControls Then 
     Dim oFoundControl As Control 

     oFoundControl = FindAControl(oControl.Controls, toFind) 
     If oFoundControl IsNot Nothing Then 
      Return oFoundControl 
     End If 
     End If 
    Next 
    End If 

    Return Nothing 
End Function 


Public Sub RefreshItems() 
    lblCheckedIn.Text = "0" 
    lblOnBench.Text = "0" 
    lblUpdate.Text = "0" 
    lblContactUs.Text = "0" 
    lblFinished.Text = "0" 
    lblToBeScrapped.Text = "0" 
    lblLastStop.Text = "0" 
    lblReturns.Text = "0" 

    moveIndex = 0 

    ConnOpenClose() 

    Sql = "SELECT * FROM Invoices WHERE CurrentStatus<>'Checked-Out' ORDER BY INVOICES.CurrentStatusNum, INVOICES.OpenedDate,OpenedTime" 
    Rs = New OleDbDataAdapter(Sql, Conn) 
    Rs.Fill(DS, "Invoices") 

    For i = 0 To DS.Tables("Invoices").Rows.Count - 1 
    If DS.Tables("Invoices").Rows(i).Item("CurrentStatus") = "Checked In" Then 
     lblCheckedIn.Text = Convert.ToInt16(lblCheckedIn.Text) + 1 
    ElseIf DS.Tables("Invoices").Rows(i).Item("CurrentStatus") = "On Bench" Then 
     lblOnBench.Text = Convert.ToInt16(lblOnBench.Text) + 1 
    ElseIf DS.Tables("Invoices").Rows(i).Item("CurrentStatus") = "Update" Then 
     lblUpdate.Text = Convert.ToInt16(lblUpdate.Text) + 1 
    ElseIf DS.Tables("Invoices").Rows(i).Item("CurrentStatus") = "Contact Us" Then 
     lblContactUs.Text = Convert.ToInt16(lblContactUs.Text) + 1 
    ElseIf DS.Tables("Invoices").Rows(i).Item("CurrentStatus") = "To Be Scrapped" Then 
     lblToBeScrapped.Text = Convert.ToInt16(lblToBeScrapped.Text) + 1 
    ElseIf DS.Tables("Invoices").Rows(i).Item("CurrentStatus") = "Finished" Then 
     lblFinished.Text = Convert.ToInt16(lblFinished.Text) + 1 
    End If 
    If DS.Tables("Invoices").Rows(i).Item("LastStopSystem") = True Then 
     lblLastStop.Text = Convert.ToInt16(lblLastStop.Text) + 1 
    End If 
    If DS.Tables("Invoices").Rows(i).Item("ReturningSystem") = True Then 
     lblReturns.Text = Convert.ToInt16(lblReturns.Text) + 1 
    End If 
    Next 

    lblTotalCheckedIn.Text = "Checked In: " & lblCheckedIn.Text 
    lblTotalOnBench.Text = "On Bench: " & lblOnBench.Text 
    lblTotalUpdate.Text = "Update: " & lblUpdate.Text 
    lblTotalContactUs.Text = "Contact Us: " & lblContactUs.Text 
    lblTotalToBeScrapped.Text = "To Be Scrapped: " & lblToBeScrapped.Text 
    lblTotalFinished.Text = "Finished: " & lblFinished.Text 
    lblTotalLastStop.Text = "LS Systems: " & lblLastStop.Text 
    lblTotalReturns.Text = "Returns: " & lblReturns.Text 

    ConnOpenClose() 

    Sql = "SELECT * FROM Invoices WHERE CurrentStatus<>'Checked-Out' ORDER BY INVOICES.CurrentStatusNum, INVOICES.OpenedDate,OpenedTime" 
    SortCategory = "" 
    Rs = New OleDbDataAdapter(Sql, Conn) 
    Rs.Fill(DS, "Invoices") 
    For a = 1 To 20 
    If ((a - 1) + moveIndex) > (DS.Tables("Invoices").Rows.Count - 1) Then 
     'exceeds total number of records, display blank fields 
     oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label) 
     If oControl IsNot Nothing Then oControl.Text = "" 
     If oControl IsNot Nothing Then oControl.Visible = False 
     oControl = TryCast(FindAControl(Me.Controls, "lblInvoice" & a), Label) 
     If oControl IsNot Nothing Then oControl.Text = "" 
     If oControl IsNot Nothing Then oControl.Visible = False 
     oControl = TryCast(FindAControl(Me.Controls, "lblSystemMakeModel" & a), Label) 
     If oControl IsNot Nothing Then oControl.Text = "" 
     If oControl IsNot Nothing Then oControl.Visible = False 
     cmdScrollDown.Enabled = False 
    Else 
     If sortCategory <> DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") Then 
     sortCategory = DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") 
     If DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") = "Checked In" Then 
      oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label) 
      If oControl IsNot Nothing Then oControl.Text = "Checked In - Total: " & lblTotalCheckedIn.Text 
      If oControl IsNot Nothing Then oControl.Visible = False 
     ElseIf DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") = "On Bench" Then 
      oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label) 
      If oControl IsNot Nothing Then oControl.Text = "On Bench - Total: " & lblTotalOnBench.Text 
      If oControl IsNot Nothing Then oControl.Visible = False 
     ElseIf DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") = "Update" Then 
      oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label) 
      If oControl IsNot Nothing Then oControl.Text = "Update - Total: " & lblTotalUpdate.Text 
      If oControl IsNot Nothing Then oControl.Visible = False 
     ElseIf DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") = "Contact Us" Then 
      oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label) 
      If oControl IsNot Nothing Then oControl.Text = "Contact Us - Total: " & lblTotalContactUs.Text 
      If oControl IsNot Nothing Then oControl.Visible = False 
     ElseIf DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") = "To Be Scrapped" Then 
      oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label) 
      If oControl IsNot Nothing Then oControl.Text = "To Be Scrapped - Total: " & lblTotalToBeScrapped.Text 
      If oControl IsNot Nothing Then oControl.Visible = False 
     ElseIf DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") = "Finished" Then 
      oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label) 
      If oControl IsNot Nothing Then oControl.Text = "Finished - Total: " & lblTotalFinished.Text 
      If oControl IsNot Nothing Then oControl.Visible = False 
     Else 
      oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label) 
      If oControl IsNot Nothing Then oControl.Text = DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") 
      If oControl IsNot Nothing Then oControl.Visible = False 
     End If 
     oControl = TryCast(FindAControl(Me.Controls, "lblInvoice" & a), Label) 
     If oControl IsNot Nothing Then oControl.Text = "" 
     If oControl IsNot Nothing Then oControl.Visible = False 
     oControl = TryCast(FindAControl(Me.Controls, "lblSystemMakeModel" & a), Label) 
     If oControl IsNot Nothing Then oControl.Text = "" 
     If oControl IsNot Nothing Then oControl.Visible = False 
     Else 
     oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label) 
     If oControl IsNot Nothing Then oControl.Text = DS.Tables("Invoices").Rows(a + moveIndex).Item("CurrentStatus") 
     If oControl IsNot Nothing Then oControl.Visible = False 
     oControl = TryCast(FindAControl(Me.Controls, "lblInvoice" & a), Label) 
     If oControl IsNot Nothing Then oControl.Text = DS.Tables("Invoices").Rows(a + moveIndex).Item("InvoiceID") & " - " & DS.Tables("Invoices").Rows(a + moveIndex).Item("CustomerFName") & " " & DS.Tables("Invoices").Rows(a + moveIndex).Item("CustomerLName") & " (" & DateDiff("d", DS.Tables("Invoices").Rows(a + moveIndex).Item("OpenedDate"), Now()) & " days)" 
     If oControl IsNot Nothing Then oControl.Visible = True 
     oControl = TryCast(FindAControl(Me.Controls, "lblSystemMakeModel" & a), Label) 
     If DS.Tables("Invoices").Rows(a + moveIndex).Item("LastStopSystem") = True Then 
      If DS.Tables("Invoices").Rows(a + moveIndex).Item("ReturningSystem") = True Then 
      If oControl IsNot Nothing Then oControl.Text = "" 
      If oControl IsNot Nothing Then oControl.ForeColor = Drawing.Color.Red 
      Else 
      If oControl IsNot Nothing Then oControl.Text = "" 
      If oControl IsNot Nothing Then oControl.ForeColor = Drawing.Color.DarkGreen 
      End If 
      If oControl IsNot Nothing Then oControl.Visible = True 
     Else 
      If DS.Tables("Invoices").Rows(a + moveIndex).Item("ReturningSystem") = True Then 
      If oControl IsNot Nothing Then oControl.Text = "" 
      If oControl IsNot Nothing Then oControl.ForeColor = Drawing.Color.Red 
      Else 
      If oControl IsNot Nothing Then oControl.Text = "" 
      If oControl IsNot Nothing Then oControl.ForeColor = Drawing.Color.Black 
      End If 
      If oControl IsNot Nothing Then oControl.Visible = True 
     End If 
     'Rs.movenext 
     End If 
    End If 
    Next 
    If (19 + moveIndex) > (DS.Tables("Invoices").Rows.Count - 1) Then cmdScrollDown.Enabled = True 
End Sub 
+0

ああでストアドプロシージャ"sp_SB-Invoices"という名前のデータベースは、次のSQL文です。SELECT * FROM請求書WHERE CurrentStatus <> 'チェックアウト' ORDER BY INVOICES.CurrentStatusNum、INVOICES.OpenedDate、OpenedTime with OWNERACCESS OPTION; – Russell

答えて

1

は、フォームのコントロールコレクション内の特定のコントロールを見つけるための再帰的な方法である:

Private Function FindAControl(ByVal controls As ControlCollection, ByVal toFind As String) As Control 

    If String.IsNullOrEmpty(toFind) Then 
     Throw New ArgumentException("toFind is null or empty") 
    End If 

    If controls IsNot Nothing Then 
     For Each oControl As Control In controls 
      If oControl IsNot Nothing Then 
       If Not String.IsNullOrEmpty(oControl.ID) AndAlso oControl.ID.Equals(toFind, StringComparison.InvariantCultureIgnoreCase) Then 
        Return oControl 
       ElseIf oControl.HasControls Then 
        Dim oFoundControl As Control 

        oFoundControl = FindAControl(oControl.Controls, toFind) 
        If oFoundControl IsNot Nothing Then 
         Return oFoundControl 
        End If 
       End If 
      End If 
     Next 
    End If 

    Return Nothing 
End Function 

あなたはこれを呼び出すことができます。

Dim oControl As Label 
oControl = TryCast(FindAControl(Me.Controls, "lblStatus" & a), Label) 
If oControl IsNot Nothing Then 
    oControl.Text = "" 
End If 
+0

あなたが書き込んだその関数についての警告が表示されています。oFoundControl.Controlsの下で、9行目に「Variable oFoundControl」という値が割り当てられる前に警告が表示されます。実行時の結果。 - コンパイルして実行すると4行目にエラーが表示され、NullReferenceExceptionはユーザコードによって処理されませんでした。どのようにこれを修正することができます。関数はこの問題で呼び出されています:TryCast(FindControls、 "lblStatus"&a)、Label).Text = "" – Russell

+1

申し訳ありませんが、コードに入力ミスがあります。私は答えを修正しましたが、下線付きの名前を 'oControl'に変更することができます。 –

+0

さて、squigglyラインはなくなりましたが、コンパイルして実行しているときに、別のエラーが発生しています。IfOControl.ID.Equals(toFind、StringComparison.InvariantCultureIgnoreCase)。 - エラーは "NullReferenceExceptionがユーザーコードによって処理されなかった"ということです。 – Russell

関連する問題