Results 1 to 9 of 9

Thread: [RESOLVED] Am I going about this all wrong?

  1. #1

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Resolved [RESOLVED] Am I going about this all wrong?

    I am moving systems from VB6 to VS2008. I am trying to do things in the "NET" way. On this particular form I had a FlexGrid. I entered an employee number and it went to the time card database and filled the grid with the employee's punches. In 2008 there is no Flexgrid. I have a data grid. I use a reader to get the employee information and another reader to get the time card data. What i don't get is how to fill the cells/rows in the data grid. Do I use the sql statement in the 2nd read as the data source for the datagrid? Or, am I just using VB 2008 to write VB6 code?

    Code:
    Dim reader As SqlDataReader
                Dim sql As New SqlClient.SqlCommand("SELECT * From EMPFILE WHERE [EMPNUM] = " & CInt(txtEmpNumber.Text))
                sql.Connection = cnxntech
                sql.CommandType = CommandType.Text
                reader = sql.ExecuteReader
                Using reader
                    If reader.HasRows Then
                        lblEmpName.Text = reader("NAMEF") & " " & reader("NAMEL")
                        lblEmpName.Refresh()
                        '
                        '------------------------------------------------------------
                        ' get time card data
                        '------------------------------------------------------------
                        '
                        Dim reader1 As SqlDataReader
                        Dim sql1 As New SqlClient.SqlCommand("SELECT * From TIMECARDS WHERE [EMPNUM] = " & CInt(txtEmpNumber.Text) & " ORDER BY [PAYDATE] DESC, [SHIFTDATE] DESC")
                        sql1.Connection = cnxntech
                        sql1.CommandType = CommandType.Text
                        reader1 = sql1.ExecuteReader
                        Using reader1
                            If reader1.HasRows Then
                                While reader1.Read
                                    '-------must figure out how to get to columns
                                End While
                            End If
                        End Using
                        sql1.Dispose()
                        reader1.Close()
                        '===============
                    End If
                End Using
    Any guidance will be appreciated as I will be using the same sort of processing in many places withing this app.
    Last edited by Pasvorto; Dec 21st, 2010 at 09:29 AM.
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  2. #2
    Fanatic Member amrita's Avatar
    Join Date
    Jan 2007
    Location
    Orissa,India
    Posts
    888

    Re: Am I going about this all wrong?

    Yes. Fill a datatable using dataadapter and assign the datatable to datasource of grid.
    thanks
    amrita

  3. #3
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Am I going about this all wrong?

    I would change this:
    Code:
    Dim sql1 As New SqlClient.SqlCommand("SELECT * From TIMECARDS WHERE [EMPNUM] = " & CInt(txtEmpNumber.Text) & " ORDER BY [PAYDATE] DESC, [SHIFTDATE] DESC")
    Like this
    Code:
    Dim empNum As Integer
    If Not Interger.TryParse(Me.txtEmpNumber.Txt,empNum) Then
        MessageBox.Show("Please enter a valid Employee Number")
        Exit Sub
    End IF
    
    Dim sql1 As New SqlClient.SqlCommand("SELECT * From TIMECARDS WHERE [EMPNUM] =  @EmpNum ORDER BY [PAYDATE] DESC, [SHIFTDATE] DESC")
    sql1.ParametersAddWithValue (@EmpNum) = @empNumber
    Then for the datagrid
    Code:
                           Using reader1
                            If reader1.HasRows Then
                                  Me.DataGrid1.DataSource = reader1
                            End If
                        End Using
                        sql1.Dispose()
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  4. #4

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: Am I going about this all wrong?

    Thanks. That's kind of what I thought. However, I am getting an error for this line:

    sql1.ParametersAddWithValue (@EmpNum) = @empNumber

    'ParametersAddWithValue' is not a member of 'System.data.sqlclient.sqlcommand.'
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  5. #5
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Am I going about this all wrong?

    I miss typed should be sql1.Parameters.AddWithValue


    Should be like this
    sql1.Parameters.AddWithValue("@EmpNum", empNumber)
    Last edited by GaryMazzone; Dec 21st, 2010 at 09:51 AM. Reason: Fixed add with values
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  6. #6

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: Am I going about this all wrong?

    Cool. It seems to like this:

    sql1.Parameters.AddWithValue("@EmpNum", reader("empNum"))

    I will use this as a template.
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  7. #7

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: Am I going about this all wrong?

    Not quite working yet. It is not populating the data grid. I stepped through the code to get the time card data and it is executing every step.
    Code:
                '---------------------------------------------------
                ' get employee data
                '---------------------------------------------------
                '
                cnxntech = New SqlClient.SqlConnection(cnxntechstring)
                cnxntech.Open()
                Dim reader As SqlDataReader
                Dim sql As New SqlClient.SqlCommand("SELECT * From EMPFILE WHERE [EMPNUM] = " & CInt(txtEmpNumber.Text))
                sql.Connection = cnxntech
                sql.CommandType = CommandType.Text
                reader = sql.ExecuteReader
                Using reader
                    If reader.HasRows Then
                        reader.Read()
                        Dim empnum As Integer
                        empnum = reader("empnum")
                        lblEmpName.Text = reader("NAMEF") & " " & reader("NAMEL")
                        lblEmpName.Refresh()
                        '
                        '------------------------------------------------------------
                        ' get time card data
                        '------------------------------------------------------------
                        '
                        cnxn = New SqlClient.SqlConnection(cnxnstring)
                        cnxn.Open()
                        Dim reader1 As SqlDataReader
                        Dim sql1 As New SqlClient.SqlCommand("SELECT * From TIMECARDS WHERE [EMPNUM] =  @EmpNum ORDER BY [PAYDATE] DESC, [SHIFTDATE] DESC")
                        sql1.Parameters.AddWithValue("@EmpNum", empnum)
                        sql1.Connection = cnxn
                        sql1.CommandType = CommandType.Text
                        reader1 = sql1.ExecuteReader
                        Using reader1
                            If reader1.HasRows Then
                                dgv1.DataSource = reader1
                                dgv1.Refresh()
                            End If
                        End Using
                        sql1.Dispose()
                        reader1.Close()
                        '===============
                        cnxn.Close()
                        cnxn.Dispose()
                    End If
                End Using
                sql.Dispose()
                cnxntech.Close()
                cnxntech.Dispose()
    It is executing every line in the get time card data routine. Just no data appears in the grid.
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

  8. #8
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: [RESOLVED] Am I going about this all wrong?

    Look at this thread:

    http://www.vbforums.com/showthread.php?t=522723


    I would also change that first select statement

    Dim sql As New SqlClient.SqlCommand("SELECT * From EMPFILE WHERE [EMPNUM] = " & CInt(txtEmpNumber.Text))

    to

    Code:
    Dim empNum as Integer
    If Not Integer.TryParse(txtEmpNUmber.Text,empNum) Then
         MessageBox.Show("Not a valid Employee Number")
         Me.txtEmpoyeeNumber.SetFocus
         Exit Sub
    End If
    
    Dim sql As New SqlClient.SqlCommand("SELECT * From EMPFILE WHERE [EMPNUM] =  @EmpNum"
    sql.Parameters.AddWithValue("@EmpNum",empNum)
    You should be using parameters where ever possible in you SQL queries. Also I would be using the .Net functions and not the VB6 functions (Integer.TryParse instead of CInt)
    Last edited by GaryMazzone; Dec 21st, 2010 at 12:20 PM.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  9. #9

    Thread Starter
    PowerPoster Pasvorto's Avatar
    Join Date
    Oct 2002
    Location
    Minnesota, USA
    Posts
    2,951

    Re: [RESOLVED] Am I going about this all wrong?

    Perfect! Thank so much.
    ===================================================
    If your question has been answered, mark the thread as [RESOLVED]

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