Datareader returns null value after the first run

cshar_user_123

New member
Joined
Jan 22, 2018
Messages
2
Programming Experience
3-5
Hello,

I have a data validation implemented using C#. I have the validation at multiple cases. It works fine for the first case and it returns empty value for the subsequent test cases.

Please check the code below :

public class dataValidation
{
    SqlConnection conn = null;
    SqlDataReader reader = null;
    String uniqueId = Global.orderID;
    String Order_Customer = "";

    public customerValidation_new()
    {
    }

    void Testone.Run()
    {
        Validate.AreEqual(Getcustomer(), Global.customer, Validate.DefaultMessage, false);
        Global.customer = "";
    }

    public string Getcustomer()
    {
        try
        {
            conn = new SqlConnection();
            conn.ConnectionString = "Data Source=*****;" +
                                    "Initial Catalog=***;" +
                                    "User id=**;" +
                                    "Password=***";

            conn.Open();

            SqlCommand cmd = new SqlCommand("select CustomerID from TOrder where OrderId = @OrderID", conn);
            SqlParameter param = new SqlParameter();

            param.ParameterName = "@OrderID";
            param.Value = uniqueId;

            cmd.Parameters.Add(param);
            reader = cmd.ExecuteReader();

            while (reader.Read())
            {
                Order_Customer = reader["CustomerID"].ToString();
            }

        }
        finally
        {
            // close reader
            if (reader != null)
            {
                reader.Close();
            }

            // close connection
            if (conn != null)
            {
                conn.Close();
            }
        }

        switch (Order_Customer)
        {
            case "1":
                return "John";
            case "2":
                return "Jim";
            case "6":
                return "Kate";
            case "5":
                return "David";
        }

        return Order_Customer;
    }
}
 
Last edited by a moderator:
Firstly, I have added appropriate formatting tags to your post to make the code easer to read. Please do so for us in future, i.e.


[xcode=c#]your code here[/xcode]

or, if you want to add extra formatting of you own (e.g. bold or colour):

[code]your code here[/code]


Also, I strongly recommend that you format your code properly in VS. Your indenting is all over the place and makes your code harder to read and therefore easier to make mistakes in. In fact, I can barely read it as it is so I'm going to have to spend my time now to reformat it in order to be able to help with the issue.
 
Now that the indenting is consistent and all the unnecessary whitespace has been removed, I can read the code easily. I have to say, I don't really see a problem. If Order_Customer contains "1", "2", "6" or "5" then Getcustomer will return a specific values, otherwise it will return whatever is in Order_Customer. If you're seeing it return an empty String then that means that Order_Customer contained an empty String.

Have you debugged that code? I'm guessing not. You generally don't find bugs in code simply by reading it. You have to run it and examine it in action. If you don't know how to debug then now is the time to learn. Start by placing a breakpoint on the declaration of Getcustomer with the F9 key. When you run the project, execution will break at that line. You can then step through the code line by line using F10. At each step, you can use the Autos, Locals, Watch, Immediate and other windows to examine the state of the application. You should have a clear idea of what you expect to happen before each step and you can then check whether that did happen after the step. As soon as reality differs from expectation, you have found an issue. Even if you can't work out how to fix the issue, at least you can provide us with all the relevant information, including the data in use at the time.

By the way, you don't need a try...finally to close your reader and connection. It's far more concise to employ a 'using' block to create and destroy disposable objects. In a case like this:
using (var connection = new SqlConnection("connection string here"))
using (var command = new SqlCommand("SQL query here", connection))
{
    connection.Open();

    using (var reader = command.ExecuteReader())
    {
        // ...
    }
}

The data reader will be closed at the end of the inner 'using' block and the connection will be closed at the end of the outer block.
 
Of course I did debug
How would we know that? Maybe you are unaware but loads of people don't even know what debugging is and if you're making your first post, there's every chance that you're at that stage.
with debugger on it works. So it is very confusing why it does not work in release mode after the first run.
That's exactly the sort of relevant information that you should be providing in your initial post. That the same code appears to behave differently in Debug and Release is not normal so always relevant when trying to solve an issue with that code. Is anything else different between the two builds, e.g. the database you're connecting to? If you can't reproduce a Release issue in Debug then one option is to add some tracing code. Obviously you can't see what's happening while Release code is executing so you can add code to output that information to a log of some sort. You can then compare the log to what you expected it to be and hopefully spot where the two deviate and see what data was in use at the time.
 

Latest posts

Back
Top Bottom