How to pull SQL data in to a DatagridView.......

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • =?Utf-8?B?TWFyY2Vsbw==?=

    How to pull SQL data in to a DatagridView.......

    Hello,
    I am new to pulling data into VB and using DataGridViews.. ... so this
    might be a dumb question but I can not seem to understand how to make this
    work.

    What I need acomplished:
    Exec a select statement and out put the results to a DataGridView. The
    columns must be created on the fly as the query may return different fields.

    A small example I got to work to a List box. If you could convert this to a
    DGV for me it would be great ....
    Col1 - Full Name
    Col2 - Address
    oSQLcmd.Connect ion = oSQLconn
    oSQLcmd.Command Type = CommandType.Tex t
    oSQLcmd.Command Text = "SELECT FullNAME, Address FROM t_dssemp"

    strSQLopen = OpenSQLConn() ' Open Connection

    If oSQLconn.State = ConnectionState .Open Then
    oSQLdr = oSQLcmd.Execute Reader() 'executing the command and
    assigning it to connection

    While oSQLdr.Read()
    ListBox1.Items. Add(oSQLdr.Item ("FullName") & " " &
    oSQLdr.Item("Ad dress"))
    End While
    End If
    oSQLdr.Close()

    Thank you very much
    Marcelo

  • Cor Ligthert[MVP]

    #2
    Re: How to pull SQL data in to a DatagridView... ....

    Hi Marcelo,

    With the DataGridView a datatable is still working the best. You can use a
    datareader however are always busy to synchronise the indexes from the
    columns.

    Just my opinion

    Cor

    "Marcelo" <Marcelo@discus sions.microsoft .comschreef in bericht
    news:5EBDE2A4-F61B-4479-8292-38519AE7A810@mi crosoft.com...
    Hello,
    I am new to pulling data into VB and using DataGridViews.. ... so this
    might be a dumb question but I can not seem to understand how to make this
    work.
    >
    What I need acomplished:
    Exec a select statement and out put the results to a DataGridView. The
    columns must be created on the fly as the query may return different
    fields.
    >
    A small example I got to work to a List box. If you could convert this to
    a
    DGV for me it would be great ....
    Col1 - Full Name
    Col2 - Address
    oSQLcmd.Connect ion = oSQLconn
    oSQLcmd.Command Type = CommandType.Tex t
    oSQLcmd.Command Text = "SELECT FullNAME, Address FROM t_dssemp"
    >
    strSQLopen = OpenSQLConn() ' Open Connection
    >
    If oSQLconn.State = ConnectionState .Open Then
    oSQLdr = oSQLcmd.Execute Reader() 'executing the command and
    assigning it to connection
    >
    While oSQLdr.Read()
    ListBox1.Items. Add(oSQLdr.Item ("FullName") & " " &
    oSQLdr.Item("Ad dress"))
    End While
    End If
    oSQLdr.Close()
    >
    Thank you very much
    Marcelo
    >

    Comment

    • =?Utf-8?B?TWFyY2Vsbw==?=

      #3
      Re: How to pull SQL data in to a DatagridView... ....

      Cor,
      Sory I did not completly understand you commant but could you show me the
      code to take my list view and make it a Datagridview?

      thanks
      Marcelo

      "Cor Ligthert[MVP]" wrote:
      Hi Marcelo,
      >
      With the DataGridView a datatable is still working the best. You can use a
      datareader however are always busy to synchronise the indexes from the
      columns.
      >
      Just my opinion
      >
      Cor
      >
      "Marcelo" <Marcelo@discus sions.microsoft .comschreef in bericht
      news:5EBDE2A4-F61B-4479-8292-38519AE7A810@mi crosoft.com...
      Hello,
      I am new to pulling data into VB and using DataGridViews.. ... so this
      might be a dumb question but I can not seem to understand how to make this
      work.

      What I need acomplished:
      Exec a select statement and out put the results to a DataGridView. The
      columns must be created on the fly as the query may return different
      fields.

      A small example I got to work to a List box. If you could convert this to
      a
      DGV for me it would be great ....
      Col1 - Full Name
      Col2 - Address
      oSQLcmd.Connect ion = oSQLconn
      oSQLcmd.Command Type = CommandType.Tex t
      oSQLcmd.Command Text = "SELECT FullNAME, Address FROM t_dssemp"

      strSQLopen = OpenSQLConn() ' Open Connection

      If oSQLconn.State = ConnectionState .Open Then
      oSQLdr = oSQLcmd.Execute Reader() 'executing the command and
      assigning it to connection

      While oSQLdr.Read()
      ListBox1.Items. Add(oSQLdr.Item ("FullName") & " " &
      oSQLdr.Item("Ad dress"))
      End While
      End If
      oSQLdr.Close()

      Thank you very much
      Marcelo
      >
      >

      Comment

      • Cor Ligthert[MVP]

        #4
        Re: How to pull SQL data in to a DatagridView... ....

        Just the essential part, I have sent another message, however it seems to be
        somewhere in nowhere land and I have no copy from it. I am not using the
        Hungarian notation as you do, that is not adviced in dotNet. There it is
        Pascal notation with some exceptions.

        Instead of your datareader

        dim dt as datatable
        cmd.fill(dt)
        dgv.Datasource = dt

        I assume that you don't want to concatenate the name and adress now
        otherwise you can add a column.



        Cor

        "Marcelo" <Marcelo@discus sions.microsoft .comschreef in bericht
        news:4CA393D2-F462-433B-9E13-7F5CC2810696@mi crosoft.com...
        Cor,
        Sory I did not completly understand you commant but could you show me
        the
        code to take my list view and make it a Datagridview?
        >
        thanks
        Marcelo
        >
        "Cor Ligthert[MVP]" wrote:
        >
        >Hi Marcelo,
        >>
        >With the DataGridView a datatable is still working the best. You can use
        >a
        >datareader however are always busy to synchronise the indexes from the
        >columns.
        >>
        >Just my opinion
        >>
        >Cor
        >>
        >"Marcelo" <Marcelo@discus sions.microsoft .comschreef in bericht
        >news:5EBDE2A 4-F61B-4479-8292-38519AE7A810@mi crosoft.com...
        Hello,
        I am new to pulling data into VB and using DataGridViews.. ... so
        this
        might be a dumb question but I can not seem to understand how to make
        this
        work.
        >
        What I need acomplished:
        Exec a select statement and out put the results to a DataGridView.
        The
        columns must be created on the fly as the query may return different
        fields.
        >
        A small example I got to work to a List box. If you could convert this
        to
        a
        DGV for me it would be great ....
        Col1 - Full Name
        Col2 - Address
        oSQLcmd.Connect ion = oSQLconn
        oSQLcmd.Command Type = CommandType.Tex t
        oSQLcmd.Command Text = "SELECT FullNAME, Address FROM t_dssemp"
        >
        strSQLopen = OpenSQLConn() ' Open Connection
        >
        If oSQLconn.State = ConnectionState .Open Then
        oSQLdr = oSQLcmd.Execute Reader() 'executing the command and
        assigning it to connection
        >
        While oSQLdr.Read()
        ListBox1.Items. Add(oSQLdr.Item ("FullName") & " " &
        oSQLdr.Item("Ad dress"))
        End While
        End If
        oSQLdr.Close()
        >
        Thank you very much
        Marcelo
        >
        >>
        >>

        Comment

        • Cor Ligthert[MVP]

          #5
          Re: How to pull SQL data in to a DatagridView... ....

          I have pasted this in your code, by the way, did you know that the Hungarian
          method is rare used in dotNet.

          I have typed this on the fly, so watch typos etc.

          \\\
          cmd.Connection = conn
          cmd.CommandType = CommandType.Tex t
          cmd.CommandText = "SELECT FullNAME, Address FROM t_dssemp"
          conn.Open
          dim dt as new datatable
          Try
          cmd.Fill(dt)
          Catch ex as exception
          MessageBox.Show (ex.Tostring)
          Finally
          conn.Close
          End Try
          dt.Columns.Add( "Name",System.S tring,"FullName + " " + Address")
          Listbox1.DataSo urce = dt
          ListBox1.DataMe mber = "Name"
          ///



          "Marcelo" <Marcelo@discus sions.microsoft .comschreef in bericht
          news:4CA393D2-F462-433B-9E13-7F5CC2810696@mi crosoft.com...
          Cor,
          Sory I did not completly understand you commant but could you show me
          the
          code to take my list view and make it a Datagridview?
          >
          thanks
          Marcelo
          >
          "Cor Ligthert[MVP]" wrote:
          >
          >Hi Marcelo,
          >>
          >With the DataGridView a datatable is still working the best. You can use
          >a
          >datareader however are always busy to synchronise the indexes from the
          >columns.
          >>
          >Just my opinion
          >>
          >Cor
          >>
          >"Marcelo" <Marcelo@discus sions.microsoft .comschreef in bericht
          >news:5EBDE2A 4-F61B-4479-8292-38519AE7A810@mi crosoft.com...
          Hello,
          I am new to pulling data into VB and using DataGridViews.. ... so
          this
          might be a dumb question but I can not seem to understand how to make
          this
          work.
          >
          What I need acomplished:
          Exec a select statement and out put the results to a DataGridView.
          The
          columns must be created on the fly as the query may return different
          fields.
          >
          A small example I got to work to a List box. If you could convert this
          to
          a
          DGV for me it would be great ....
          Col1 - Full Name
          Col2 - Address
          oSQLcmd.Connect ion = oSQLconn
          oSQLcmd.Command Type = CommandType.Tex t
          oSQLcmd.Command Text = "SELECT FullNAME, Address FROM t_dssemp"
          >
          strSQLopen = OpenSQLConn() ' Open Connection
          >
          If oSQLconn.State = ConnectionState .Open Then
          oSQLdr = oSQLcmd.Execute Reader() 'executing the command and
          assigning it to connection
          >
          While oSQLdr.Read()
          ListBox1.Items. Add(oSQLdr.Item ("FullName") & " " &
          oSQLdr.Item("Ad dress"))
          End While
          End If
          oSQLdr.Close()
          >
          Thank you very much
          Marcelo
          >
          >>
          >>

          Comment

          • =?Utf-8?B?TWFyY2Vsbw==?=

            #6
            Re: How to pull SQL data in to a DatagridView... ....

            Cor,
            I will look into the Pascal notation. Right now i have all the code in
            Hungarian and I dont want to make mods till this works..... Below is the
            code you have me with 3 errors I ws unable to repair. Also you dumped the
            data into a listbox, I was looking for Datagridview... or shuold i wait till
            i se thi code running.

            Thanks again
            Marcelo

            oSQLcmd.Connect ion = oSQLconn
            oSQLcmd.Command Type = CommandType.Tex t
            oSQLcmd.Command Text = "SELECT nAME, password FROM t_dssemp"
            oSQLconn.Open()
            Dim dt As New DataTable
            Try
            oSQLcmd.Fill(dt ) 'Error - 'Fill' is not a member of
            system.data.sql client.sqlcomma nd
            Catch ex As Exception
            MessageBox.Show (ex.ToString)
            Finally
            oSQLconn.Close( )
            End Try
            dt.Columns.Add( "Name", System.String, "FullName") ' Error = 'Sting'
            is not a type in 'System' and can not be used as a expression
            Listbox1.DataSo urce = dt
            ListBox1.DataMe mber = "Name" ' Error - DataMember is not a member of
            system.windows. form.listbox
            "Cor Ligthert[MVP]" wrote:
            I have pasted this in your code, by the way, did you know that the Hungarian
            method is rare used in dotNet.
            >
            I have typed this on the fly, so watch typos etc.
            >
            \\\
            cmd.Connection = conn
            cmd.CommandType = CommandType.Tex t
            cmd.CommandText = "SELECT FullNAME, Address FROM t_dssemp"
            conn.Open
            dim dt as new datatable
            Try
            cmd.Fill(dt)
            Catch ex as exception
            MessageBox.Show (ex.Tostring)
            Finally
            conn.Close
            End Try
            dt.Columns.Add( "Name",System.S tring,"FullName + " " + Address")
            Listbox1.DataSo urce = dt
            ListBox1.DataMe mber = "Name"
            ///
            >
            >
            >
            "Marcelo" <Marcelo@discus sions.microsoft .comschreef in bericht
            news:4CA393D2-F462-433B-9E13-7F5CC2810696@mi crosoft.com...
            Cor,
            Sory I did not completly understand you commant but could you show me
            the
            code to take my list view and make it a Datagridview?

            thanks
            Marcelo

            "Cor Ligthert[MVP]" wrote:
            Hi Marcelo,
            >
            With the DataGridView a datatable is still working the best. You can use
            a
            datareader however are always busy to synchronise the indexes from the
            columns.
            >
            Just my opinion
            >
            Cor
            >
            "Marcelo" <Marcelo@discus sions.microsoft .comschreef in bericht
            news:5EBDE2A4-F61B-4479-8292-38519AE7A810@mi crosoft.com...
            Hello,
            I am new to pulling data into VB and using DataGridViews.. ... so
            this
            might be a dumb question but I can not seem to understand how to make
            this
            work.

            What I need acomplished:
            Exec a select statement and out put the results to a DataGridView.
            The
            columns must be created on the fly as the query may return different
            fields.

            A small example I got to work to a List box. If you could convert this
            to
            a
            DGV for me it would be great ....
            Col1 - Full Name
            Col2 - Address
            oSQLcmd.Connect ion = oSQLconn
            oSQLcmd.Command Type = CommandType.Tex t
            oSQLcmd.Command Text = "SELECT FullNAME, Address FROM t_dssemp"

            strSQLopen = OpenSQLConn() ' Open Connection

            If oSQLconn.State = ConnectionState .Open Then
            oSQLdr = oSQLcmd.Execute Reader() 'executing the command and
            assigning it to connection

            While oSQLdr.Read()
            ListBox1.Items. Add(oSQLdr.Item ("FullName") & " " &
            oSQLdr.Item("Ad dress"))
            End While
            End If
            oSQLdr.Close()

            Thank you very much
            Marcelo

            >
            >
            >
            >

            Comment

            • Cor Ligthert[MVP]

              #7
              Re: How to pull SQL data in to a DatagridView... ....

              Sorry Marcelo,

              I did it to much by hand.

              You need the dataadapter in it

              dim da is new SQLDataAdapter( oSQLcom, oSQLconn)
              da.Fill(dt)

              binding a datagridview is so easy
              dgrv.datasource = dt
              (it had to be displaymember, I don't know why I wrote datamember)

              I thought it was this
              dt.datacolumn.a dd("Name", GetType(System. String), "FullName")



              Not tested again,

              Cor

              "Marcelo" <Marcelo@discus sions.microsoft .comschreef in bericht
              news:A48E4F52-FA1D-4E90-B131-A7C0F7661525@mi crosoft.com...
              Cor,
              I will look into the Pascal notation. Right now i have all the code in
              Hungarian and I dont want to make mods till this works..... Below is the
              code you have me with 3 errors I ws unable to repair. Also you dumped the
              data into a listbox, I was looking for Datagridview... or shuold i wait
              till
              i se thi code running.
              >
              Thanks again
              Marcelo
              >
              oSQLcmd.Connect ion = oSQLconn
              oSQLcmd.Command Type = CommandType.Tex t
              oSQLcmd.Command Text = "SELECT nAME, password FROM t_dssemp"
              oSQLconn.Open()
              Dim dt As New DataTable
              Try
              oSQLcmd.Fill(dt ) 'Error - 'Fill' is not a member of
              system.data.sql client.sqlcomma nd
              Catch ex As Exception
              MessageBox.Show (ex.ToString)
              Finally
              oSQLconn.Close( )
              End Try
              dt.Columns.Add( "Name", System.String, "FullName") ' Error = 'Sting'
              is not a type in 'System' and can not be used as a expression
              Listbox1.DataSo urce = dt
              ListBox1.DataMe mber = "Name" ' Error - DataMember is not a member
              of
              system.windows. form.listbox
              "Cor Ligthert[MVP]" wrote:
              >
              >I have pasted this in your code, by the way, did you know that the
              >Hungarian
              >method is rare used in dotNet.
              >>
              >I have typed this on the fly, so watch typos etc.
              >>
              >\\\
              > cmd.Connection = conn
              > cmd.CommandType = CommandType.Tex t
              > cmd.CommandText = "SELECT FullNAME, Address FROM t_dssemp"
              > conn.Open
              > dim dt as new datatable
              > Try
              > cmd.Fill(dt)
              > Catch ex as exception
              > MessageBox.Show (ex.Tostring)
              > Finally
              > conn.Close
              > End Try
              > dt.Columns.Add( "Name",System.S tring,"FullName + " " + Address")
              > Listbox1.DataSo urce = dt
              > ListBox1.DataMe mber = "Name"
              > ///
              >>
              >>
              >>
              >"Marcelo" <Marcelo@discus sions.microsoft .comschreef in bericht
              >news:4CA393D 2-F462-433B-9E13-7F5CC2810696@mi crosoft.com...
              Cor,
              Sory I did not completly understand you commant but could you show me
              the
              code to take my list view and make it a Datagridview?
              >
              thanks
              Marcelo
              >
              "Cor Ligthert[MVP]" wrote:
              >
              >Hi Marcelo,
              >>
              >With the DataGridView a datatable is still working the best. You can
              >use
              >a
              >datareader however are always busy to synchronise the indexes from the
              >columns.
              >>
              >Just my opinion
              >>
              >Cor
              >>
              >"Marcelo" <Marcelo@discus sions.microsoft .comschreef in bericht
              >news:5EBDE2A 4-F61B-4479-8292-38519AE7A810@mi crosoft.com...
              Hello,
              I am new to pulling data into VB and using DataGridViews.. ... so
              this
              might be a dumb question but I can not seem to understand how to
              make
              this
              work.
              >
              What I need acomplished:
              Exec a select statement and out put the results to a
              DataGridView.
              The
              columns must be created on the fly as the query may return different
              fields.
              >
              A small example I got to work to a List box. If you could convert
              this
              to
              a
              DGV for me it would be great ....
              Col1 - Full Name
              Col2 - Address
              oSQLcmd.Connect ion = oSQLconn
              oSQLcmd.Command Type = CommandType.Tex t
              oSQLcmd.Command Text = "SELECT FullNAME, Address FROM
              t_dssemp"
              >
              strSQLopen = OpenSQLConn() ' Open Connection
              >
              If oSQLconn.State = ConnectionState .Open Then
              oSQLdr = oSQLcmd.Execute Reader() 'executing the command
              and
              assigning it to connection
              >
              While oSQLdr.Read()
              ListBox1.Items. Add(oSQLdr.Item ("FullName") & " " &
              oSQLdr.Item("Ad dress"))
              End While
              End If
              oSQLdr.Close()
              >
              Thank you very much
              Marcelo
              >
              >>
              >>
              >>
              >>

              Comment

              Working...