473,407 Members | 2,306 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,407 software developers and data experts.

ADO.NET Simple returning a single row (newbie)

bencoding
Hello, I'm a newbie to the .NET programming and need some help.

I am creating a registration page and want to be able to take the username and password that the user entered and check to the the database to make sure it doesn't already exist. If it does I want to return an error Response.Write() message and if it doesnt then I will continue with my other code. I am very new to this and it's hard finding some support forums online that are what I need.

This is what I have so far:

Expand|Select|Wrap|Line Numbers
  1.  SqlConnection myConn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Program Files\\MyProject\\App_Data\\Database.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True");
  2.  
  3. SqlCommand cmdCheck = new SqlCommand("SELECT 1 AS [EXIST] FROM Users WHERE Email = '" + email.Text + "' AND IsActive = 'True'", myConn);
Can someone give me the code I would need to check that table to see there is already an email address that is active = True and return a message if it is there.

Thanks
Jun 17 '08 #1
5 1739
gpraghuram
1,275 Expert 1GB
This should be moved to the .NET forum

Raghu
Jun 18 '08 #2
r035198x
13,262 8TB
What specifically do you need help with now? Does your code run as expected?

P.S Moved to .NET forum.
Jun 18 '08 #3
I suggest you make a new field in the table with BIT data type. Set the BIT to 1 when user is already logged on. And just retrieve it for already logged in user or new user. If it gives 1 for X user then X cannot log in again coz its already logged in. If it gives 0 for X , X can log in now.

I hope i am clear.
Now as for the code, you can do it yourself its not that tough.
Jun 18 '08 #4
If u want to just check that whether that userid or email address exists or not in the table, then u can return a boolean value from your query. Or u can also use output variable in case you are using a stored procedure.
eg.
take an output variable @intErrorOutput, and then set the variable value in database, and then place the check on the basis of output variable value.
------------------------------------
@intErrorOutput INT OUTPUT

IF EXISTS
(
SELECT * FROM TableName
WHERE user = @userValue
)
BEGIN
SET @intErrorOutput = 1
RETURN
END
else
SET @intErrorOutput = 0
Jun 18 '08 #5
Thanks for your help guys, I ended up figuring it out and did something different, so as I mentioned this is a register page and I need to check the database to make sure that the email address is not already in the database and in an active account. So I ended up using the ExecuteScalar(); which returns the first value of the first column, and then put in a if != null condition. Here is my code, feel free to critique and let me know what I could of done better. thx


Expand|Select|Wrap|Line Numbers
  1.     protected void Button1_Click(object sender, EventArgs e)
  2.     {
  3.         try
  4.         {
  5.             SqlConnection myConn = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Program Files\\ContractorClientManager\\App_Data\\Database.mdf\";Integrated Security=True;Connect Timeout=30;User Instance=True");
  6.             SqlCommand cmdCheck = new SqlCommand("SELECT 1 FROM Users WHERE Email = '" + email.Text + "' AND IsActive = 'True'", myConn);
  7.             SqlCommand cmdInsert = new SqlCommand("INSERT Users (Email,Password) VALUES ('" + email.Text + "','" + password.Text + "')", myConn);
  8.  
  9.             myConn.Open();
  10.  
  11.             if (cmdCheck.ExecuteScalar() != null)
  12.             {
  13.                 error_header.Text = "This account already exists click <a href=\"LoginRetrieval.aspx\">HERE</a>  to retrieve password.</a>";
  14.             }
  15.             else
  16.             {
  17.                 cmdInsert.ExecuteNonQuery();
  18.                 Response.Redirect("Login.aspx?msg=user_new");
  19.             }
  20.  
  21.             myConn.Dispose();
  22.         }
  23.         catch
  24.         {
  25.             Response.Write(e.ToString());
  26.         }
  27.  
  28.  
  29.     }
Jun 18 '08 #6

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

Similar topics

20
by: Ken Godee | last post by:
module1 calls a function in module2 module2 starts a thread that calls a function in module3 and then returns to module1 thread finishes and I need the return value from the thread to use in...
27
by: Brian Sabbey | last post by:
Here is a first draft of a PEP for thunks. Please let me know what you think. If there is a positive response, I will create a real PEP. I made a patch that implements thunks as described here....
1
by: Randell D. | last post by:
HELP! I am determined to stick with this... I'm getting there... for those who haven't read my earlier posts, I'm createing what should be a simple function that I can call to check that...
7
by: BrianJones | last post by:
Hi, if you have a function, how is it possible to return an array? E.g.: unsigned long function(...) // what I want to do, obviously illegal I do know such would be possible by using a dynamic...
6
by: KevinD | last post by:
assumption: I am new to C and old to COBOL I have been reading a lot (self teaching) but something is not sinking in with respect to reading a simple file - one record at a time. Using C, I am...
2
by: Nathan Sokalski | last post by:
I am using a System.IO.StreamReader to read a text file that was created with Notepad. It is returning everything except the single quotes (at least that is the only thing I have noticed it is not...
27
by: one man army | last post by:
Hi All- I am new to PHP. I found FAQTS and the php manual. I am trying this sequence, but getting 'no zip string found:'... PHP Version 4.4.0 $doc = new DomDocument; $res =...
13
by: Karl Groves | last post by:
I'm missing something very obvious, but it is getting late and I've stared at it too long. TIA for responses I am writing a basic function (listed at the bottom of this post) that returns...
26
by: optimistx | last post by:
A variable in global scope var a1 = 'contents of global variable a1'; can be references (with some limitations) as window; // or window.a1; // or even window;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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,...
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...
0
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,...

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.