Solved

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

Posted on 2011-09-24
3
518 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

726 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