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. Visual Basic
  4. Problem with DataGridView

Problem with DataGridView

Scheduled Pinned Locked Moved Visual Basic
databasehelpcsharpvisual-studioannouncement
3 Posts 2 Posters 0 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.
  • D Offline
    D Offline
    drexler_kk
    wrote on last edited by
    #1

    Hi all,I'm having problem here with my code. I would like to Update the Database with my DataGridView1 when Click the Button1. But from the code below,I found that an Error have occured. Error from Visual Studio 2008= System.Data: Missing the DataColumn 'DO_No'in the DataTable 'Main_table' for the SourceColumn 'DO_No'. The following are the code I load the data from database to my DataGridView1:

    Private Sub MaintainDO_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ObjMyConn = New MySqlConnection(MyConnString)

        SQL = "SELECT \* FROM main\_table"
        Try
    
            ObjMyConn.Open()
            ObjAdapter = New MySqlDataAdapter(SQL, ObjMyConn)
            ObjComBuilder = New MySqlCommandBuilder(ObjAdapter)
            ds = New DataSet
            ObjAdapter.Fill(ds, "Main\_table")
            dt = ds.Tables("Main\_table")
            dt.Columns(0).ColumnName = "Date"
            dt.Columns(1).ColumnName = "DO No."
            dt.Columns(2).ColumnName = "Lorry No."
            dt.Columns(3).ColumnName = "Load From"
            dt.Columns(4).ColumnName = "To Site"
            dt.Columns(5).ColumnName = "Company Name"
            dt.Columns(6).ColumnName = "Item Description"
    
            DataGridView1.DataSource = dt.DataSet.Tables("Main\_table")
            DataGridView1.Refresh()
        Catch ex As Exception
            MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly)
        Finally
            ObjMyConn.Close()
        End Try
    End Sub
    

    The code I put inside my Button1:

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        SQL = "SELECT \* FROM main\_table"
        Try
            ObjAdapter.Update(ds.Tables("Main\_table")) '<< Here is how I used to update my database.
    
            ds.Tables("Main\_table").Clear()
            DataGridView1.DataSource = Nothing
            DataGridView1.Refresh()
    
            ObjMyConn.Open()
            ObjAdapter = New MySqlDataAdapter(SQL, ObjMyConn)
            ObjComBuilder = New MySqlCommandBuilder(ObjAdapter)
            ObjAdapter.Fill(ds, "Main\_table")
            dt = ds.Tables("Main\_table")
    
            dt.Columns(0).ColumnName = "Date"
            dt.Columns(1).ColumnName = "DO No."
            dt.Columns(2).ColumnName = "Lorry No."
            dt.Columns(3).ColumnName = "Load From"
            dt.Columns(4).ColumnName = "To Site"
    
    D 1 Reply Last reply
    0
    • D drexler_kk

      Hi all,I'm having problem here with my code. I would like to Update the Database with my DataGridView1 when Click the Button1. But from the code below,I found that an Error have occured. Error from Visual Studio 2008= System.Data: Missing the DataColumn 'DO_No'in the DataTable 'Main_table' for the SourceColumn 'DO_No'. The following are the code I load the data from database to my DataGridView1:

      Private Sub MaintainDO_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
      ObjMyConn = New MySqlConnection(MyConnString)

          SQL = "SELECT \* FROM main\_table"
          Try
      
              ObjMyConn.Open()
              ObjAdapter = New MySqlDataAdapter(SQL, ObjMyConn)
              ObjComBuilder = New MySqlCommandBuilder(ObjAdapter)
              ds = New DataSet
              ObjAdapter.Fill(ds, "Main\_table")
              dt = ds.Tables("Main\_table")
              dt.Columns(0).ColumnName = "Date"
              dt.Columns(1).ColumnName = "DO No."
              dt.Columns(2).ColumnName = "Lorry No."
              dt.Columns(3).ColumnName = "Load From"
              dt.Columns(4).ColumnName = "To Site"
              dt.Columns(5).ColumnName = "Company Name"
              dt.Columns(6).ColumnName = "Item Description"
      
              DataGridView1.DataSource = dt.DataSet.Tables("Main\_table")
              DataGridView1.Refresh()
          Catch ex As Exception
              MsgBox("Error: " & ex.Source & ": " & ex.Message, MsgBoxStyle.OkOnly)
          Finally
              ObjMyConn.Close()
          End Try
      End Sub
      

      The code I put inside my Button1:

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

          SQL = "SELECT \* FROM main\_table"
          Try
              ObjAdapter.Update(ds.Tables("Main\_table")) '<< Here is how I used to update my database.
      
              ds.Tables("Main\_table").Clear()
              DataGridView1.DataSource = Nothing
              DataGridView1.Refresh()
      
              ObjMyConn.Open()
              ObjAdapter = New MySqlDataAdapter(SQL, ObjMyConn)
              ObjComBuilder = New MySqlCommandBuilder(ObjAdapter)
              ObjAdapter.Fill(ds, "Main\_table")
              dt = ds.Tables("Main\_table")
      
              dt.Columns(0).ColumnName = "Date"
              dt.Columns(1).ColumnName = "DO No."
              dt.Columns(2).ColumnName = "Lorry No."
              dt.Columns(3).ColumnName = "Load From"
              dt.Columns(4).ColumnName = "To Site"
      
      D Offline
      D Offline
      Dave Kreskowiak
      wrote on last edited by
      #2

      OK. I think it's pretty obvious just by reading the error message. You're missing a column in your DataTable("Main_table") that you included in your SQL statement.

      A guide to posting questions on CodeProject[^]
      Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
           2006, 2007, 2008

      D 1 Reply Last reply
      0
      • D Dave Kreskowiak

        OK. I think it's pretty obvious just by reading the error message. You're missing a column in your DataTable("Main_table") that you included in your SQL statement.

        A guide to posting questions on CodeProject[^]
        Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
             2006, 2007, 2008

        D Offline
        D Offline
        drexler_kk
        wrote on last edited by
        #3

        Thanks Mr. Dave,I have solve the problem. I make a mistaken on the DataGridView1.Column1 header name. So I change it to the original name and it works now. Thanks alot to Mr. Dave and everyone reading here. Regards :) Drex

        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