Solved

C# ODBC MYSQL insert and update sql closing and openign a connection

Posted on 2011-09-24
3
516 Views
Last Modified: 2013-12-17
 Hi experts, have a system that can update and insert but when they are run one after the other
they throw up an error
A local variable named 'myOdbcCommand' is already defined in this scope
i understand that the object myOdbcCommand is defined twice what i want to know is how to destroy the object myOdbcCommand so if i want i can redefine it again later in the program.
i have done a myOdbcCommand.Connection.Close(); but this does not destroy the object any ideas. i litrally want to close all and then open it again in the next lines to allow me to clean up all my code before moving on



// **************************************************************
                // UPDATE RECORD
                // **************************************************************
                myInsertQuery = "UPDATE vtiger_account SET vtiger_account.accountname = 'xyz compnay' WHERE (((vtiger_account.accountid)=1264))";
                OdbcCommand myOdbcCommand = new OdbcCommand(myInsertQuery);
                myOdbcCommand.Connection = dbMySQL;
                dbMySQL.Open();
                myOdbcCommand.ExecuteNonQuery();
                myOdbcCommand.Connection.Close();

                //***************************************************************
                // INSERT RECORD
                //***************************************************************
           //     OdbcConnection myOdbcCommand = new OdbcConnection(myConnection);
                myInsertQuery = "INSERT INTO `vtiger_account` (`accountid`, `account_no`, `accountname`) VALUES('1267','ACC94','mmmmmmmmmmmm test kljkjkl1')";
                OdbcCommand myOdbcCommand = new OdbcCommand(myInsertQuery);
                myOdbcCommand.Connection = dbMySQL;
                dbMySQL.Open();
                myOdbcCommand.ExecuteNonQuery();
                myOdbcCommand.Connection.Close();
0
Comment
Question by:sydneyguy
  • 2
3 Comments
 
LVL 4

Accepted Solution

by:
theHollow earned 500 total points
ID: 36592514
You cannot declare a variable with the same name twice. As far as I know, there is no way round that. I'm also not quite sure why you would like to declare it twice.

In my opinion, you have three choices.
1) Make the "Insert" and "Update" into two separate methods.
2) Declare two different namts for the "insert" and the "update" command
3) Declare the comamnd in the top and use the same variable.

OdbcCommand myOdbcCommand = null;

myUpdateQuery = "UPDATE vtiger_account SET vtiger_account.accountname = 'xyz compnay' WHERE (((vtiger_account.accountid)=1264))";
myInsertQuery = "INSERT INTO `vtiger_account` (`accountid`, `account_no`, `accountname`) VALUES('1267','ACC94','mmmmmmmmmmmm test kljkjkl1')";

myOdbcCommand = new OdbcCommand(myUpdateQuery);
myOdbcCommand.Connection = dbMySQL;
dbMySQL.Open();
myOdbcCommand.ExecuteNonQuery();
myOdbcCommand.Connection.Close();

myOdbcCommand = new OdbcCommand(myInsertQuery);
myOdbcCommand.Connection = dbMySQL;
dbMySQL.Open();
myOdbcCommand.ExecuteNonQuery();
myOdbcCommand.Connection.Close(); 

Open in new window


I would cretainly make the variable "dbMySQL" more central to this code.
Because you can create a command with;
myOdbcCommand = dbMySQL.CreateCommand();

Hope it helped :-)
0
 

Author Closing Comment

by:sydneyguy
ID: 36592552
thanks i have already broken it out into 3 methods.
how is your band going?
0
 
LVL 4

Expert Comment

by:theHollow
ID: 36592663
Hi
The band is doing progress :-)
Feel free to check out our music @ MySpace
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

832 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question