Solved

Unassigned Local Variable??

Posted on 2009-03-30
1
404 Views
Last Modified: 2012-05-06
Experts,

In the attached method, I'm getting an "unassigned local variable" on this line:

        thisQuery.Append(" SET " + fieldName + "=" + incrementedFieldValue.ToString());

It's referring to the incrementedFieldValue vairable, which is clearly defined at the top of the method and assigned a value in the switch statement.  What am I missing?

public static void IncrementFieldValue(string connectionString, string tableName, string fieldName, string condition, int incrementValue, string dataType)

    {
 

        int initialFieldValue;

        int incrementedFieldValue;
 

        switch (dataType)

        {

            case "int":

                initialFieldValue = GetDBIntFieldValue(connectionString, tableName, fieldName, "");

                incrementedFieldValue = initialFieldValue + Convert.ToInt32(incrementValue);

                break;

        }
 
 

        StringBuilder thisQuery = new StringBuilder();

        thisQuery.Append("UPDATE " + tableName);

        thisQuery.Append(" SET " + fieldName + "=" + incrementedFieldValue.ToString());

        if (condition.Length > 0)

            thisQuery.Append(" WHERE " + condition);
 
 

        SqlConnection thisConnection = new SqlConnection(connectionString);

        thisConnection.Open();

        SqlCommand thisCommand = new SqlCommand(thisQuery.ToString(), thisConnection);

        int returned = thisCommand.ExecuteNonQuery();

        thisConnection.Close();
 
 

    }

Open in new window

0
Comment
Question by:NigelRocks
1 Comment
 
LVL 69

Accepted Solution

by:
Éric Moreau earned 500 total points
ID: 24020441
but in case your datatype is not INT, your variable remains unset. Initialize it when you declare it.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

23 Experts available now in Live!

Get 1:1 Help Now