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. .NET (Core and Framework)
  4. Records do not show

Records do not show

Scheduled Pinned Locked Moved .NET (Core and Framework)
databasesql-serversysadminsecurity
10 Posts 2 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.
  • A Offline
    A Offline
    akosidandan
    wrote on last edited by
    #1

    Hello, I would like to ask why do I cannot display my records in my datagriview, the database I use is sql server 2005 with ".mdf" extension. Also if I use the wizard to add datasource I can add,delete records but when I run change the startup form to show the records via code connection the records I think is gone or something in my code is really wrong :confused: The code I use is here:

    Imports System.Data.SqlClient
    Public Class Form2

    Dim con As New SqlConnection
    
    Private Sub Form2\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Try
            con.ConnectionString = "Data Source=.\\SQLEXPRESS;" + \_
                                    "AttachDbFilename=|DataDirectory|\\Database1.mdf;" + \_
                                    "Integrated Security=True;User Instance=True"
            con.Open()
            con.Close()
    
            Dim da As New SqlDataAdapter
            Dim dt As New DataTable
            Dim ds As New DataSet
    
            ds.Tables.Add(dt)
            con.Open()
            da = New SqlDataAdapter("SELECT \* FROM phoneBookTable", con)
            da.Fill(dt)
            con.Close()
    
            DataGridView1.DataSource = dt.DefaultView
    
        Catch ex As Exception
            MsgBox(ex.ToString)
            con.Close()
        End Try
    End Sub
    

    End Class

    Thanks, Dan

    L 1 Reply Last reply
    0
    • A akosidandan

      Hello, I would like to ask why do I cannot display my records in my datagriview, the database I use is sql server 2005 with ".mdf" extension. Also if I use the wizard to add datasource I can add,delete records but when I run change the startup form to show the records via code connection the records I think is gone or something in my code is really wrong :confused: The code I use is here:

      Imports System.Data.SqlClient
      Public Class Form2

      Dim con As New SqlConnection
      
      Private Sub Form2\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
          Try
              con.ConnectionString = "Data Source=.\\SQLEXPRESS;" + \_
                                      "AttachDbFilename=|DataDirectory|\\Database1.mdf;" + \_
                                      "Integrated Security=True;User Instance=True"
              con.Open()
              con.Close()
      
              Dim da As New SqlDataAdapter
              Dim dt As New DataTable
              Dim ds As New DataSet
      
              ds.Tables.Add(dt)
              con.Open()
              da = New SqlDataAdapter("SELECT \* FROM phoneBookTable", con)
              da.Fill(dt)
              con.Close()
      
              DataGridView1.DataSource = dt.DefaultView
      
          Catch ex As Exception
              MsgBox(ex.ToString)
              con.Close()
          End Try
      End Sub
      

      End Class

      Thanks, Dan

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

      Try using following code instead of yours.

          void FillData()
          {
              // 1
              // Open connection
              using (SqlConnection c = new SqlConnection(DataConnectionString))
              {
                  c.Open();
                  // 2
                  // Create new DataAdapter
                  using (SqlDataAdapter a = new SqlDataAdapter("SELECT \* FROM phoneBookTable", c))
                  {
                      // 3
                      // Use DataAdapter to fill DataTable
                      DataTable t = new DataTable();
                      a.Fill(t);
      
                      // 4
                      // Render data onto the screen
                      // dataGridView1.DataSource = t; // <-- From your designer
                  }
              }
          }
      

      HTH

      Jinal Desai - LIVE Experience is mother of sage....

      A 1 Reply Last reply
      0
      • L Lost User

        Try using following code instead of yours.

            void FillData()
            {
                // 1
                // Open connection
                using (SqlConnection c = new SqlConnection(DataConnectionString))
                {
                    c.Open();
                    // 2
                    // Create new DataAdapter
                    using (SqlDataAdapter a = new SqlDataAdapter("SELECT \* FROM phoneBookTable", c))
                    {
                        // 3
                        // Use DataAdapter to fill DataTable
                        DataTable t = new DataTable();
                        a.Fill(t);
        
                        // 4
                        // Render data onto the screen
                        // dataGridView1.DataSource = t; // <-- From your designer
                    }
                }
            }
        

        HTH

        Jinal Desai - LIVE Experience is mother of sage....

        A Offline
        A Offline
        akosidandan
        wrote on last edited by
        #3

        Hello, Thanks for the help, but sorry to say I'm a bit confused about the code you have sent :( The code seems not working ing vb 2008 or maybe I am wrong :confused: Thanks over all in trying to help me. Thanks, Dan

        L 1 Reply Last reply
        0
        • A akosidandan

          Hello, Thanks for the help, but sorry to say I'm a bit confused about the code you have sent :( The code seems not working ing vb 2008 or maybe I am wrong :confused: Thanks over all in trying to help me. Thanks, Dan

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

          The code I have posted is in CSharp. Just use the idea, you do not need to follow the exact code. Just follow the steps in VB, in your way. So, that you will get if you are missing something. HTH

          Jinal Desai - LIVE Experience is mother of sage....

          A 1 Reply Last reply
          0
          • L Lost User

            The code I have posted is in CSharp. Just use the idea, you do not need to follow the exact code. Just follow the steps in VB, in your way. So, that you will get if you are missing something. HTH

            Jinal Desai - LIVE Experience is mother of sage....

            A Offline
            A Offline
            akosidandan
            wrote on last edited by
            #5

            Hello, Thank, I try to study it :) I have some additional question's if you dont mind. I notice that when I edit the database record and add data on it, it can show the data that I have add via editing but when I add data into the database using the wizard or form the records seems to be dissapear everytime I change the startup form. (info: form1 - the form that I use to connect via wizard form2 - form I use via coding connection ) I'm so confused, I can add data but when everytime I change startup form the records I have added seems gone :confused: , also everytime I add new records it just overwrite and it doesn't add new row :( Below here is my code again :

            Imports System.Data.SqlClient
            Public Class Form2

            Dim con As New SqlConnection
            
            Private Sub Form2\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                Try
                    con.ConnectionString = "Data Source=.\\SQLEXPRESS;" + \_
                                            "AttachDbFilename=|DataDirectory|\\Database1.mdf;" + \_
                                            "Integrated Security=True;User Instance=True"
                    con.Open()
                    con.Close()
            
                    Dim da As New SqlDataAdapter
                    Dim dt As New DataTable
                    Dim ds As New DataSet
            
                    ds.Tables.Add(dt)
                    con.Open()
                    da = New SqlDataAdapter("SELECT \* FROM phoneBookTable", con)
                    da.Fill(dt)
                    con.Close()
            
            
                    Dim newRows As DataRow = dt.NewRow
                    With newRows
                        .Item(0) = "2"
                    End With
                    dt.Rows.Add(newRows)
            
                    con.Open()
                    Dim cb As New SqlCommandBuilder(da)
                    da.Update(dt)
                    con.Close()
            
                    Dim newRows2 As DataRow = dt.NewRow
                    With newRows2
                        .Item(0) = "3"
                    End With
                    dt.Rows.Add(newRows2)
            
                    con.Open()
                    cb = New SqlCommandBuilder(da)
                    da.Update(dt)
                    con.Close()
            
                    DataGridView1.DataSource = dt.DefaultView
            
                Catch ex As Exception
                    MsgBox(ex.ToString)
                    con.Close()
                End Try
            
               
            
            End Sub
            

            End Class

            thanks again, Dan

            L 1 Reply Last reply
            0
            • A akosidandan

              Hello, Thank, I try to study it :) I have some additional question's if you dont mind. I notice that when I edit the database record and add data on it, it can show the data that I have add via editing but when I add data into the database using the wizard or form the records seems to be dissapear everytime I change the startup form. (info: form1 - the form that I use to connect via wizard form2 - form I use via coding connection ) I'm so confused, I can add data but when everytime I change startup form the records I have added seems gone :confused: , also everytime I add new records it just overwrite and it doesn't add new row :( Below here is my code again :

              Imports System.Data.SqlClient
              Public Class Form2

              Dim con As New SqlConnection
              
              Private Sub Form2\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
                  Try
                      con.ConnectionString = "Data Source=.\\SQLEXPRESS;" + \_
                                              "AttachDbFilename=|DataDirectory|\\Database1.mdf;" + \_
                                              "Integrated Security=True;User Instance=True"
                      con.Open()
                      con.Close()
              
                      Dim da As New SqlDataAdapter
                      Dim dt As New DataTable
                      Dim ds As New DataSet
              
                      ds.Tables.Add(dt)
                      con.Open()
                      da = New SqlDataAdapter("SELECT \* FROM phoneBookTable", con)
                      da.Fill(dt)
                      con.Close()
              
              
                      Dim newRows As DataRow = dt.NewRow
                      With newRows
                          .Item(0) = "2"
                      End With
                      dt.Rows.Add(newRows)
              
                      con.Open()
                      Dim cb As New SqlCommandBuilder(da)
                      da.Update(dt)
                      con.Close()
              
                      Dim newRows2 As DataRow = dt.NewRow
                      With newRows2
                          .Item(0) = "3"
                      End With
                      dt.Rows.Add(newRows2)
              
                      con.Open()
                      cb = New SqlCommandBuilder(da)
                      da.Update(dt)
                      con.Close()
              
                      DataGridView1.DataSource = dt.DefaultView
              
                  Catch ex As Exception
                      MsgBox(ex.ToString)
                      con.Close()
                  End Try
              
                 
              
              End Sub
              

              End Class

              thanks again, Dan

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

              I have found very nice link to perform, add, update and delete operations along with code. Add, Edit, and Delete in DataGridView with Paging[^] HTH

              Jinal Desai - LIVE Experience is mother of sage....

              A 1 Reply Last reply
              0
              • L Lost User

                I have found very nice link to perform, add, update and delete operations along with code. Add, Edit, and Delete in DataGridView with Paging[^] HTH

                Jinal Desai - LIVE Experience is mother of sage....

                A Offline
                A Offline
                akosidandan
                wrote on last edited by
                #7

                Hello, Thanks for the nice link. This is really helpfull. Thanks, Dan

                L 1 Reply Last reply
                0
                • A akosidandan

                  Hello, Thanks for the nice link. This is really helpfull. Thanks, Dan

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

                  My Pleasure.

                  Jinal Desai - LIVE Experience is mother of sage....

                  A 1 Reply Last reply
                  0
                  • L Lost User

                    My Pleasure.

                    Jinal Desai - LIVE Experience is mother of sage....

                    A Offline
                    A Offline
                    akosidandan
                    wrote on last edited by
                    #9

                    Hello, A 5 would be good to all of you helping newbie programmers Thanks, Dan

                    L 1 Reply Last reply
                    0
                    • A akosidandan

                      Hello, A 5 would be good to all of you helping newbie programmers Thanks, Dan

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

                      Thank you very much for appreciating.

                      Jinal Desai - LIVE Experience is mother of sage....

                      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