1

I have the public DataTable here and the code looks right, but its not returning anything, the OrderID is correct, the query itself is correct, its not returning anything...can anyone tell me why?

public DataTable get_OrderTransaction_Master_ByOrderID(Int64 orderID)
    {

        cn = new SqlConnection(objCommon.IpcConnectionString);
        cn.Open();

        string query = "select transactionID from dbo.OrderTransaction_Master where orderID = " + orderID;
        SqlCommand queryCommand = new SqlCommand(query, cn);
        SqlDataReader queryCommandReader = queryCommand.ExecuteReader();
        DataTable dataTable = new DataTable();
        dataTable.Load(queryCommandReader);
        cn.Close();
        return dataTable;

    }
2
  • so submitting the exact same query against the database directly (e.g. in SQL mgmt studio), gives results? Commented Oct 12, 2012 at 21:19
  • Have you tried your query using Sql Management Studio? Sometimes errors become obvious in that tool. Commented Oct 12, 2012 at 21:20

1 Answer 1

2

Caveat:This is a guess based on incomplete information:

Try this: Change query string and add the line to add the parameter.

    string query = "select transactionID from dbo.OrderTransaction_Master where orderID = @OrderId";
    SqlCommand queryCommand = new SqlCommand(query, cn); 
    queryCommand.Parameters.AddWithValue("@OrderId", orderID);
    SqlDataReader queryCommandReader = queryCommand.ExecuteReader(); 

Explanation: Not only will this prevent SQL Injection, it will automatically assure that the OrderId is handled correctly.

You didn't specify what the data type is for the OrderId in the database. I'm guessing it may be non-numeric. (guid or varchar - I've seen databases that use nun-numeric IDs, so it's not inconceiveable.) If it's non-numeric you may be missing the quotes areound the value.

Example:

Where Id = 1

is NOT the same as

Where Id= '1'

Using a parameterized query will automagically fix this for you.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.