Solved

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

Posted on 2011-09-24
3
515 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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

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…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

914 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