473,657 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

noob sql C# problem

20 New Member
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 1293
pinman
20 New Member
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 Recognized Expert Specialist
Well, just some pointers on making it cleaner. You can use SqlCommand.Para meters.AddWithV alue(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
1743
by: administrata | last post by:
Is it possible? I tried... I = "John" print \ """ I used to love pizza"""
10
2112
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 (no, I don't use perl, never have). Not possible in python. What are some good options that people commonly use so that they can easily and quickly identify variable with just a glance? When I was writing stuff in SoftImage XSI/JScript, many of...
8
2140
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 option.
0
1480
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 single method in the form: SayHelloWorld( $name ) { return "Hello " .$name; };
2
2850
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 #include<iostream> #include<stdlib.h> using namespace std; int main() {int h,h2,tmp; h=10;
4
1212
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 23:00:00.000
5
1613
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 tree_list($parent, $level,$id) { // retrieve all children of $parent $result = mysql_query('SELECT cname,cid FROM kategorie '. 'WHERE parent="'.$parent.'";'); while ($row = mysql_fetch_array($result)) {
6
1625
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 to be more specific... All I need to do at the moment is figure out how to replace our usage of .ini files as configuration files for our small, utility type apps. I'm not looking to use XML as a data stream or anything like that. Not right now,...
4
5097
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 display in a listbox. Here's what I'm trying: dgrDataGrid.DataSource = dstData.Tables("Manufacturer") lstMake.datasource = dstData.Tables("Manufacturer") Page.DataBind There is only one column in the dataset's table, and the datagrid
0
8420
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8617
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4330
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.