Solved

Unassigned Local Variable??

Posted on 2009-03-30
1
408 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 70

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Finding and deleting duplicate (picture) files can be a time consuming task. My wife and I, our three kids and their families all share one dilemma: Managing our pictures. Between desktops, laptops, phones, tablets, and cameras; over the last decade…

738 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