[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2011-09-24
3
Medium Priority
?
524 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
What if you have to shut down the entire Citrix infrastructure for hardware maintenance, software upgrades or "the unknown"? I developed this plan for "the unknown" and hope that it helps you as well. This article explains how to properly shut down …
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.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

649 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