![]() |
|
[C#] Code is not properly catching errors. - Printable Version +- Hack Forums (https://hackforums.net) +-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444) +--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151) +---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118) +---- Thread: [C#] Code is not properly catching errors. (/showthread.php?tid=5679598) |
Code is not properly catching errors. - SIM-888979 - Jul 27, 2017 Here is a simple login page that I made, it has 2 catch function: 1. If username (email) is not found, emailFail.Visible = true; (notification stating that email is not found) 2. If password is invalid, loginFail.Visible = true; (notification stating that password is wrong) However, at this moment, even if the email is in the database and the password is wrong, it still shows emailFail.Visible = true instead of loginFail.Visible = true; Any ideas? Code: https://pastebin.com/cSUkDHH7 * - Sommmen - Jul 27, 2017 functions that return booleans don't need to be compared. If(emailFail.Visible) {... } is cleaner. (not the problem but is good practice, looking at the code now) a) refactor your code to several methods so each method can be easier verified/tested. In this case for example a comepareHash function. this also improves on readability b) you only need one connection to the database, and you can reuse this connection to output several commands. (https://stackoverflow.com/questions/13677318/how-to-run-multiple-sql-commands-in-a-single-sql-connection) c) read through this quickly: https://docs.microsoft.com/en-us/dotnet/csharp/programming-guide/inside-a-program/coding-conventions and this: https://docs.microsoft.com/en-us/dotnet/standard/design-guidelines/general-naming-conventions (dont use hungarian notation) now the problem is hard to see for me, but the first i would change is this: Code while (getUserIdRDR.Read() == true)to: Code getUserIdRDR.Read()Also, why are you not comparing the values in the database? you're requesting all customers and then going through them 1 by 1 to check. why not do Code 'SELECT Email, Pass FROM customer WHERE email = "' + inputEmail +'" AND pass = "' + hashedPassword + '"';Here's an example of how i would go about this: Code string hash(string s)* - SIM-888979 - Jul 27, 2017 (Jul 27, 2017 09:20 AM)Sommmen Wrote: functions that return booleans don't need to be compared. I changed while (getUserIdRDR.Read() == true) to while(getUserIdRDR.Read()). Is that what you mean? It still shows emailFail.visible when it should be loginFail.visible. * - Sommmen - Jul 27, 2017 Updat the answer with some pseudo code, check that out :) kinda hard to help you without debugging :(. * - SIM-888979 - Jul 27, 2017 (Jul 27, 2017 10:23 AM)Sommmen Wrote: Updat the answer with some pseudo code, check that out :) I think I found the problem. email_textbox.Text == (string)rdr["cEmail"] is not true in all situations for some reason. Any ideas why? * - Sommmen - Jul 27, 2017 (Jul 27, 2017 10:45 AM)Aurora Wrote: I think I found the problem. trim your input, and also take a lookg at string.Compare(). (case sensitivity?) inputText.trim() == (string) rdr... or, even better try: reader.getString(column) instead. The cast might ruin it. also be sure to print both variables so you can validate in console. * - 0xDEAD10CC - Jul 27, 2017 (Jul 27, 2017 11:21 AM)Sommmen Wrote: trim your input, and also take a lookg at string.Compare(). (case sensitivity?) FYI- thisIsNotPascalCase... ThisIsPascalCase. thisIsCamelCase... ThisIsNotCamelCase. I think you have your styles mixed up, and although C# uses both, it does not use camelCase for function names. You should be reviewing .NET naming conventions too. C# is not Java. Also, you do not need string.Compare() for case sensitivity and locale. You can use string.Equals() which is the preferred method for string equality checking. Instead of using the console, why not just breakpoint at that location and not having to modify and recompile, then debug your code that way? * - Rageline - Jul 27, 2017 Any specific reason you are not using ASP.NET Core? * - Ninja - Jul 27, 2017 You're making $1000 a week, right? Just hire someone to do it for you. * - SIM-888979 - Jul 27, 2017 (Jul 27, 2017 08:14 PM)Ninja Wrote: You're making $1000 a week, right? Just hire someone to do it for you. Not sure why that matters, but I did contact you regarding this project (this part is a small part of the entire project) ages ago.
* - Ninja - Jul 27, 2017 (Jul 27, 2017 08:31 PM)Aurora Wrote: Not sure why that matters, but I did contact you regarding this project (this part is a small part of the entire project) ages ago. I know, but that doesn't change what I said. Also, you should look outside HF. Go to websites like Upwork or Freelancer. |