Why is “Input string was not in a correct format.” in the query in mySQL?
up vote
0
down vote
favorite
In the following code,
I get a run time error of
"Input string was not in a correct format." after oCommand.ExecuteReader().
oCommand.CommandText = "CheckOutItem";
oCommand.CommandType = System.Data.CommandType.StoredProcedure;
oCommand.Parameters.AddWithValue("@OrderID", OrderID);
oCommand.Parameters.AddWithValue("@GivenLargeSizeName", Enum.GetName(typeof(Size), Size.REGULAR));
oCommand.Parameters.AddWithValue("@GivenSmallSizeName", Enum.GetName(typeof(Size), Size.SMALL));
oCommand.Parameters.AddWithValue("@Latitude",oOrder.OrderTimeLocation.Location.latitude);
oCommand.Parameters.AddWithValue("@Longitude",oOrder.OrderTimeLocation.Location.longitude);
oCommand.Parameters.AddWithValue("@ListingID", MySqlDbType.Int64);
oCommand.Parameters.AddWithValue("@Quantity", MySqlDbType.Int16);
oCommand.Parameters.AddWithValue("@Size", MySqlDbType.VarChar);
oCommand.Parameters.AddWithValue("@DeliveryMethod", MySqlDbType.VarChar);
foreach (var OrderItem in oOrder.OrderItems)
{
oCommand.Parameters["@ListingID"].Value = OrderItem.ListingID;
oCommand.Parameters["@Quantity"].Value = OrderItem.Quantity;
oCommand.Parameters["@Size"].Value = Enum.GetName(typeof(Size), OrderItem.Size);
oCommand.Parameters["@DeliveryMethod"].Value = Enum.GetName(typeof(DeliverMethod), OrderItem.DeliveryPickupMethod);
using (var reader = oCommand.ExecuteReader())
{
while (reader.Read())
{
Int64 OrderItemID = Convert.ToInt64(reader["@ORDER_ITEM_ID"]);
Int64 OrderItemStatusID = Convert.ToInt64(reader["@ORDER_ITEM_STATUS_ID"]);
Int64 OrderItemPriceID = Convert.ToInt64(reader["@ORDER_ITEM_PRICE_ID"]);
TotalOrderCostFromDB = TotalOrderCostFromDB + Convert.ToDecimal(reader["@TOTAL_ORDER_COST"]);
}
}
}
if I use statements like
oCommand.Parameters.AddWithValue("@ListingID",OrderItem.ListingID)
works fine but I can not loop.
All the values I am assigning are correct in debugging and no nulls or weird stuff getting into .Value.
Since I get the correct answer in AddWithValue method, I don't think I have any issue with StoredPoocedure.
Is there a better approach to loop for this purpose?
After some debugging, I was able to narrow down the issues to last two lines
oCommand.Parameters.AddWithValue("@Size", MySqlDbType.VarChar);
oCommand.Parameters.AddWithValue("@DeliveryMethod", MySqlDbType.VarChar);
If I initialize this with a dummy string, everything is fine. But initializing with VarChar or String cause problems.
For my understanding, following returns a string right?
Enum.GetName(typeof(Size), OrderItem.Size)
mysql asp.net
add a comment |
up vote
0
down vote
favorite
In the following code,
I get a run time error of
"Input string was not in a correct format." after oCommand.ExecuteReader().
oCommand.CommandText = "CheckOutItem";
oCommand.CommandType = System.Data.CommandType.StoredProcedure;
oCommand.Parameters.AddWithValue("@OrderID", OrderID);
oCommand.Parameters.AddWithValue("@GivenLargeSizeName", Enum.GetName(typeof(Size), Size.REGULAR));
oCommand.Parameters.AddWithValue("@GivenSmallSizeName", Enum.GetName(typeof(Size), Size.SMALL));
oCommand.Parameters.AddWithValue("@Latitude",oOrder.OrderTimeLocation.Location.latitude);
oCommand.Parameters.AddWithValue("@Longitude",oOrder.OrderTimeLocation.Location.longitude);
oCommand.Parameters.AddWithValue("@ListingID", MySqlDbType.Int64);
oCommand.Parameters.AddWithValue("@Quantity", MySqlDbType.Int16);
oCommand.Parameters.AddWithValue("@Size", MySqlDbType.VarChar);
oCommand.Parameters.AddWithValue("@DeliveryMethod", MySqlDbType.VarChar);
foreach (var OrderItem in oOrder.OrderItems)
{
oCommand.Parameters["@ListingID"].Value = OrderItem.ListingID;
oCommand.Parameters["@Quantity"].Value = OrderItem.Quantity;
oCommand.Parameters["@Size"].Value = Enum.GetName(typeof(Size), OrderItem.Size);
oCommand.Parameters["@DeliveryMethod"].Value = Enum.GetName(typeof(DeliverMethod), OrderItem.DeliveryPickupMethod);
using (var reader = oCommand.ExecuteReader())
{
while (reader.Read())
{
Int64 OrderItemID = Convert.ToInt64(reader["@ORDER_ITEM_ID"]);
Int64 OrderItemStatusID = Convert.ToInt64(reader["@ORDER_ITEM_STATUS_ID"]);
Int64 OrderItemPriceID = Convert.ToInt64(reader["@ORDER_ITEM_PRICE_ID"]);
TotalOrderCostFromDB = TotalOrderCostFromDB + Convert.ToDecimal(reader["@TOTAL_ORDER_COST"]);
}
}
}
if I use statements like
oCommand.Parameters.AddWithValue("@ListingID",OrderItem.ListingID)
works fine but I can not loop.
All the values I am assigning are correct in debugging and no nulls or weird stuff getting into .Value.
Since I get the correct answer in AddWithValue method, I don't think I have any issue with StoredPoocedure.
Is there a better approach to loop for this purpose?
After some debugging, I was able to narrow down the issues to last two lines
oCommand.Parameters.AddWithValue("@Size", MySqlDbType.VarChar);
oCommand.Parameters.AddWithValue("@DeliveryMethod", MySqlDbType.VarChar);
If I initialize this with a dummy string, everything is fine. But initializing with VarChar or String cause problems.
For my understanding, following returns a string right?
Enum.GetName(typeof(Size), OrderItem.Size)
mysql asp.net
What you mean you cant loop?
– Juan Carlos Oropeza
Nov 20 at 15:08
If I have addwithvalue in loop, it says item already added. So I have to change only value in the foreach loop.
– PCG
Nov 20 at 15:13
1
are you sure you can change the parameter value in the loop? I always create a new command.
– Juan Carlos Oropeza
Nov 20 at 15:18
you should create a new command object for each query you want to run, it's simpler to manage. You can't keep adding the same parameters over and over again to the same command object. If you need to add some of parameters the same to every command you create, with the same values each time, then you can easily move that bit into a separate method and just call it each time within your loop, passing in the new command object.
– ADyson
Nov 20 at 15:34
I already did oCommand.Parameters["@ListingID"].Value = OrderItem.ListingID that few times and did not cause problems. My commandText is same only value changes for fixed set of Parameter names.
– PCG
Nov 20 at 16:13
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
In the following code,
I get a run time error of
"Input string was not in a correct format." after oCommand.ExecuteReader().
oCommand.CommandText = "CheckOutItem";
oCommand.CommandType = System.Data.CommandType.StoredProcedure;
oCommand.Parameters.AddWithValue("@OrderID", OrderID);
oCommand.Parameters.AddWithValue("@GivenLargeSizeName", Enum.GetName(typeof(Size), Size.REGULAR));
oCommand.Parameters.AddWithValue("@GivenSmallSizeName", Enum.GetName(typeof(Size), Size.SMALL));
oCommand.Parameters.AddWithValue("@Latitude",oOrder.OrderTimeLocation.Location.latitude);
oCommand.Parameters.AddWithValue("@Longitude",oOrder.OrderTimeLocation.Location.longitude);
oCommand.Parameters.AddWithValue("@ListingID", MySqlDbType.Int64);
oCommand.Parameters.AddWithValue("@Quantity", MySqlDbType.Int16);
oCommand.Parameters.AddWithValue("@Size", MySqlDbType.VarChar);
oCommand.Parameters.AddWithValue("@DeliveryMethod", MySqlDbType.VarChar);
foreach (var OrderItem in oOrder.OrderItems)
{
oCommand.Parameters["@ListingID"].Value = OrderItem.ListingID;
oCommand.Parameters["@Quantity"].Value = OrderItem.Quantity;
oCommand.Parameters["@Size"].Value = Enum.GetName(typeof(Size), OrderItem.Size);
oCommand.Parameters["@DeliveryMethod"].Value = Enum.GetName(typeof(DeliverMethod), OrderItem.DeliveryPickupMethod);
using (var reader = oCommand.ExecuteReader())
{
while (reader.Read())
{
Int64 OrderItemID = Convert.ToInt64(reader["@ORDER_ITEM_ID"]);
Int64 OrderItemStatusID = Convert.ToInt64(reader["@ORDER_ITEM_STATUS_ID"]);
Int64 OrderItemPriceID = Convert.ToInt64(reader["@ORDER_ITEM_PRICE_ID"]);
TotalOrderCostFromDB = TotalOrderCostFromDB + Convert.ToDecimal(reader["@TOTAL_ORDER_COST"]);
}
}
}
if I use statements like
oCommand.Parameters.AddWithValue("@ListingID",OrderItem.ListingID)
works fine but I can not loop.
All the values I am assigning are correct in debugging and no nulls or weird stuff getting into .Value.
Since I get the correct answer in AddWithValue method, I don't think I have any issue with StoredPoocedure.
Is there a better approach to loop for this purpose?
After some debugging, I was able to narrow down the issues to last two lines
oCommand.Parameters.AddWithValue("@Size", MySqlDbType.VarChar);
oCommand.Parameters.AddWithValue("@DeliveryMethod", MySqlDbType.VarChar);
If I initialize this with a dummy string, everything is fine. But initializing with VarChar or String cause problems.
For my understanding, following returns a string right?
Enum.GetName(typeof(Size), OrderItem.Size)
mysql asp.net
In the following code,
I get a run time error of
"Input string was not in a correct format." after oCommand.ExecuteReader().
oCommand.CommandText = "CheckOutItem";
oCommand.CommandType = System.Data.CommandType.StoredProcedure;
oCommand.Parameters.AddWithValue("@OrderID", OrderID);
oCommand.Parameters.AddWithValue("@GivenLargeSizeName", Enum.GetName(typeof(Size), Size.REGULAR));
oCommand.Parameters.AddWithValue("@GivenSmallSizeName", Enum.GetName(typeof(Size), Size.SMALL));
oCommand.Parameters.AddWithValue("@Latitude",oOrder.OrderTimeLocation.Location.latitude);
oCommand.Parameters.AddWithValue("@Longitude",oOrder.OrderTimeLocation.Location.longitude);
oCommand.Parameters.AddWithValue("@ListingID", MySqlDbType.Int64);
oCommand.Parameters.AddWithValue("@Quantity", MySqlDbType.Int16);
oCommand.Parameters.AddWithValue("@Size", MySqlDbType.VarChar);
oCommand.Parameters.AddWithValue("@DeliveryMethod", MySqlDbType.VarChar);
foreach (var OrderItem in oOrder.OrderItems)
{
oCommand.Parameters["@ListingID"].Value = OrderItem.ListingID;
oCommand.Parameters["@Quantity"].Value = OrderItem.Quantity;
oCommand.Parameters["@Size"].Value = Enum.GetName(typeof(Size), OrderItem.Size);
oCommand.Parameters["@DeliveryMethod"].Value = Enum.GetName(typeof(DeliverMethod), OrderItem.DeliveryPickupMethod);
using (var reader = oCommand.ExecuteReader())
{
while (reader.Read())
{
Int64 OrderItemID = Convert.ToInt64(reader["@ORDER_ITEM_ID"]);
Int64 OrderItemStatusID = Convert.ToInt64(reader["@ORDER_ITEM_STATUS_ID"]);
Int64 OrderItemPriceID = Convert.ToInt64(reader["@ORDER_ITEM_PRICE_ID"]);
TotalOrderCostFromDB = TotalOrderCostFromDB + Convert.ToDecimal(reader["@TOTAL_ORDER_COST"]);
}
}
}
if I use statements like
oCommand.Parameters.AddWithValue("@ListingID",OrderItem.ListingID)
works fine but I can not loop.
All the values I am assigning are correct in debugging and no nulls or weird stuff getting into .Value.
Since I get the correct answer in AddWithValue method, I don't think I have any issue with StoredPoocedure.
Is there a better approach to loop for this purpose?
After some debugging, I was able to narrow down the issues to last two lines
oCommand.Parameters.AddWithValue("@Size", MySqlDbType.VarChar);
oCommand.Parameters.AddWithValue("@DeliveryMethod", MySqlDbType.VarChar);
If I initialize this with a dummy string, everything is fine. But initializing with VarChar or String cause problems.
For my understanding, following returns a string right?
Enum.GetName(typeof(Size), OrderItem.Size)
mysql asp.net
mysql asp.net
edited Nov 21 at 14:52
asked Nov 20 at 15:03
PCG
85111
85111
What you mean you cant loop?
– Juan Carlos Oropeza
Nov 20 at 15:08
If I have addwithvalue in loop, it says item already added. So I have to change only value in the foreach loop.
– PCG
Nov 20 at 15:13
1
are you sure you can change the parameter value in the loop? I always create a new command.
– Juan Carlos Oropeza
Nov 20 at 15:18
you should create a new command object for each query you want to run, it's simpler to manage. You can't keep adding the same parameters over and over again to the same command object. If you need to add some of parameters the same to every command you create, with the same values each time, then you can easily move that bit into a separate method and just call it each time within your loop, passing in the new command object.
– ADyson
Nov 20 at 15:34
I already did oCommand.Parameters["@ListingID"].Value = OrderItem.ListingID that few times and did not cause problems. My commandText is same only value changes for fixed set of Parameter names.
– PCG
Nov 20 at 16:13
add a comment |
What you mean you cant loop?
– Juan Carlos Oropeza
Nov 20 at 15:08
If I have addwithvalue in loop, it says item already added. So I have to change only value in the foreach loop.
– PCG
Nov 20 at 15:13
1
are you sure you can change the parameter value in the loop? I always create a new command.
– Juan Carlos Oropeza
Nov 20 at 15:18
you should create a new command object for each query you want to run, it's simpler to manage. You can't keep adding the same parameters over and over again to the same command object. If you need to add some of parameters the same to every command you create, with the same values each time, then you can easily move that bit into a separate method and just call it each time within your loop, passing in the new command object.
– ADyson
Nov 20 at 15:34
I already did oCommand.Parameters["@ListingID"].Value = OrderItem.ListingID that few times and did not cause problems. My commandText is same only value changes for fixed set of Parameter names.
– PCG
Nov 20 at 16:13
What you mean you cant loop?
– Juan Carlos Oropeza
Nov 20 at 15:08
What you mean you cant loop?
– Juan Carlos Oropeza
Nov 20 at 15:08
If I have addwithvalue in loop, it says item already added. So I have to change only value in the foreach loop.
– PCG
Nov 20 at 15:13
If I have addwithvalue in loop, it says item already added. So I have to change only value in the foreach loop.
– PCG
Nov 20 at 15:13
1
1
are you sure you can change the parameter value in the loop? I always create a new command.
– Juan Carlos Oropeza
Nov 20 at 15:18
are you sure you can change the parameter value in the loop? I always create a new command.
– Juan Carlos Oropeza
Nov 20 at 15:18
you should create a new command object for each query you want to run, it's simpler to manage. You can't keep adding the same parameters over and over again to the same command object. If you need to add some of parameters the same to every command you create, with the same values each time, then you can easily move that bit into a separate method and just call it each time within your loop, passing in the new command object.
– ADyson
Nov 20 at 15:34
you should create a new command object for each query you want to run, it's simpler to manage. You can't keep adding the same parameters over and over again to the same command object. If you need to add some of parameters the same to every command you create, with the same values each time, then you can easily move that bit into a separate method and just call it each time within your loop, passing in the new command object.
– ADyson
Nov 20 at 15:34
I already did oCommand.Parameters["@ListingID"].Value = OrderItem.ListingID that few times and did not cause problems. My commandText is same only value changes for fixed set of Parameter names.
– PCG
Nov 20 at 16:13
I already did oCommand.Parameters["@ListingID"].Value = OrderItem.ListingID that few times and did not cause problems. My commandText is same only value changes for fixed set of Parameter names.
– PCG
Nov 20 at 16:13
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53395850%2fwhy-is-input-string-was-not-in-a-correct-format-in-the-query-in-mysql%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
What you mean you cant loop?
– Juan Carlos Oropeza
Nov 20 at 15:08
If I have addwithvalue in loop, it says item already added. So I have to change only value in the foreach loop.
– PCG
Nov 20 at 15:13
1
are you sure you can change the parameter value in the loop? I always create a new command.
– Juan Carlos Oropeza
Nov 20 at 15:18
you should create a new command object for each query you want to run, it's simpler to manage. You can't keep adding the same parameters over and over again to the same command object. If you need to add some of parameters the same to every command you create, with the same values each time, then you can easily move that bit into a separate method and just call it each time within your loop, passing in the new command object.
– ADyson
Nov 20 at 15:34
I already did oCommand.Parameters["@ListingID"].Value = OrderItem.ListingID that few times and did not cause problems. My commandText is same only value changes for fixed set of Parameter names.
– PCG
Nov 20 at 16:13