Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Use from dataReader to bind data to a data gridview

Use from dataReader to bind data to a data gridview

Scheduled Pinned Locked Moved C#
databasecomquestion
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • U Offline
    U Offline
    User 13293914
    wrote on last edited by
    #1

    Hi, i am loading my data with a dataReader but every time i am select another table, the new selected Rows will add below the previous table Rows. how can i empty it and load Only data of the new selected table. thank you in advance. here is my code: OleDBConnectio con=new OleDBConnection(conectionString); con.Open(); OleDBDataCommand com=new OleDBCammand(“Select *From[“+txtBox1.Text+”]”,con); OleDBDataReader dr; dr=com.ExecuteReader(); While(dr.read()) { Dgv1.Rows.Add(dr[“ID”],dr.[“name”].ToString()); } dr.Close(); con.Close(); i should mention that i don't want use from data source to bind my database to DGV, because i want to add row in my DGV programmatically. thanks a lot.

    L Richard DeemingR 2 Replies Last reply
    0
    • U User 13293914

      Hi, i am loading my data with a dataReader but every time i am select another table, the new selected Rows will add below the previous table Rows. how can i empty it and load Only data of the new selected table. thank you in advance. here is my code: OleDBConnectio con=new OleDBConnection(conectionString); con.Open(); OleDBDataCommand com=new OleDBCammand(“Select *From[“+txtBox1.Text+”]”,con); OleDBDataReader dr; dr=com.ExecuteReader(); While(dr.read()) { Dgv1.Rows.Add(dr[“ID”],dr.[“name”].ToString()); } dr.Close(); con.Close(); i should mention that i don't want use from data source to bind my database to DGV, because i want to add row in my DGV programmatically. thanks a lot.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Dgv1.Rows.Clear();

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

      U 1 Reply Last reply
      0
      • L Lost User

        Dgv1.Rows.Clear();

        Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]

        U Offline
        U Offline
        User 13293914
        wrote on last edited by
        #3

        Hi, thank you legitimate programmer. thanks a million for your help:thumbsup::thumbsup::thumbsup::rose:

        1 Reply Last reply
        0
        • U User 13293914

          Hi, i am loading my data with a dataReader but every time i am select another table, the new selected Rows will add below the previous table Rows. how can i empty it and load Only data of the new selected table. thank you in advance. here is my code: OleDBConnectio con=new OleDBConnection(conectionString); con.Open(); OleDBDataCommand com=new OleDBCammand(“Select *From[“+txtBox1.Text+”]”,con); OleDBDataReader dr; dr=com.ExecuteReader(); While(dr.read()) { Dgv1.Rows.Add(dr[“ID”],dr.[“name”].ToString()); } dr.Close(); con.Close(); i should mention that i don't want use from data source to bind my database to DGV, because i want to add row in my DGV programmatically. thanks a lot.

          Richard DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          Member 13325846 wrote:

          new OleDBCammand(“Select *From[“+txtBox1.Text+”]”,con)

          That code is vulnerable to SQL Injection[^]. Unfortunately, there's no easy way to fix it, since you can't use a parameter as a table name. Rather than letting the user type in any table name, it would be better to give them a drop-down list of tables to choose from. After all, not every table in your database is going to have Id and Name columns. It would also be better to only load the specific columns you need, rather than using SELECT * FROM ...


          "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          U 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            Member 13325846 wrote:

            new OleDBCammand(“Select *From[“+txtBox1.Text+”]”,con)

            That code is vulnerable to SQL Injection[^]. Unfortunately, there's no easy way to fix it, since you can't use a parameter as a table name. Rather than letting the user type in any table name, it would be better to give them a drop-down list of tables to choose from. After all, not every table in your database is going to have Id and Name columns. It would also be better to only load the specific columns you need, rather than using SELECT * FROM ...


            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            U Offline
            U Offline
            User 13293914
            wrote on last edited by
            #5

            Hi, Thank You For your care. Yes your right and this was just an example and I am using from a cboBox. Thanks a million:rose:

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups