Solved

Unassigned Local Variable??

Posted on 2009-03-30
1
405 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 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Login 17 50
User Authentication using Digital Certificate 2 44
How to call weather service by using asmx? 4 29
Hey!! 5 33
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

786 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