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:
-
SqlConnection myConnection = new SqlConnection("data source=myPC;database=myDB;uid=abc;password=abc123");
-
-
DataSet dsFillData = new DataSet();
-
-
SqlCommand cmdobj = new SqlCommand("Select * from tbl_LoginIDsets", myConnection);
-
-
cmdobj.CommandType = CommandType.Text;
-
-
-
SqlDataAdapter daAdapter = new SqlDataAdapter(cmdobj);
-
-
-
daAdapter.Fill(dsFillData);
-
-
SqlDataReader dr = null;
-
-
myConnection.Open();
-
dr = cmdobj.ExecuteReader();
-
if(dr.Read())
-
{
-
Response.Write("Valid User");
-
}
-
else
-
{
-
Response.Write("Invalid User");
-
}
-
-
}
-
}
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..
|
|
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.
|
|
July 23rd, 2008 09:17 AM
# 3
|
Re: validate entries using an sql table
You didn't check for the entered username and password.
|
|
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:
- SqlConnection myConnection = new SqlConnection("data source=myPC;database=myDB;uid=abc;password=abc123");
-
-
myConnection.Open();
-
-
Response.Write(ConnectionState.Open);
-
-
DataSet dsFillData = new DataSet();
-
-
SqlCommand cmdobj = new SqlCommand("Select * from tbl_LoginIDsets", myConnection);
-
-
cmdobj.CommandType = CommandType.Text;
-
-
-
SqlDataAdapter daAdapter = new SqlDataAdapter(cmdobj);
-
-
-
daAdapter.Fill(dsFillData);
-
-
SqlDataReader dr = null;
-
-
SqlCommand cmd = new SqlCommand("Select * from tbl_LoginIDsets where LoginID = " +UserName +"and Password = " +Password, myConnection);
-
-
-
dr = cmd.ExecuteReader();
-
if(dr.Read())
-
{
-
Response.Write("Valid User");
-
}
-
else
-
{
-
Response.Write("Invalid User");
-
}
-
-
}
-
}
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.
|
|
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.
|
|
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.
|
|
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?
|
|
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
|
|
July 24th, 2008 10:02 AM
# 9
|
Re: validate entries using an sql table
Post the full code that you used ...
|
|
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:
- using System;
-
using System.Data;
-
using System.Configuration;
-
using System.Web;
-
using System.Web.Security;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
using System.Web.UI.WebControls.WebParts;
-
using System.Web.UI.HtmlControls;
-
using System.Data.SqlClient;
-
-
public partial class _Default : System.Web.UI.Page
-
{
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
-
}
-
-
protected void LoginButton_Click(object sender, EventArgs e)
-
{
-
SqlConnection myConnection = new SqlConnection("data source=myPC;database=myDB;uid=abc;password=abc123");
-
-
myConnection.Open();
-
-
Response.Write(ConnectionState.Open);
-
-
DataSet dsFillData = new DataSet();
-
-
SqlCommand cmdobj = new SqlCommand("Select * from tbl_LoginIDsets", myConnection);
-
-
cmdobj.CommandType = CommandType.Text;
-
-
-
SqlDataAdapter daAdapter = new SqlDataAdapter(cmdobj);
-
-
-
daAdapter.Fill(dsFillData);
-
-
SqlDataReader dr = null;
-
-
SqlCommand cmd = new SqlCommand("Select * from tbl_LoginIDsets where LoginID = " +UserName +" and Password = " +Password, myConnection);
-
-
-
dr = cmd.ExecuteReader();
-
if(dr.Read())
-
{
-
Response.Write("Valid User");
-
}
-
else
-
{
-
Response.Write("Invalid User");
-
}
-
-
}
-
}
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".
|
|
July 25th, 2008 06:54 AM
# 11
|
Re: validate entries using an sql table
Quote:
...
-
DataSet dsFillData = new DataSet();
-
SqlCommand cmdobj = new SqlCommand("Select * from tbl_LoginIDsets", myConnection);
-
cmdobj.CommandType = CommandType.Text;
-
SqlDataAdapter daAdapter = new SqlDataAdapter(cmdobj);
-
daAdapter.Fill(dsFillData);
-
|
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?
|
|
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
|
|
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.
|
|
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.
|
|
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
- " where name = '" +name.Text + "'";
|
|
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
- " where name = '" +name.Text + "'";
|
it worked! It worked!! IT WORKED!!! Thank You!!!!
|
|
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.
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
|