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
and here is the database Table
thank you for helping
I also tried the Convert.ToInt32 one it's the smae
hover ur mouse on
txtSupplierID.Text
while the error message is displayed to see its value
@Crayvwdynm hover?
like put ur mouse over it
oh
That's too large for a 16 bit integer.
Nononon that's not updated
its Convert.ToInt32 one
hehe
TheRanger#3357
REPL Result: Success
Result: int
Compile: 641.765ms | Execution: 38.729ms | React with ❌ to remove this embed.
You may need to call
Trim
on the string if it contains a space.
Also, you should look into int.TryParse
.lets just see its value first
hover ur mouse on
txtSupplierID.Text
while the error message is displayed to see its valuei can't over because the error will appear
hover
you can
just put ur mouse there
like this?
or do i not run it
on
txtSupplierID.Text
in ur visual studiosomething like this should appear
empty
it's a textbox
hover ur mouse on that circle
a small window like this will pop up
while the System.FormatException is popped up
u can only see its value when the program is crashed
okay what does that mean?
when you see this error
hover ur mouse on the circle
okok wait
its empty
u cant convert an empty string to an integer
it's not empty tho...
i put in the value and then add it in the database
so it's not empty
hmm, maybe wrong textbox?
i don't think so
it's working for everything except the supplier
well go to ur designer mode
click on the supplier id textbox
see what's it called
what's called?
the textbox
it's called txtSupplierID
i have to see the proof
screenshot or anything
is this correct?
thats not the issue, no need to post it
okayokay i just thought maybe
it's my first time creating a 3 layer application
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 ishmmm
what about the rest of the textboxes? try hovering on them
but it's not empty :((
the debugger doesnt agree with you
and I trust the debugger 🙂
well that's true
yeah do what Ranger suggested, make sure they have values as expected
wait check this out
i'm gonna add this to the database
it's successfully got added but got the error afterwards
lemme check
but your error is in
thats the Add method, and the very first line
so when u click add, it adds to the database, then the error pops up?
yup
Are you doubleclicking it or something?
so it adds, wipes the field, runs again..
it doesn't run again it close
that sounds exactly like whats happening
can u show the
ClearText
method?wait i don't know how to explain
because i have 2 class
i just do this for cleaner looks
well it definitely sounds like ur double clicking it
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$tryparse
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. (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.
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__ 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.
what does
int.TryParse
return?
and how many arguments does it take?wait what
what do i do
start with answering my two questions
the act of answering them should give you new insight
so int.tryparse converts it into an int if the textbox contains only an integer?
something like that. answer the questions please.
okay the int.tryparse returns a bool value
It does!
yes
then the 2nd question is 2 arguments?
yes!
so i have to use if else?
so it returns a bool, and it takes
(string input, out int number)
that sounds like a good idea, yepoh okay let me try
so why does the debugger counts my textbox as an empty string?
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
ohh
i have this code where if i click in the datagridview
invoking twice, makes sense
¯\_(ツ)_/¯
could be
well sht
anyways let me try it
how do i use the out int number?
is this correct?
almost
I'd recommend giving it a new local variable as your
out
and that assignment in the if is... not correctahhh okayokay
i'll try to finish it tomorrow
i'm sleepy thanks for the help!