I have set the DateTime delivered variable and I need to get the modified variable from the "GetQuantityInStock" stored procedure in order to apply it in the next request. How can I do that?
Error: Using a local variable "delivered" that is not assigned a value.
private void InsertQuery()
{
var count =
from order in showOrders
group order by order.ProductArticleNumber into articles
select new
{
ProductArticleNumber = articles.Key,
Count = articles.Count(),
};
DateTime today = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
DateTime delivered;
int id;
if (cboPickUpPoints.SelectedValue != null)
{
int pickUpPoint = (int)cboPickUpPoints.SelectedValue;
if (MessageBox.Show("Вы точно уверены в своём выборе? ", "Сообщение",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
using (SqlConnection connectionString = new SqlConnection(Properties.Settings.Default.connectionString))
{
connectionString.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "GetCountArticles";
cmd.Connection = connectionString;
foreach (var items in count)
{
cmd.Parameters.AddWithValue("@countArticles", items.Count);
cmd.Parameters.AddWithValue("@ProductArticleNumber", items.ProductArticleNumber);
cmd.ExecuteNonQuery();
}
SqlCommand cmd1 = new SqlCommand();
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.CommandText = "GetQuantityInStock";
cmd1.Connection = connectionString;
foreach (var items in count)
{
cmd1.Parameters.AddWithValue("@ProductArticleNumber", items.ProductArticleNumber);
int countArticles = (int)cmd1.ExecuteScalar();
if (countArticles > 3)
{
delivered = today.AddDays(3); //<-- The variable I need
}
else
{
delivered = today.AddDays(6); //<-- The variable I need
}
}
SqlCommand cmd2 = new SqlCommand();
cmd2.CommandType = CommandType.StoredProcedure;
cmd2.CommandText = "InsertOrder";
cmd2.Connection = connectionString;
cmd2.Parameters.AddWithValue("@OrderDate", today);
cmd2.Parameters.AddWithValue("@OrderDeliveryDate", delivered); //<-- Error
cmd2.Parameters.AddWithValue("@OrderPickupPoint", pickUpPoint);
>Solution :
You are initializing delivered inside of a foreach statement body which is not guaranteed to be executed (if count is empty) at least from the compiler point of view.
just initialize it something:
DateTime delivered = DateTime.UtcNow;
or rewrite code in the way that it is initialized and used in correct place.
Also it seems a bit strange that you are calculating a single date per batch, basically it will result in the countArticles analyzed only for the last item in count which can produce inconsistent results depending on the order of the items in count. If you are adamant on using stored procedures then I would recommend to rewrite both GetCountArticles and ProductArticleNumber to accept collection of ids (for example like using table valued parameters for SQL server) and process them in batch on the db side (i.e. ProductArticleNumber will become MaxProductArticleNumber or similar)
Also note that there are arguments against using AddWithValue – see AddWithValue is Evil