[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Unassigned Local Variable??

Posted on 2009-03-30
1
Medium Priority
?
414 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month18 days, 23 hours left to enroll

834 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