C
C#2y ago
Crayv

It's working fine but the error is 'Input string was not in a correct format.'

Can anyone help me here i'll post the error
100 Replies
Crayv
CrayvOP2y ago
Crayv
CrayvOP2y ago
and here is the database Table
Crayv
CrayvOP2y ago
Crayv
CrayvOP2y ago
thank you for helping I also tried the Convert.ToInt32 one it's the smae
TheRanger
TheRanger2y ago
hover ur mouse on txtSupplierID.Text while the error message is displayed to see its value @Crayv
Crayv
CrayvOP2y ago
wdynm hover?
TheRanger
TheRanger2y ago
like put ur mouse over it
Crayv
CrayvOP2y ago
Crayv
CrayvOP2y ago
oh
SUPER MEGA T REX
That's too large for a 16 bit integer.
Crayv
CrayvOP2y ago
Nononon that's not updated its Convert.ToInt32 one hehe
MODiX
MODiX2y ago
TheRanger#3357
REPL Result: Success
Convert.ToInt32("11942965")
Convert.ToInt32("11942965")
Result: int
11942965
11942965
Compile: 641.765ms | Execution: 38.729ms | React with ❌ to remove this embed.
SUPER MEGA T REX
You may need to call Trim on the string if it contains a space. Also, you should look into int.TryParse.
TheRanger
TheRanger2y ago
lets just see its value first hover ur mouse on txtSupplierID.Text while the error message is displayed to see its value
Crayv
CrayvOP2y ago
Crayv
CrayvOP2y ago
i can't over because the error will appear hover
TheRanger
TheRanger2y ago
you can just put ur mouse there
Crayv
CrayvOP2y ago
like this?
Crayv
CrayvOP2y ago
Crayv
CrayvOP2y ago
or do i not run it
TheRanger
TheRanger2y ago
on txtSupplierID.Text in ur visual studio
TheRanger
TheRanger2y ago
something like this should appear
Crayv
CrayvOP2y ago
empty
Crayv
CrayvOP2y ago
Crayv
CrayvOP2y ago
it's a textbox
TheRanger
TheRanger2y ago
TheRanger
TheRanger2y ago
hover ur mouse on that circle a small window like this will pop up
Crayv
CrayvOP2y ago
TheRanger
TheRanger2y ago
while the System.FormatException is popped up u can only see its value when the program is crashed
Crayv
CrayvOP2y ago
okay what does that mean?
TheRanger
TheRanger2y ago
when you see this error hover ur mouse on the circle
Crayv
CrayvOP2y ago
okok wait
Crayv
CrayvOP2y ago
TheRanger
TheRanger2y ago
TheRanger
TheRanger2y ago
its empty u cant convert an empty string to an integer
Crayv
CrayvOP2y ago
it's not empty tho...
Crayv
CrayvOP2y ago
Crayv
CrayvOP2y ago
i put in the value and then add it in the database so it's not empty
TheRanger
TheRanger2y ago
hmm, maybe wrong textbox?
Crayv
CrayvOP2y ago
i don't think so it's working for everything except the supplier
TheRanger
TheRanger2y ago
well go to ur designer mode click on the supplier id textbox see what's it called
Crayv
CrayvOP2y ago
what's called?
TheRanger
TheRanger2y ago
the textbox
Crayv
CrayvOP2y ago
it's called txtSupplierID
TheRanger
TheRanger2y ago
i have to see the proof screenshot or anything
Crayv
CrayvOP2y ago
Crayv
CrayvOP2y ago
is this correct?
TheRanger
TheRanger2y ago
thats not the issue, no need to post it
Crayv
CrayvOP2y ago
okayokay i just thought maybe it's my first time creating a 3 layer application
Pobiega
Pobiega2y ago
well your error is from Convert.ToInt32, which is a parsing method and its throwing a parsing error, because the string is empty. so we need to figure out why it is
Crayv
CrayvOP2y ago
hmmm
TheRanger
TheRanger2y ago
what about the rest of the textboxes? try hovering on them
Crayv
CrayvOP2y ago
but it's not empty :((
Pobiega
Pobiega2y ago
the debugger doesnt agree with you and I trust the debugger 🙂
Crayv
CrayvOP2y ago
well that's true
Pobiega
Pobiega2y ago
yeah do what Ranger suggested, make sure they have values as expected
Crayv
CrayvOP2y ago
wait check this out
Crayv
CrayvOP2y ago
Crayv
CrayvOP2y ago
i'm gonna add this to the database
Crayv
CrayvOP2y ago
Crayv
CrayvOP2y ago
it's successfully got added but got the error afterwards lemme check
Pobiega
Pobiega2y ago
but your error is in
Pobiega
Pobiega2y ago
thats the Add method, and the very first line
TheRanger
TheRanger2y ago
so when u click add, it adds to the database, then the error pops up?
Crayv
CrayvOP2y ago
yup
Pobiega
Pobiega2y ago
Are you doubleclicking it or something? so it adds, wipes the field, runs again..
Crayv
CrayvOP2y ago
it doesn't run again it close
Pobiega
Pobiega2y ago
that sounds exactly like whats happening
TheRanger
TheRanger2y ago
can u show the ClearText method?
Crayv
CrayvOP2y ago
wait i don't know how to explain because i have 2 class
Crayv
CrayvOP2y ago
Crayv
CrayvOP2y ago
i just do this for cleaner looks
TheRanger
TheRanger2y ago
well it definitely sounds like ur double clicking it
Pobiega
Pobiega2y ago
the fix is easy thou use int.TryParse instead of Convert.ToInt32 any place where you are getting a string from the user, use tryparse the user can not be trusted the user can enter "moofla" moofla is not a number
Crayv
CrayvOP2y ago
TheRanger
TheRanger2y ago
$tryparse
MODiX
MODiX2y ago
The TryParse pattern is considered best practice of parsing data from a string: - a TryParse method returns true or false to inform you if it succeeded or not, so you can use it directly in a condition, - since C# 7 you can declare a variable that will be used as an out argument inline in an argument list, - it forces you to check if the out argument contains valid data afterwards, Avoid: Convert.ToInt32 — it's a bad choice for parsing an int. It exists only for backwards compatibility reasons and should be considered last resort.
return Convert.ToInt32(null); //returns 0. null should not be considered as 0 ever
return Convert.ToInt32("asdf"); //throws FormatException
return Convert.ToInt32(null); //returns 0. null should not be considered as 0 ever
return Convert.ToInt32("asdf"); //throws FormatException
(Note: Convert does contain useful conversion methods: To/FromBase64String, To/FromHexString, ToString(X value, int toBase), ToX(string? value, int fromBase)) Avoid: int.Parse — you have to use a try/catch statement to handle invalid input, which is a less clean solution.
var number = int.Parse("abc"); //throws FormatException
var number = int.Parse(""); //throws FormatException
var number = int.Parse("abc"); //throws FormatException
var number = int.Parse(""); //throws FormatException
Use int.TryParse https://docs.microsoft.com/en-us/dotnet/api/system.int32.tryparse?view=net-5.0#System_Int32_TryParse_System_String_System_Int32__
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
Int32.TryParse Method (System)
Converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the operation succeeded.
Pobiega
Pobiega2y ago
what does int.TryParse return? and how many arguments does it take?
Crayv
CrayvOP2y ago
wait what what do i do
Pobiega
Pobiega2y ago
start with answering my two questions the act of answering them should give you new insight
Crayv
CrayvOP2y ago
so int.tryparse converts it into an int if the textbox contains only an integer?
Pobiega
Pobiega2y ago
something like that. answer the questions please.
Crayv
CrayvOP2y ago
okay the int.tryparse returns a bool value
Pobiega
Pobiega2y ago
It does!
Crayv
CrayvOP2y ago
yes then the 2nd question is 2 arguments?
Pobiega
Pobiega2y ago
yes!
Crayv
CrayvOP2y ago
so i have to use if else?
Pobiega
Pobiega2y ago
so it returns a bool, and it takes (string input, out int number) that sounds like a good idea, yep
Crayv
CrayvOP2y ago
oh okay let me try so why does the debugger counts my textbox as an empty string?
Pobiega
Pobiega2y ago
look at the bot message above in this thread because your textbox has an empty string when the click is invoked likely because of a doubleclick or some other reason the button handler is invoking twice
Crayv
CrayvOP2y ago
ohh i have this code where if i click in the datagridview
TheRanger
TheRanger2y ago
invoking twice, makes sense
Crayv
CrayvOP2y ago
Pobiega
Pobiega2y ago
¯\_(ツ)_/¯ could be
Crayv
CrayvOP2y ago
well sht anyways let me try it how do i use the out int number?
Pobiega
Pobiega2y ago
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
if (int.TryParse(someInput, out var result))
{
Console.WriteLine($"Thanks for giving me the following number: {result}!");
}
else
{
Console.WriteLine("You didn't give me a valid number :c");
}
Crayv
CrayvOP2y ago
is this correct?
Crayv
CrayvOP2y ago
Pobiega
Pobiega2y ago
almost I'd recommend giving it a new local variable as your out and that assignment in the if is... not correct
Crayv
CrayvOP2y ago
ahhh okayokay i'll try to finish it tomorrow i'm sleepy thanks for the help!
Want results from more Discord servers?
Add your server