473,408 Members | 2,427 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

Use of unassigned local variable

I keep getting the error 'Use of unassigned local variable' in my code,
which I have used before and it works fine :

SqlTransaction Trans1, Trans2;
SqlConnection objConnectionDeactivateInvisilinkLNX,
objConnectionDeactivateInvisilinkSQLSRVXwireless;
SqlCommand objCommandDeactivateInvisilinkLNX,
objCommandDeactivateInvisilinkSQLSRVXwireless;
try
{
//Trans 1 - COM4S_CARDS
objConnectionDeactivateInvisilinkLNX = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectLNXTest"]);
objConnectionDeactivateInvisilinkLNX.Open();

objCommandDeactivateInvisilinkLNX =
objConnectionDeactivateInvisilinkLNX.CreateCommand ();

Trans1 =
objConnectionDeactivateInvisilinkLNX.BeginTransact ion(IsolationLevel.Rea
dCommitted);

objCommandDeactivateInvisilinkLNX.Connection =
objConnectionDeactivateInvisilinkLNX;
objCommandDeactivateInvisilinkLNX.Transaction = Trans1;

//Trans 2 - NUM_TABLE
objConnectionDeactivateInvisilinkSQLSRVXwireless = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectSQLSRVXwirele
ssTest"]);
objConnectionDeactivateInvisilinkSQLSRVXwireless.O pen();

objCommandDeactivateInvisilinkSQLSRVXwireless =
objConnectionDeactivateInvisilinkSQLSRVXwireless.C reateCommand();

Trans2 =
objConnectionDeactivateInvisilinkSQLSRVXwireless.B eginTransaction(Isolat
ionLevel.ReadCommitted);

objCommandDeactivateInvisilinkSQLSRVXwireless.Conn ection =
objConnectionDeactivateInvisilinkSQLSRVXwireless;
objCommandDeactivateInvisilinkSQLSRVXwireless.Tran saction = Trans2;
}
catch
{
lblDeactivateInvisilinkResult.Text = "Could Not Connect To Database -
please try again later";
}

try
{
//1 - COM4S_CARDS
string strUpdateCOM4SCards;

strUpdateCOM4SCards = "UPDATE COM4S_CARDS ";
strUpdateCOM4SCards += "SET STATE = 'N', ALLOW_TOGGLE = 0,
SECONDARY_OLI = NULL, G_BIT = 0 ";
strUpdateCOM4SCards += "WHERE SERIAL = " +
Convert.ToInt32(ViewState["Serial"]);

objCommandDeactivateInvisilinkLNX.CommandText = strUpdateCOM4SCards;
objCommandDeactivateInvisilinkLNX.ExecuteNonQuery( );
//2 - NUM_TABLE
string strUpdateNumTable, strCLI;

strCLI = Convert.ToString("07059" + ViewState["Serial"]);

strUpdateNumTable = "DELETE FROM NUM_TABLE ";
strUpdateNumTable += "WHERE CLI = '" + strCLI + "'";

objCommandDeactivateInvisilinkSQLSRVXwireless.Comm andText =
strUpdateNumTable;
objCommandDeactivateInvisilinkSQLSRVXwireless.Exec uteNonQuery();

Trans1.Commit();
Trans2.Commit();
objConnectionDeactivateInvisilinkLNX.Close();
objConnectionDeactivateInvisilinkSQLSRVXwireless.C lose();
lblDeactivateInvisilinkResult.Text = "Number Successfully Activated";
}
catch
{
Trans1.Rollback();
Trans2.Rollback();
objConnectionDeactivateInvisilinkLNX.Close();
objConnectionDeactivateInvisilinkSQLSRVXwireless.C lose();
lblDeactivateInvisilinkResult.Text = "Database Error - please try
again later";
}
Can anybody help me out with this?
Thanks,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #1
3 3925
I believe the problem is because you're using the TransX objects in the
second try block. If try block 1 throws an exception, there is no guarantee
that the TransX objects are initialized.

Initialize them to NULL in the definition block and you'll resolve the
compiler issue, but you should also check the variables in the code prior to
using them.
Arild

"Mike P" <mr*@telcoelectronics.co.uk> wrote in message
news:um**************@TK2MSFTNGP11.phx.gbl...
I keep getting the error 'Use of unassigned local variable' in my code,
which I have used before and it works fine :

SqlTransaction Trans1, Trans2;
SqlConnection objConnectionDeactivateInvisilinkLNX,
objConnectionDeactivateInvisilinkSQLSRVXwireless;
SqlCommand objCommandDeactivateInvisilinkLNX,
objCommandDeactivateInvisilinkSQLSRVXwireless;
try
{
//Trans 1 - COM4S_CARDS
objConnectionDeactivateInvisilinkLNX = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectLNXTest"]);
objConnectionDeactivateInvisilinkLNX.Open();

objCommandDeactivateInvisilinkLNX =
objConnectionDeactivateInvisilinkLNX.CreateCommand ();

Trans1 =
objConnectionDeactivateInvisilinkLNX.BeginTransact ion(IsolationLevel.Rea
dCommitted);

objCommandDeactivateInvisilinkLNX.Connection =
objConnectionDeactivateInvisilinkLNX;
objCommandDeactivateInvisilinkLNX.Transaction = Trans1;

//Trans 2 - NUM_TABLE
objConnectionDeactivateInvisilinkSQLSRVXwireless = new
SqlConnection(ConfigurationSettings.AppSettings["strConnectSQLSRVXwirele
ssTest"]);
objConnectionDeactivateInvisilinkSQLSRVXwireless.O pen();

objCommandDeactivateInvisilinkSQLSRVXwireless =
objConnectionDeactivateInvisilinkSQLSRVXwireless.C reateCommand();

Trans2 =
objConnectionDeactivateInvisilinkSQLSRVXwireless.B eginTransaction(Isolat
ionLevel.ReadCommitted);

objCommandDeactivateInvisilinkSQLSRVXwireless.Conn ection =
objConnectionDeactivateInvisilinkSQLSRVXwireless;
objCommandDeactivateInvisilinkSQLSRVXwireless.Tran saction = Trans2;
}
catch
{
lblDeactivateInvisilinkResult.Text = "Could Not Connect To Database -
please try again later";
}

try
{
//1 - COM4S_CARDS
string strUpdateCOM4SCards;

strUpdateCOM4SCards = "UPDATE COM4S_CARDS ";
strUpdateCOM4SCards += "SET STATE = 'N', ALLOW_TOGGLE = 0,
SECONDARY_OLI = NULL, G_BIT = 0 ";
strUpdateCOM4SCards += "WHERE SERIAL = " +
Convert.ToInt32(ViewState["Serial"]);

objCommandDeactivateInvisilinkLNX.CommandText = strUpdateCOM4SCards;
objCommandDeactivateInvisilinkLNX.ExecuteNonQuery( );
//2 - NUM_TABLE
string strUpdateNumTable, strCLI;

strCLI = Convert.ToString("07059" + ViewState["Serial"]);

strUpdateNumTable = "DELETE FROM NUM_TABLE ";
strUpdateNumTable += "WHERE CLI = '" + strCLI + "'";

objCommandDeactivateInvisilinkSQLSRVXwireless.Comm andText =
strUpdateNumTable;
objCommandDeactivateInvisilinkSQLSRVXwireless.Exec uteNonQuery();

Trans1.Commit();
Trans2.Commit();
objConnectionDeactivateInvisilinkLNX.Close();
objConnectionDeactivateInvisilinkSQLSRVXwireless.C lose();
lblDeactivateInvisilinkResult.Text = "Number Successfully Activated";
}
catch
{
Trans1.Rollback();
Trans2.Rollback();
objConnectionDeactivateInvisilinkLNX.Close();
objConnectionDeactivateInvisilinkSQLSRVXwireless.C lose();
lblDeactivateInvisilinkResult.Text = "Database Error - please try
again later";
}
Can anybody help me out with this?
Thanks,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!

Nov 15 '05 #2
Thanks Arild. I can avoid this error by using return in the first catch
block, so if there is an error in the first block, the second block
doesn't get reached.

Cheers,

Mike

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 15 '05 #3
Mike P wrote:
I keep getting the error 'Use of unassigned local variable' in my
code, which I have used before and it works fine :


This can be easily avoided by declaring your variable and setting it to
null.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Eric Sabine | last post by:
I just need to understand this bit. In some code, I had the following give me an error at build time XmlDocument xmlDoc; xmlDoc.LoadXml(xmlString); The error was "unassigned use of local...
5
by: Mike P | last post by:
I am instantiating a class in a switch statement as there are a number of different overloads depending upon the data entered by the user. The problem I have is that after instantiating my class,...
12
by: Rene | last post by:
I understand that if I don't assign a *local* variable before I use it, the compiler will generate a "Use of unassigned local variable" error. What I don't get is why doesn't the compiler just...
3
by: John Smith | last post by:
In the following (pseudo)code, why is it that C# returns an "unassigned local variable" error for nVar? It seems I have to declare the variable outside foo() for the error to dissapear. void...
29
by: Joseph Geretz | last post by:
Use of unassigned local variable 'fs' Please see where I've indicated where the compiler is flagging this error in the method below. fs is initialized in the first line in the try block, so why...
9
by: tshad | last post by:
I am getting an error: Use of unassigned local variable 'postDateRow' But it is assigned. Here is the code: int payDateRow; int postDateRow;
3
by: Antonio | last post by:
Can somebody tell me what's wrong with this code? When I try to debug, I get "Use of unassigned local variable 'ip2se'. string ip2se; if(e.Item.Cells.Text == e.Item.Cells.Text) ip2se =...
22
by: Laura T. | last post by:
In the following example, c# 2.0 compiler says that a3 and ret are used before assigned. as far as I can see, definite assignment is made. If I add finally { ret = true; a3 = "b3";
8
by: Dom | last post by:
This is a little tricky, but I think the error I'm getting isn't valid. The compiler just doesn't know my logic. MyObject o; switch (IntVariable) { case 1: o = new MyObject() break;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.