Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Use of unassigned local variable

Status
Not open for further replies.

lfc77

Programmer
Aug 12, 2003
218
GB
I keep getting the error 'Use of unassigned local variable' in this 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.BeginTransaction(IsolationLevel.ReadCommitted);

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

//Trans 2 - NUM_TABLE
objConnectionDeactivateInvisilinkSQLSRVXwireless = new SqlConnection(ConfigurationSettings.AppSettings["strConnectSQLSRVXwirelessTest"]);
objConnectionDeactivateInvisilinkSQLSRVXwireless.Open();

objCommandDeactivateInvisilinkSQLSRVXwireless = objConnectionDeactivateInvisilinkSQLSRVXwireless.CreateCommand();

Trans2 = objConnectionDeactivateInvisilinkSQLSRVXwireless.BeginTransaction(IsolationLevel.ReadCommitted);

objCommandDeactivateInvisilinkSQLSRVXwireless.Connection = objConnectionDeactivateInvisilinkSQLSRVXwireless;
objCommandDeactivateInvisilinkSQLSRVXwireless.Transaction = 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.CommandText = strUpdateNumTable;
objCommandDeactivateInvisilinkSQLSRVXwireless.ExecuteNonQuery();

Trans1.Commit();
Trans2.Commit();
objConnectionDeactivateInvisilinkLNX.Close();
objConnectionDeactivateInvisilinkSQLSRVXwireless.Close();
lblDeactivateInvisilinkResult.Text = "Number Successfully Activated";
}
catch
{
Trans1.Rollback();
Trans2.Rollback();
objConnectionDeactivateInvisilinkLNX.Close();
objConnectionDeactivateInvisilinkSQLSRVXwireless.Close();
lblDeactivateInvisilinkResult.Text = "Database Error - please try again later";
}


Can anybody help me out with this?


Thanks,

lfc77
 
Which line does it error at?

You need to make sure that every variable you want to use in the try and in the catch clause, is defined outside of the try - catch block.

HTH, Stephan


 
I think it might be your
Code:
objConnectionxxxx = new ...
declaration that needs to be outside the first Try/Catch block.

 
I think the problem is that if the first catch block is reached (if there is an error in the first try block), the code continues into the next try block.

I managed to prevent this by adding a return statement at the end of the first catch block.


Cheers,

lfc77
 
Other than rearranging your code around. What the compiler is doing is warning you because the variable could be used before being unasigned. This is bad in any environment, but gets progressively worse in languages like C/C++.

Go ahead and assign the value of null to the necessary complex types. Again, this is merely a band-aid and an explanation. You as the developer can only reasonibly determine if you should or should not be initally setting the value to null or instantiating the complex type.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top