Column 'PlayerID' does not belong to table.

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 914 Views - Last Post: 03 July 2014 - 03:57 AM Rate Topic: -----

#1 Dien09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-June 14

Column 'PlayerID' does not belong to table.

Posted 01 July 2014 - 10:26 PM

Hi, for some reason, whenever I try adding information to my database table, I get this error which states that my column 'PlayerID' does not belong to table. That is not the case because I do have a column named 'PlayerID' in my database table. My friend is also having the same problem, and we both can not figure it out. I Googled it, but I got even more confused, as almost all of the explanation is too technical and includes material that we will not cover in this class. I know you have to call the column name exactly as it appears on your database, as well as you can not have spaces because supposedly VB Studio messes up. I would appreciate any help. I don't need the correct answer, outright, as I would like to understand this error. Thanks.

Public Class frmAdd

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim dt As New DataTable
        Dim connStr As String = "provider=microsoft.jet.oledb.4.0;" & "data source=player.mdb"
        Dim sqlStr As String = "select * from player"
        Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr)
        Dim i, Age As Integer
        Dim PlayerID, PlayerName, Position, TeamName, AgentName As String
        Dim rw As DataRow
        Dim commandbuilder As New OleDb.OleDbCommandBuilder(dataAdapter)
        'Add a new row to the player table'
        rw = dt.NewRow
        PlayerID = mtxtPlayerID.Text
        PlayerName = mtxtName.Text
        Age = mtxtAge.Text
        Position = mtxtPosition.Text
        TeamName = mtxtTeam.Text
        AgentName = mtxtAgent.Text
        rw.Item("PlayerID") = PlayerID
        rw.Item("PlayerName") = PlayerName
        rw.Item("Position") = Position
        rw.Item("Age") = Age
        rw.Item("Team") = TeamName
        rw.Item("Agent") = AgentName
        Try
            dt.Rows.Add(rw)
            'Update the Player table in the player.mdb database'
            i = dataAdapter.Update(dt)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        'Displays number of rows updated'
        mtxtPlayerID.Text = ""
        mtxtName.Text = ""
        mtxtAge.Text = ""
        mtxtPosition.Text = ""
        mtxtTeam.Text = ""
        mtxtAgent.Text = ""
        mtxtPlayerID.Focus()
        MessageBox.Show("# of rows updated= " & i)


    End Sub
End Class




Is This A Good Question/Topic? 0
  • +

Replies To: Column 'PlayerID' does not belong to table.

#2 maj3091  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 299
  • View blog
  • Posts: 1,768
  • Joined: 26-March 09

Re: Column 'PlayerID' does not belong to table.

Posted 01 July 2014 - 11:55 PM

How is PlayerID defined in your table? Is it an identity field? If so, you won't be able to add it in like that (albeit not sure about the error).

Can you show the schema of your table (Field Names, Type, etc).

Also, this is .Net, not VB6....

This post has been edited by maj3091: 01 July 2014 - 11:56 PM

Was This Post Helpful? 0
  • +
  • -

#3 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3319
  • View blog
  • Posts: 11,223
  • Joined: 12-December 12

Re: Column 'PlayerID' does not belong to table.

Posted 02 July 2014 - 03:25 AM

Moved to VB.NET forum.
Was This Post Helpful? 0
  • +
  • -

#4 Dien09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-June 14

Re: Column 'PlayerID' does not belong to table.

Posted 02 July 2014 - 09:03 AM

I am sorry guys, I was pretty sure that I posted this under the appropriate section. I can't attach an image, so I just copied my Access database info.
PlayerID Number
PlayerName Short Text
Position Short Text
Age Number
Team Short Text
Agent Short Text
Was This Post Helpful? 0
  • +
  • -

#5 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 443
  • View blog
  • Posts: 3,061
  • Joined: 12-January 10

Re: Column 'PlayerID' does not belong to table.

Posted 02 July 2014 - 11:44 AM

you have player id as a number in the db but as text in your method
Was This Post Helpful? 0
  • +
  • -

#6 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3319
  • View blog
  • Posts: 11,223
  • Joined: 12-December 12

Re: Column 'PlayerID' does not belong to table.

Posted 02 July 2014 - 11:55 AM

What is the exact error message and what line does it refer to in your posted code?

I would put the full path in here "data source=player.mdb" to make sure that it isn't looking at an earlier version of the database.

If the error is on line 20 then comment it out; if the error moves to line 21 then it isn't a problem with that particular field, but with the table itself.

And, of course, check that the field isn't named Player1D.
Was This Post Helpful? 0
  • +
  • -

#7 Dien09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-June 14

Re: Column 'PlayerID' does not belong to table.

Posted 02 July 2014 - 12:36 PM

Hi, this is the error message I am getting:

"An unhandled exception of type 'System.ArgumentException' occurred in System.Data.dll

Additional information: Column 'PlayerID' does not belong to table."

I also tried changing the PlayerID as an Integer, but same error. It is indeed named, PlayerID, not Player1D.
Was This Post Helpful? 0
  • +
  • -

#8 Dien09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-June 14

Re: Column 'PlayerID' does not belong to table.

Posted 02 July 2014 - 12:42 PM

View Postandrewsw, on 02 July 2014 - 11:55 AM, said:

What is the exact error message and what line does it refer to in your posted code?

I would put the full path in here "data source=player.mdb" to make sure that it isn't looking at an earlier version of the database.

If the error is on line 20 then comment it out; if the error moves to line 21 then it isn't a problem with that particular field, but with the table itself.

And, of course, check that the field isn't named Player1D.

I put the entire path there, and also commented out line 20. Now I am getting the same error for line 21. What problem do you think my table is giving me?
Was This Post Helpful? 0
  • +
  • -

#9 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3319
  • View blog
  • Posts: 11,223
  • Joined: 12-December 12

Re: Column 'PlayerID' does not belong to table.

Posted 02 July 2014 - 12:48 PM

You could loop through the columns to check what their column-names are. In C#:
foreach (DataColumn c in rw.Table.Columns)  //loop through the columns. 
{
    MessageBox.Show(c.ColumnName);
}

I would probably check Columns.Count firstly though.

Debugging would help, there's a tutorial linked in my signature below.

In VB:
        For Each col As DataColumn In rw.Table.Columns
            MessageBox.Show(col.ColumnName)
        Next

This post has been edited by andrewsw: 02 July 2014 - 12:48 PM

Was This Post Helpful? 0
  • +
  • -

#10 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3319
  • View blog
  • Posts: 11,223
  • Joined: 12-December 12

Re: Column 'PlayerID' does not belong to table.

Posted 02 July 2014 - 12:55 PM

View PostDien09, on 02 July 2014 - 07:42 PM, said:

What problem do you think my table is giving me?

Re-visiting your code, I reckon you haven't populated the DataTable, so the DataRow contains no columns.
Was This Post Helpful? 0
  • +
  • -

#11 Dien09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-June 14

Re: Column 'PlayerID' does not belong to table.

Posted 02 July 2014 - 12:59 PM

Hi bro, could you show me how to use the Columns.count function? Where do I put it?

This post has been edited by andrewsw: 02 July 2014 - 01:08 PM
Reason for edit:: Removed large previous quote, press REPLY

Was This Post Helpful? 0
  • +
  • -

#12 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3319
  • View blog
  • Posts: 11,223
  • Joined: 12-December 12

Re: Column 'PlayerID' does not belong to table.

Posted 02 July 2014 - 01:18 PM

You have a CommandBuilder but you do nothing with it. I doubt that you need it and you just need to Fill your DataTable.

Quote

Hi bro, could you show me how to use the Columns.count function? Where do I put it?

MessageBox.Show(dt.Columns.Count.ToString)

but if you don't know how to use a Count property, or where to put it, then I recommend that you halt this project and do some studying: knowing what a property is, and how and when you can display it, is essential knowledge.

This post has been edited by andrewsw: 02 July 2014 - 09:55 PM

Was This Post Helpful? 0
  • +
  • -

#13 Dien09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-June 14

Re: Column 'PlayerID' does not belong to table.

Posted 02 July 2014 - 01:23 PM

I know how to use a count property, but I have never used that specific one before.

This post has been edited by andrewsw: 02 July 2014 - 01:25 PM
Reason for edit:: Removed previous quote

Was This Post Helpful? 0
  • +
  • -

#14 andrewsw  Icon User is online

  • Fire giant boob nipple gun!
  • member icon

Reputation: 3319
  • View blog
  • Posts: 11,223
  • Joined: 12-December 12

Re: Column 'PlayerID' does not belong to table.

Posted 02 July 2014 - 01:28 PM

Please note that there is no need to keep quoting the entire previous post, there is a large Reply button further down the page.
Was This Post Helpful? 0
  • +
  • -

#15 Dien09  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 14-June 14

Re: Column 'PlayerID' does not belong to table.

Posted 02 July 2014 - 08:31 PM

Hey, I emailed my professor and she told me that I was missing a few stuff. I've added this on, however, I am getting a "Syntax error in INSERT INTO statement". Then it tells me that the number of rows updated is 0. Anyone has any idea as to why I am getting this error?

Public Class frmAdd
    Dim dt As New DataTable
    Dim rowindex As Integer = 0
    Dim connStr As String = "provider=microsoft.jet.oledb.4.0;" &
            "data source=C:\Users\Haris\Documents\player.mdb"
    Dim sqlStr As String = "select * from player"
    Private Sub FrmAdd_Load(sender As Object, e As EventArgs) Handles Me.Load
        dt.Clear()
        Dim dataApater As New OleDb.OleDbDataAdapter(sqlStr, connStr)
        dataApater.Fill(dt)
        dataApater.Dispose()

    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Dim dataAdapter As New OleDb.OleDbDataAdapter(sqlStr, connStr)
        Dim i, Age, PlayerID As Integer
        Dim PlayerName, Position, TeamName, AgentName As String


        Dim rw As DataRow
        Dim commandbuilder As New OleDb.OleDbCommandBuilder(dataAdapter)
        'Add a new row to the player table'
        rw = dt.NewRow
        PlayerID = mtxtPlayerID.Text
        PlayerName = mtxtName.Text
        Age = mtxtAge.Text
        Position = mtxtPosition.Text
        TeamName = mtxtTeam.Text
        AgentName = mtxtAgent.Text
        rw.Item("PlayerID") = PlayerID
        rw.Item("PlayerName") = PlayerName
        rw.Item("Position") = Position
        rw.Item("Age") = Age
        rw.Item("Team") = TeamName
        rw.Item("Agent") = AgentName
        Try
            dt.Rows.Add(rw)
            'Update the Player table in the player.mdb database'
            i = dataAdapter.Update(dt)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
        'Displays number of rows updated'
        mtxtPlayerID.Text = ""
        mtxtName.Text = ""
        mtxtAge.Text = ""
        mtxtPosition.Text = ""
        mtxtTeam.Text = ""
        mtxtAgent.Text = ""
        mtxtPlayerID.Focus()
        MessageBox.Show("# of rows updated= " & i)


    End Sub
End Class


Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2