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)









share|improve this question
























  • 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

















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)









share|improve this question
























  • 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















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)









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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




















  • 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



















active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















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






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Berounka

Sphinx de Gizeh

Different font size/position of beamer's navigation symbols template's content depending on regular/plain...