Solved

Unassigned Local Variable??

Posted on 2009-03-30
1
403 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
Comment Utility
but in case your datatype is not INT, your variable remains unset. Initialize it when you declare it.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Bit flags and bit flag manipulation is perhaps one of the most underrated strategies in programming, likely because most programmers developing in high-level languages rely too much on the high-level features, and forget about the low-level ones. Th…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

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

11 Experts available now in Live!

Get 1:1 Help Now