Click here to Skip to main content
15,899,935 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
I have a simple query which is :

SQL
SELECT fullname FROM usersAccount WHERE username = @usr AND passwrd = @pwd;


Does It required to used ParameterDirection.Input for better Query ? Am just curious about using ParameterDirection on non-stored Query if theirs some sort of Improvement/Coding Quality.

SQL
cmd.Parameters.Add("@usr", SqlDbType.VarChar, 20, ParameterDirection.Input).Value = Me.txtUserName.Text
cmd.Parameters.Add("@pw", SqlDbType.BigInt, 15, ParameterDirection.Input).Value = Me.txtPassword.Text</pre>
Posted
Updated 24-Sep-13 8:38am
v3

1 solution

No. The easiest way to do this is just to say:
VB
cmd.Parameters.AddWithValue("@usr", txtUserName.Text)
cmd.Parameters.AddWithValue("@pw", txtPassword.Text)


But please don't do that! Storing passwords in clear text is a horrible security risk. See here: Password Storage: How to do it.[^] - it's in C#, but the code is pretty much identical in VB.


"I thought that using

VB
cmd.Parameter.add("pwd",SqlDbType.BigInt, 15).Value

is better than AddWithValue due to it can strict the Type/Length of the Input"

It can, yes. And that's a problem!

When you use the Add method with a text string and specify the length, the Add method truncates the string to the length you specify, without telling you. So if you specify a string length of 20 characters and your user enters a 100 character comment, 80 characters will be discarded and you don't know. Worse - your user doesn't know and is instead assured that the database was updated correctly.
If you use AddWithValue, the whole string is passed through, and SQL will refuse to enter it and throw an exception back to your code - so you can tell the user there is a problem!

There is also the fun and games of changes to the DB needing to be reflected in the code and so forth, but throwing away user data without anyone knowing is much, much worse - because it is unfixable (the data can't be recovered) and that erodes user confidence in the system as a whole.

And still don't save passwords in clear text!
 
Share this answer
 
v2
Comments
iMaker.ph 24-Sep-13 17:56pm    
I thought that using

cmd.Parameter.add("pwd",SqlDbType.BigInt, 15).Value is better than AddWithValue due to it can strict the Type/Length of the Input
OriginalGriff 25-Sep-13 5:27am    
Answer updated.
iMaker.ph 25-Sep-13 20:29pm    
What I just did with that scenario is copy the length of the Field username & passwrd to the Length of my Parameter. Well that be enough?
OriginalGriff 26-Sep-13 5:29am    
That doesn't stop the problem: you throw away the users data without telling them.
That means in three months time when they go back to look at it...they can't get it all back. At that point there is a real problem because the users no longer trust the data in your system - and by extension they don't trust the system, or you either!

Much, much better to say "this didn't fit" by catching an error and let the user know, before it becomes irrevocable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900