473,387 Members | 1,553 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

noob sql C# problem

20
hi. i'm pretty much a noob to c# and visual studio and am trying to create a simple login method. i have created a users table in the database and can add users by inputing there md5 encrypted password and a non encrypted username. problem is then checking a user has inputed valid username and passwords. i've created a stored procedure that returns 1 for a valid login and 0 for invalid but don't know if this procedure is the problem
Expand|Select|Wrap|Line Numbers
  1. PROCEDURE validlogin
  2.         @uname nchar(30),
  3.         @pass nchar(100),
  4.         @valid int        OUTPUT
  5. AS
  6.     IF EXISTS(SELECT * FROM Users WHERE User_name = @uname AND Password = @pass)
  7.         SELECT @valid = 1
  8.     ELSE
  9.         SELECT @valid = 0
  10.     RETURN @valid
  11.  
this next method then takes the data from the login page and runs the stored procedure after this it returns true or false to another method which will handle what to do next.

Expand|Select|Wrap|Line Numbers
  1. private bool UserAuthenticate(string uname, string pass)
  2.         {
  3.             int validateresult;
  4.             string hashedpwd = EncodePassword(pass);
  5.             //query the username and password
  6.             sqlSelectCommand1.CommandType = CommandType.StoredProcedure;
  7.             sqlSelectCommand1.CommandText = "validlogin";
  8.             sqlSelectCommand1.Parameters.Add(new SqlParameter("@uname", SqlDbType.NChar, 30));
  9.             sqlSelectCommand1.Parameters["@uname"].Value = uname;
  10.             sqlSelectCommand1.Parameters.Add(new SqlParameter("@pass", SqlDbType.NChar, 30));
  11.             sqlSelectCommand1.Parameters["@pass"].Value = hashedpwd;
  12.             sqlSelectCommand1.Parameters.Add(new SqlParameter("@valid", SqlDbType.NChar, 30));
  13.             sqlSelectCommand1.Parameters["@valid"].Direction = ParameterDirection.Output;
  14.             //sqlDataAdapter1.SelectCommand = sqlSelectCommand1;
  15.             sqlConnection1.Open();
  16.  
  17.             validateresult = Convert.ToInt32(sqlSelectCommand1.ExecuteScalar());
  18.             sqlConnection1.Close();
  19.             if (validateresult == 1)
  20.             {
  21.                 return true;
  22.             }
  23.             else
  24.             {
  25.                 return false;
  26.             }
  27. }
any help would be much appreciated
Aug 30 '08 #1
2 1268
pinman
20
god only knows why but the code seems to now work after i took a break and re-ran the program. one thing i did change was change SELECT @valid = 1 to SET @valid = 1 in the stored procedure, but i then changed it back and it worked both ways. so the result from the stored procedure is 0 or 1 depending if the correct data has been entered. next question is where next. i think i have options of creating a cookie on users machine or using session variables? any other ways to proceed? any way that code could be improved? (including how to put in error handling)
thanks in advance
Aug 30 '08 #2
Curtis Rutland
3,256 Expert 2GB
Well, just some pointers on making it cleaner. You can use SqlCommand.Parameters.AddWithValue(string param, object value) instead of Add, which should condense things a bit. Also, you should be able to cast the return value of ExecuteScalar as a bool rather than an int.

Just little things. Your way isn't wrong, just different than mine.

Also, please use [code] tags when posting code. You can use the # symbol in the text editor.

Mod.
Aug 30 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

7
by: administrata | last post by:
Is it possible? I tried... I = "John" print \ """ I used to love pizza"""
10
by: Matt Hollingsworth | last post by:
Hello, Very new to python, so a noob question. When I've written stuff in JavaScript or MEL in the past, I've always adopted the variable naming convention of using a $ as the first character...
8
by: Ivan Shevanski | last post by:
Alright heres another noob question for everyone. Alright, say I have a menu like this. print "1. . .Start" print "2. . .End" choice1 = raw_input("> ") and then I had this to determine what...
0
by: AndyW | last post by:
Hey folks. I am trying to get a soap wsdl service working and have a bit of a noob php programming question for it. I'm using PHP 5.x btw. I have written a soap server that contains a...
2
by: Link360 | last post by:
Im a complete noob and im proud of it. I am excited in learning everything about the C++ language. Right now im trying to make tic-tac-toe. Go ahead laugh. here is what i have so far ...
4
by: jobs | last post by:
Three noob questions. Please help. 1. Is there a way to have arguments on subroutine that are optional? 2. Say I need to convert datetime to string..in this format : 2006-09-07...
5
by: Milan Krejci | last post by:
the thing is that descentant branches i dont want to expand do expand. $id variable contains an array of branches i want the program to go through (alcohol's id -beer id etc) function...
6
by: Lang Murphy | last post by:
I'm baaaaack... some of you answered a question I had last week. Only problem is: I'm a dope who doesn't understand most of what y'all posted. Raw noob when it comes to .Net and C#. So I'm going...
4
by: Arch Stanton | last post by:
I'm trying to bind data in a dataset (obtained from an Access DB) to a listbox in ASP.net. I know my dataset is being created properly because it displays fine in a datagrid, but I can't get it to...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.