lesson 13: dataAdapter.UpdateCommand = conn.CreateCommand() - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

lesson 13: dataAdapter.UpdateCommand = conn.CreateCommand()

tyg1578tyg1578 Posts: 5Member
I have been trying out the ADO.net lesson 13 (http://www.programmersheaven.com/2/Les_VBNET_13_p1) on this site and have a problem I can't seem to overcome.

I adapted the solution to the "food for thought" exercise #5. Here is the link for the source code:
http://www.programmersheaven.com/articles/faraz/vbnet/lesson14/DBAppWithBothTables_Q5.zip

I adapted the solution from SQLServer to Access OLEDB. I am using my own DB instead of the programmersHeaven db (which I couldn't find on this site...), so i changed names of variables and tables, but I have basically kept the same structure intact.

The problem I am having is with InitializeCommands() which is meant to initialize the insert, update and delete SQL commands. When I run the program I get a NullReferenceException unhandled error. (Reference to object not established as an instance of an object). The line of code that throws the error is:

dataAdapterEvento.UpdateCommand = conn.CreateCommand()

(although I get the same error with DeleteCommand or InsertCommand too)

After I got the error I moved the call to InitializeCommands() to btnEditRecord_Click I am able to connect to the db, fill the dataset and navigate between records as I should be able to...so I would think that there is not a Null reference here...conn and dataAdapter are good because otherwise I wouldn't see any data. (both of them are btw are declared like this:

Public Class ADOForm
Inherits System.Windows.Forms.Form
Private conn As OleDb.OleDbConnection
Private dataAdapterEvento As OleDb.OleDbDataAdapter

Any ideas out there as to why this doesn't work?
Thanks!




Comments

  • PavlinIIPavlinII Posts: 404Member
    : I have been trying out the ADO.net lesson 13 (http://www.programmersheaven.com/2/Les_VBNET_13_p1) on this site and have a problem I can't seem to overcome.
    :
    : I adapted the solution to the "food for thought" exercise #5. Here is the link for the source code:
    : http://www.programmersheaven.com/articles/faraz/vbnet/lesson14/DBAppWithBothTables_Q5.zip
    :
    : I adapted the solution from SQLServer to Access OLEDB. I am using my own DB instead of the programmersHeaven db (which I couldn't find on this site...), so i changed names of variables and tables, but I have basically kept the same structure intact.
    :
    : The problem I am having is with InitializeCommands() which is meant to initialize the insert, update and delete SQL commands. When I run the program I get a NullReferenceException unhandled error. (Reference to object not established as an instance of an object). The line of code that throws the error is:
    :
    : dataAdapterEvento.UpdateCommand = conn.CreateCommand()
    :
    : (although I get the same error with DeleteCommand or InsertCommand too)
    :
    : After I got the error I moved the call to InitializeCommands() to btnEditRecord_Click I am able to connect to the db, fill the dataset and navigate between records as I should be able to...so I would think that there is not a Null reference here...conn and dataAdapter are good because otherwise I wouldn't see any data. (both of them are btw are declared like this:
    :
    : Public Class ADOForm
    : Inherits System.Windows.Forms.Form
    : Private conn As OleDb.OleDbConnection
    : Private dataAdapterEvento As OleDb.OleDbDataAdapter
    :
    : Any ideas out there as to why this doesn't work?
    : Thanks!
    :
    :
    :
    :
    :
    Hi,
    it seems that your error occurs becouse conn object was not created, and conn.CreateCommand does not have "conn"

    I guess you've forgot to do
    [code]conn = New SqlConnection[/code]
    before you call
    [code]conn.CreateCommand[/code]

    Take a look into that example again, in btnLoadTable_Click is
    [code]conn = NewSqlConnection[/code]
    called before InitializeComponents is called

    btw, I would prefer creating command object in this way:
    [code] dataAdapterArticle.InsertCommand = New SqlCommand("INSERT INTO article " + _
    "(artId, title, topic, authorId, lines, dateOfPublishing) " + _
    "VALUES(@artId, @title, @topic, @authorId, @lines, @dateOfPublishing)", Conn)[/code]

    Hope this helps


    [blue][b][italic][size=4]P[/size]avlin [size=4]II[/italic][/size][/b][/blue]

    [purple]Don't take life too seriously anyway you won't escape alive from it![/purple]


  • tyg1578tyg1578 Posts: 5Member
    : : I have been trying out the ADO.net lesson 13 (http://www.programmersheaven.com/2/Les_VBNET_13_p1) on this site and have a problem I can't seem to overcome.
    : :
    : : I adapted the solution to the "food for thought" exercise #5. Here is the link for the source code:
    : : http://www.programmersheaven.com/articles/faraz/vbnet/lesson14/DBAppWithBothTables_Q5.zip
    : :
    : : I adapted the solution from SQLServer to Access OLEDB. I am using my own DB instead of the programmersHeaven db (which I couldn't find on this site...), so i changed names of variables and tables, but I have basically kept the same structure intact.
    : :
    : : The problem I am having is with InitializeCommands() which is meant to initialize the insert, update and delete SQL commands. When I run the program I get a NullReferenceException unhandled error. (Reference to object not established as an instance of an object). The line of code that throws the error is:
    : :
    : : dataAdapterEvento.UpdateCommand = conn.CreateCommand()
    : :
    : : (although I get the same error with DeleteCommand or InsertCommand too)
    : :
    : : After I got the error I moved the call to InitializeCommands() to btnEditRecord_Click I am able to connect to the db, fill the dataset and navigate between records as I should be able to...so I would think that there is not a Null reference here...conn and dataAdapter are good because otherwise I wouldn't see any data. (both of them are btw are declared like this:
    : :
    : : Public Class ADOForm
    : : Inherits System.Windows.Forms.Form
    : : Private conn As OleDb.OleDbConnection
    : : Private dataAdapterEvento As OleDb.OleDbDataAdapter
    : :
    : : Any ideas out there as to why this doesn't work?
    : : Thanks!
    : :
    : :
    : :
    : :
    : :
    : Hi,
    : it seems that your error occurs becouse conn object was not created, and conn.CreateCommand does not have "conn"
    :
    : I guess you've forgot to do
    : [code]conn = New SqlConnection[/code]
    : before you call
    : [code]conn.CreateCommand[/code]
    :
    : Take a look into that example again, in btnLoadTable_Click is
    : [code]conn = NewSqlConnection[/code]
    : called before InitializeComponents is called
    :
    : btw, I would prefer creating command object in this way:
    : [code] dataAdapterArticle.InsertCommand = New SqlCommand("INSERT INTO article " + _
    : "(artId, title, topic, authorId, lines, dateOfPublishing) " + _
    : "VALUES(@artId, @title, @topic, @authorId, @lines, @dateOfPublishing)", Conn)[/code]
    :
    : Hope this helps
    :
    :
    : [blue][b][italic][size=4]P[/size]avlin [size=4]II[/italic][/size][/b][/blue]
    :
    : [purple]Don't take life too seriously anyway you won't escape alive from it![/purple]
    :
    :
    :
    Thanks for the suggestion...

    I verified that conn was declared before it was used and I think that it was in btnLoadTable_Click :

    [code] Dim conn As New OleDb.OleDbConnection(connectionString)
    [/code]

    I checked too to make sure that conn is in the class declaration and it is:

    [code]
    Public Class ADOForm2
    Inherits System.Windows.Forms.Form
    Private conn As OleDb.OleDbConnection
    [/code]

    So, on a hunch, I moved the SQL commands to btnLoadTable_Click and they work just fine! I can view, update, insert and delete all records. Isn't that odd? So basically, I just moved the contents of InitializeCommands into btnLoadTable_Click and everything works...Although I'm really not sure why, but since it works, I'm not complaining.



  • PavlinIIPavlinII Posts: 404Member
    : : : I have been trying out the ADO.net lesson 13 (http://www.programmersheaven.com/2/Les_VBNET_13_p1) on this site and have a problem I can't seem to overcome.
    : : :
    : : : I adapted the solution to the "food for thought" exercise #5. Here is the link for the source code:
    : : : http://www.programmersheaven.com/articles/faraz/vbnet/lesson14/DBAppWithBothTables_Q5.zip
    : : :
    : : : I adapted the solution from SQLServer to Access OLEDB. I am using my own DB instead of the programmersHeaven db (which I couldn't find on this site...), so i changed names of variables and tables, but I have basically kept the same structure intact.
    : : :
    : : : The problem I am having is with InitializeCommands() which is meant to initialize the insert, update and delete SQL commands. When I run the program I get a NullReferenceException unhandled error. (Reference to object not established as an instance of an object). The line of code that throws the error is:
    : : :
    : : : dataAdapterEvento.UpdateCommand = conn.CreateCommand()
    : : :
    : : : (although I get the same error with DeleteCommand or InsertCommand too)
    : : :
    : : : After I got the error I moved the call to InitializeCommands() to btnEditRecord_Click I am able to connect to the db, fill the dataset and navigate between records as I should be able to...so I would think that there is not a Null reference here...conn and dataAdapter are good because otherwise I wouldn't see any data. (both of them are btw are declared like this:
    : : :
    : : : Public Class ADOForm
    : : : Inherits System.Windows.Forms.Form
    : : : Private conn As OleDb.OleDbConnection
    : : : Private dataAdapterEvento As OleDb.OleDbDataAdapter
    : : :
    : : : Any ideas out there as to why this doesn't work?
    : : : Thanks!
    : : :
    : : :
    : : :
    : : :
    : : :
    : : Hi,
    : : it seems that your error occurs becouse conn object was not created, and conn.CreateCommand does not have "conn"
    : :
    : : I guess you've forgot to do
    : : [code]conn = New SqlConnection[/code]
    : : before you call
    : : [code]conn.CreateCommand[/code]
    : :
    : : Take a look into that example again, in btnLoadTable_Click is
    : : [code]conn = NewSqlConnection[/code]
    : : called before InitializeComponents is called
    : :
    : : btw, I would prefer creating command object in this way:
    : : [code] dataAdapterArticle.InsertCommand = New SqlCommand("INSERT INTO article " + _
    : : "(artId, title, topic, authorId, lines, dateOfPublishing) " + _
    : : "VALUES(@artId, @title, @topic, @authorId, @lines, @dateOfPublishing)", Conn)[/code]
    : :
    : : Hope this helps
    : :
    : :
    : : [blue][b][italic][size=4]P[/size]avlin [size=4]II[/italic][/size][/b][/blue]
    : :
    : : [purple]Don't take life too seriously anyway you won't escape alive from it![/purple]
    : :
    : :
    : :
    : Thanks for the suggestion...
    :
    : I verified that conn was declared before it was used and I think that it was in btnLoadTable_Click :
    :
    : [code] Dim conn As New OleDb.OleDbConnection(connectionString)
    : [/code]
    :
    : I checked too to make sure that conn is in the class declaration and it is:
    :
    : [code]
    : Public Class ADOForm2
    : Inherits System.Windows.Forms.Form
    : Private conn As OleDb.OleDbConnection
    : [/code]
    :
    : So, on a hunch, I moved the SQL commands to btnLoadTable_Click and they work just fine! I can view, update, insert and delete all records. Isn't that odd? So basically, I just moved the contents of InitializeCommands into btnLoadTable_Click and everything works...Although I'm really not sure why, but since it works, I'm not complaining.
    :
    :
    :
    :
    I don't thing this to be odd..
    There was problem with timing operations and time when conn was initialized.

    When conn is declared, it has value of Nothing (object not iniciazed) so you can not use it and conn.CreateCommand leads to NullReferenceException, as any other conn.AnyNonstaticMember does.

    When you moved your code, you've arranged that conn.CreateCommand was called after conn=New OleDbConnection. This line initializes conn object to concrete instance and you can use all methods of it.




    [blue][b][italic][size=4]P[/size]avlin [size=4]II[/italic][/size][/b][/blue]

    [purple]Don't take life too seriously anyway you won't escape alive from it![/purple]


  • tyg1578tyg1578 Posts: 5Member
    Thanks for your help Pavlin II

Sign In or Register to comment.