Solved

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

Posted on 2011-09-24
3
512 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
Comment Utility
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
Comment Utility
thanks i have already broken it out into 3 methods.
how is your band going?
0
 
LVL 4

Expert Comment

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

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Join & Write a Comment

Suggested Solutions

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

743 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now