Results 1 to 6 of 6

Thread: Record Count

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683

    Record Count

    I am converting a vb6 project to .net and half done. I am still trying to get a handle on Datasets and datareaders but so far so good. I have a form that when it loads does the following:
    1. Loads a combobox with all userids (Done with dataset from one table)
    2. Loads a listbox with all items that can be requested (done with a datareader from a second table)
    3. Count the number of records in a third table and display the next number or the number 1 if no records (THE PROBLEM)

    Here is the code so far (combo box code not here as it was done with the wizard)

    VB Code:
    1. Private Sub Request_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
    2.         DsSN1.Clear()
    3.         SqlDataAdapter1.Fill(DsSN1)
    4.         Dim cn1 As New SqlConnection
    5.         Dim da1 As New SqlDataAdapter
    6.  
    7.         'Populate the listbox step 2
    8.         cn1.ConnectionString = "workstation id=""S-KG-CFSCE-2098"";packet size=4096;integrated security=SSPI;data s" & _
    9.         "ource=""KG-CFSCE-2B"";persist security info=False;initial catalog=TrainingManageme" & _
    10.         "ntDB"
    11.         cn1.Open()
    12.         Dim cmdtext As String = "Select distinct Asset, AssetConfig from Equipment where Type = 'Equipment' order by asset asc"
    13.         da1.SelectCommand = New SqlCommand(cmdtext, cn1)
    14.         Dim dr As SqlDataReader = da1.SelectCommand.ExecuteReader
    15.         While dr.Read
    16.             If dr.IsDBNull(0) Then
    17.                 List1.Items.Add((vbNullString & " " & dr.GetString(1)))
    18.             End If
    19.             If dr.IsDBNull(1) Then
    20.                 List1.Items.Add((dr.GetString(0) & " " & vbNullString))
    21.             Else
    22.                 List1.Items.Add((dr.GetString(0) & " " & dr.GetString(1)))
    23.             End If
    24.  
    25.         End While
    26.         dr.Close()
    27.         cn1.Close()
    28.         List1.SelectedIndex = 0
    29.  
    30.         ''Get Highest Request Number and add one step 3
    31.         cn1.ConnectionString = "workstation id=""S-KG-CFSCE-2098"";packet size=4096;integrated security=SSPI;data s" & _
    32.                 "ource=""KG-CFSCE-2B"";persist security info=False;initial catalog=TrainingManagementDB"
    33.         cn1.Open()
    34.         Dim cmdtext2 As String = "Select Count(RequestNum) from Request"
    35.         da1.SelectCommand = New SqlCommand(cmdtext2, cn1)
    36.         Dim dr2 As SqlDataReader = da1.SelectCommand.ExecuteReader
    37.         While dr2.Read
    38.             If dr2.IsDBNull(0) Then
    39.                 txtNum.text = 1
    40.             Else
    41.                 txtNum.text =(Cint(dr.GetString(0)) +1)
    42.             End If
    43.         End While
    44.  
    45.  
    46.     End Sub

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683
    Just a couple of questions that go with this. Can you reuse connecitons, datasets, or datareaders or do you create a new one for each part?

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683
    Ok found the problem I was doing a datareader getstring with an integer and should of been doing a getvalue.

  4. #4
    PowerPoster
    Join Date
    Dec 2003
    Location
    Bristol, England (but heart is in Virginia)
    Posts
    2,949
    Originally posted by Beast777
    Just a couple of questions that go with this. Can you reuse connecitons, datasets, or datareaders or do you create a new one for each part?
    You have no need to close and reopen these objects if you are going to use them immediately. Connections take up a lot of resources so you would close them if they are not being used.

    Your posted code contained the following in the final loop

    "txtNum.text =(Cint(dr.GetString(0)) +1)"

    but dr has been closed by that time. Probably you meant dr2, but that may be a simple typo in the posting.
    Taxes
    The more I learn about VB.NET the more I like dBaseIII Plus

    The foregoing, whilst believed to be correct, is given without guarantee as to it's accuracy and entirely without recourse. You are required to decide for yourself whether or not it is suitable for your purposes and no liability for loss of any nature can be entertained.

  5. #5
    I'm about to be a PowerPoster! mendhak's Avatar
    Join Date
    Feb 2002
    Location
    Ulaan Baator GooGoo: Frog
    Posts
    38,170
    Would he be able to solve his problem if he used @@IDENTITY ?

  6. #6

    Thread Starter
    Fanatic Member
    Join Date
    Nov 2003
    Posts
    683
    ok heres the code thats working with all my open and closes. any help with my open and closes that I may be doing wrong, please feel free to help.

    VB Code:
    1. Private Sub Request_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load
    2.         DsSN1.Clear()
    3.         SqlDataAdapter1.Fill(DsSN1)
    4.         'Populate the listbox
    5.         SqlConnection1.ConnectionString = "workstation id=""S-KG-CFSCE-2098"";packet size=4096;integrated security=SSPI;data s" & _
    6.         "ource=""KG-CFSCE-2B"";persist security info=False;initial catalog=TrainingManageme" & _
    7.         "ntDB"
    8.         SqlConnection1.Open()
    9.         Dim cmdtext As String = "Select distinct Asset, AssetConfig from Equipment where Type = 'Equipment' order by asset asc"
    10.         da.SelectCommand = New SqlCommand(cmdtext, SqlConnection1)
    11.         Dim dr As SqlDataReader = da.SelectCommand.ExecuteReader
    12.  
    13.         While dr.Read
    14.             If dr.IsDBNull(0) Then
    15.                 List1.Items.Add((vbNullString & " " & dr.GetString(1)))
    16.             End If
    17.             If dr.IsDBNull(1) Then
    18.                 List1.Items.Add((dr.GetString(0) & " " & vbNullString))
    19.             Else
    20.                 List1.Items.Add((dr.GetString(0) & " " & dr.GetString(1)))
    21.             End If
    22.  
    23.         End While
    24.         dr.Close()
    25.         SqlConnection1.Close()
    26.         List1.SelectedIndex = 0
    27.  
    28.         ''Get Highest Request Number and add one
    29.         SqlConnection1.ConnectionString = "workstation id=""S-KG-CFSCE-2098"";packet size=4096;integrated security=SSPI;data s" & _
    30.                 "ource=""KG-CFSCE-2B"";persist security info=False;initial catalog=TrainingManagementDB"
    31.         SqlConnection1.Open()
    32.         Dim cmdtext2 As String = "Select Count(RequestNum) from Request"
    33.         da.SelectCommand = New SqlCommand(cmdtext2, SqlConnection1)
    34.         dr = da.SelectCommand.ExecuteReader
    35.        
    36.         While dr.Read
    37.             txtRequestNum.Text = dr.GetValue(0) + 1
    38.  
    39.         End While
    40.         SqlConnection1.Close()
    41.  
    42.     End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width