Sign In | Register Now About Bytes | Help | Site Map
Connecting Tech Pros Worldwide

validate entries using an sql table

Question posted by: BlackEye (Newbie) on July 23rd, 2008 08:49 AM
Hi all!

i want to validate the user name and password using an sql table i created.
i have tried the following code:

Expand|Select|Wrap|Line Numbers
  1. SqlConnection myConnection = new SqlConnection("data source=myPC;database=myDB;uid=abc;password=abc123");
  2.  
  3.         DataSet dsFillData = new DataSet();
  4.  
  5.         SqlCommand cmdobj = new SqlCommand("Select * from tbl_LoginIDsets", myConnection);
  6.  
  7.         cmdobj.CommandType = CommandType.Text;
  8.  
  9.  
  10.         SqlDataAdapter daAdapter = new SqlDataAdapter(cmdobj);
  11.  
  12.  
  13.         daAdapter.Fill(dsFillData);
  14.  
  15.         SqlDataReader dr = null;
  16.  
  17.         myConnection.Open();
  18.         dr = cmdobj.ExecuteReader();
  19.                 if(dr.Read())
  20.                 {
  21.                     Response.Write("Valid User");
  22.                 }
  23.                 else
  24.                 {
  25.                     Response.Write("Invalid User");
  26.                 }
  27.  
  28.     }
  29. }


but my code seems to validate even the incorrect entries and also the fonts get enlarged on postback. can any body pls tell me what is the problem with this code? i'll be greatfull..
debasisdas's Avatar
debasisdas
Moderator
6,576 Posts
July 23rd, 2008
08:59 AM
#2

Re: validate entries using an sql table
try to use

Select count(*) from tbl_LoginIDsets where user_name='username' and password='password'

if this query returns 1 then proceed further and login else reprompt for the correct username and password.

Reply
r035198x's Avatar
r035198x
Administrator
10,754 Posts
July 23rd, 2008
09:17 AM
#3

Re: validate entries using an sql table
You didn't check for the entered username and password.

Reply
BlackEye's Avatar
BlackEye
Newbie
13 Posts
July 24th, 2008
07:19 AM
#4

Re: validate entries using an sql table
Quote:
try to use

Select count(*) from tbl_LoginIDsets where user_name='username' and password='password'

if this query returns 1 then proceed further and login else reprompt for the correct username and password.



i have now tried the following code:


Expand|Select|Wrap|Line Numbers
  1.         SqlConnection myConnection = new SqlConnection("data source=myPC;database=myDB;uid=abc;password=abc123");
  2.  
  3.         myConnection.Open();
  4.  
  5.         Response.Write(ConnectionState.Open);
  6.  
  7.         DataSet dsFillData = new DataSet();
  8.  
  9.         SqlCommand cmdobj = new SqlCommand("Select * from tbl_LoginIDsets", myConnection);
  10.  
  11.         cmdobj.CommandType = CommandType.Text;
  12.  
  13.  
  14.         SqlDataAdapter daAdapter = new SqlDataAdapter(cmdobj);
  15.  
  16.  
  17.         daAdapter.Fill(dsFillData);
  18.  
  19.         SqlDataReader dr = null;
  20.  
  21.         SqlCommand cmd = new SqlCommand("Select * from tbl_LoginIDsets where LoginID = " +UserName +"and Password = " +Password, myConnection);
  22.  
  23.  
  24.         dr = cmd.ExecuteReader();
  25.                 if(dr.Read())
  26.                 {
  27.                     Response.Write("Valid User");
  28.                 }
  29.                 else
  30.                 {
  31.                     Response.Write("Invalid User");
  32.                 }
  33.  
  34.     }
  35. }



the command which i have underlined is giving the following error:
[Incorrect syntax near 'Password'.]

can you pls guide me where am i going wrong now? i'll be greatful.

Reply
r035198x's Avatar
r035198x
Administrator
10,754 Posts
July 24th, 2008
07:43 AM
#5

Re: validate entries using an sql table
You are missing some spaces e.g before "and Password = " needs to be " and Password = " ...
You need quotes around varchar type values.

Reply
BlackEye's Avatar
BlackEye
Newbie
13 Posts
July 24th, 2008
09:09 AM
#6

Re: validate entries using an sql table
Quote:
You are missing some spaces e.g before "and Password = " needs to be " and Password = " ...
You need quotes around varchar type values.


i have iserted the spaces u suggested. the error hsa now changed to:
[The multi-part identifier "System.Web.UI.WebControls.TextBox" could not be bound.]
the error is still on the same line.

Reply
r035198x's Avatar
r035198x
Administrator
10,754 Posts
July 24th, 2008
09:26 AM
#7

Re: validate entries using an sql table
Quote:
i have iserted the spaces u suggested. the error hsa now changed to:
[The multi-part identifier "System.Web.UI.WebControls.TextBox" could not be bound.]
the error is still on the same line.

In your code, where are Password and UserName values coming from?

Reply
BlackEye's Avatar
BlackEye
Newbie
13 Posts
July 24th, 2008
09:49 AM
#8

Re: validate entries using an sql table
Quote:
In your code, where are Password and UserName values coming from?


the user and password values are entered by the user who wants to log in.
these values are to be validated from existing values in a table with feilds LoginID and Password

Reply
r035198x's Avatar
r035198x
Administrator
10,754 Posts
July 24th, 2008
10:02 AM
#9

Re: validate entries using an sql table
Post the full code that you used ...

Reply
BlackEye's Avatar
BlackEye
Newbie
13 Posts
July 25th, 2008
06:26 AM
#10

Re: validate entries using an sql table
Quote:
Post the full code that you used ...


this is my complete code:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.Data.SqlClient;
  11.  
  12. public partial class _Default : System.Web.UI.Page 
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.  
  17.     }
  18.  
  19.     protected void LoginButton_Click(object sender, EventArgs e)
  20.     {
  21.         SqlConnection myConnection = new SqlConnection("data source=myPC;database=myDB;uid=abc;password=abc123");
  22.  
  23.         myConnection.Open();
  24.  
  25.         Response.Write(ConnectionState.Open);
  26.  
  27.         DataSet dsFillData = new DataSet();
  28.  
  29.         SqlCommand cmdobj = new SqlCommand("Select * from tbl_LoginIDsets", myConnection);
  30.  
  31.         cmdobj.CommandType = CommandType.Text;
  32.  
  33.  
  34.         SqlDataAdapter daAdapter = new SqlDataAdapter(cmdobj);
  35.  
  36.  
  37.         daAdapter.Fill(dsFillData);
  38.  
  39.         SqlDataReader dr = null;
  40.  
  41.         SqlCommand cmd = new SqlCommand("Select * from tbl_LoginIDsets where LoginID = " +UserName +" and Password = " +Password, myConnection);
  42.  
  43.  
  44.         dr = cmd.ExecuteReader();
  45.                 if(dr.Read())
  46.                 {
  47.                     Response.Write("Valid User");
  48.                 }
  49.                 else
  50.                 {
  51.                     Response.Write("Invalid User");
  52.                 }
  53.  
  54.     }
  55. }


could there be a problem in my connection? i'm just guessing though, because if i try to display my connection status it does say "open".

Reply
r035198x's Avatar
r035198x
Administrator
10,754 Posts
July 25th, 2008
06:54 AM
#11

Re: validate entries using an sql table
Quote:
...

Expand|Select|Wrap|Line Numbers
  1.         DataSet dsFillData = new DataSet();
  2. SqlCommand cmdobj = new SqlCommand("Select * from tbl_LoginIDsets", myConnection);
  3. cmdobj.CommandType = CommandType.Text;
  4. SqlDataAdapter daAdapter = new SqlDataAdapter(cmdobj);
  5. daAdapter.Fill(dsFillData);
  6.  



1.) What is the code above doing in there?
2.) What are the names of the controls where the user enters the username and password?

Reply
BlackEye's Avatar
BlackEye
Newbie
13 Posts
July 25th, 2008
07:17 AM
#12

Re: validate entries using an sql table
Quote:
1.) What is the code above doing in there?
2.) What are the names of the controls where the user enters the username and password?


1.) this code is supposed to fetch data from the table and fill the data set with this data using a data adapter (at least that is what i aimed to do, let me know if i am wrong)

2.) the user enters username and password into textboxes whose ID's are UserName and Password respectively

Reply
r035198x's Avatar
r035198x
Administrator
10,754 Posts
July 25th, 2008
07:32 AM
#13

Re: validate entries using an sql table
Quote:
2.) the user enters username and password into textboxes whose ID's are UserName and Password respectively

Then to get the text entered you need to use UserName.Text and Password.Text not just UserName or Password.

Reply
BlackEye's Avatar
BlackEye
Newbie
13 Posts
July 25th, 2008
07:52 AM
#14

Re: validate entries using an sql table
Quote:
Then to get the text entered you need to use UserName.Text and Password.Text not just UserName or Password.


i tried it.
but the only values it is now validating are the column names. if i enter any other values, even those which are present in my table, the debbuging stops and it gives me the following error:
[Invalid column name 'master'.
Invalid column name 'master'.]

both username=master and password=master should be valid entries according to my table.

Reply
r035198x's Avatar
r035198x
Administrator
10,754 Posts
July 25th, 2008
08:01 AM
#15

Re: validate entries using an sql table
Quote:
i tried it.
but the only values it is now validating are the column names. if i enter any other values, even those which are present in my table, the debbuging stops and it gives me the following error:
[Invalid column name 'master'.
Invalid column name 'master'.]

both username=master and password=master should be valid entries according to my table.

Character datatypes also need to have quotes around them.
e.g
Expand|Select|Wrap|Line Numbers
  1. " where name = '" +name.Text + "'";

Reply
BlackEye's Avatar
BlackEye
Newbie
13 Posts
July 25th, 2008
08:08 AM
#16

Re: validate entries using an sql table
Quote:
Character datatypes also need to have quotes around them.
e.g
Expand|Select|Wrap|Line Numbers
  1. " where name = '" +name.Text + "'";



it worked! It worked!! IT WORKED!!! Thank You!!!!

Reply
r035198x's Avatar
r035198x
Administrator
10,754 Posts
July 25th, 2008
08:19 AM
#17

Re: validate entries using an sql table
Quote:
it worked! It worked!! IT WORKED!!! Thank You!!!!

Actually you did most of the work. Good luck with rest of it.

Reply
Reply
Not the answer you were looking for? Post your question . . .
189,759 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Latest Articles: Read & Comment
Top .NET Forum Contributors