Plz find out error in my code.
-
Hi plz help This code is not working ,i m using asp.net 2.0 with C# for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"][i]["User_Name"]) && (txtPassword.Text == ds.Tables["CreateAccount"][i]["Password"])) Response.Write("You have succesfully logged in"); else Response.Write("User Name Does Not Exist"); } Thanks CARE CAREER
-
Hi plz help This code is not working ,i m using asp.net 2.0 with C# for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"][i]["User_Name"]) && (txtPassword.Text == ds.Tables["CreateAccount"][i]["Password"])) Response.Write("You have succesfully logged in"); else Response.Write("User Name Does Not Exist"); } Thanks CARE CAREER
Care Career wrote:
This code is not working
That is vague. What do you mean by "not working"?
Upcoming events: * Glasgow: Introduction to AJAX (2nd May), SQL Server 2005 - XML and XML Query Plans, Mock Objects, SQL Server Reporting Services... Never write for other people. Write for yourself, because you have a passion for it. -- Marc Clifton My website
-
Hi plz help This code is not working ,i m using asp.net 2.0 with C# for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"][i]["User_Name"]) && (txtPassword.Text == ds.Tables["CreateAccount"][i]["Password"])) Response.Write("You have succesfully logged in"); else Response.Write("User Name Does Not Exist"); } Thanks CARE CAREER
I'd probably use something like if ((ds.Tables["CreateAccount"].Select(string.format("User_Name = '{0}' AND Password = '{1}'", new object []{txtUser.Text,txtPassword.text})).length ==1) //login message else //failed message except i wouldn't process the message like that, i'd do a query such as: Select count(*) from users where User_Name = AND Password = and check that the return value was 1. The danger would be that someone will pick up your asp.net login function and deploy it in winforms where the client would have a list of all the username / password entries in the database. Russell
-
Hi plz help This code is not working ,i m using asp.net 2.0 with C# for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"][i]["User_Name"]) && (txtPassword.Text == ds.Tables["CreateAccount"][i]["Password"])) Response.Write("You have succesfully logged in"); else Response.Write("User Name Does Not Exist"); } Thanks CARE CAREER
-
Hi plz help This code is not working ,i m using asp.net 2.0 with C# for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"][i]["User_Name"]) && (txtPassword.Text == ds.Tables["CreateAccount"][i]["Password"])) Response.Write("You have succesfully logged in"); else Response.Write("User Name Does Not Exist"); } Thanks CARE CAREER
Well firstly you can't apply indexing with [] to a DataTable - it should be more like this (note bold text)
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if ((txtUser.Text == ds.Tables["CreateAccount"].Rows[i]["User_Name"]) &&
(txtPassword.Text == ds.Tables["CreateAccount"].Rows[i]["Password"]))
{
Response.Write("You have succesfully logged in");
}
else
{
Response.Write("User Name Does Not Exist");
}
}Also - I'm no C# guru but I find using the curly braces helps improve readability - one of the experts can correct me if I'm wrong but I don't see any harm in using curly braces in your "if" statement in this context. Secondly, how do you know that the username doesn't exist if the password is wrong? I'd suggest either breaking it down into 2 separate helper functions to determine where the problem is or (easier) changing the response to "Either the username or password is incorrect". Thirdly, you are passing the password as plaintext, which leads me to assume you're saving the password as plain text - bad idea. Rather use a hash to encrypt the password before processing or saving. Take a look at this[^]
-
Well firstly you can't apply indexing with [] to a DataTable - it should be more like this (note bold text)
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
if ((txtUser.Text == ds.Tables["CreateAccount"].Rows[i]["User_Name"]) &&
(txtPassword.Text == ds.Tables["CreateAccount"].Rows[i]["Password"]))
{
Response.Write("You have succesfully logged in");
}
else
{
Response.Write("User Name Does Not Exist");
}
}Also - I'm no C# guru but I find using the curly braces helps improve readability - one of the experts can correct me if I'm wrong but I don't see any harm in using curly braces in your "if" statement in this context. Secondly, how do you know that the username doesn't exist if the password is wrong? I'd suggest either breaking it down into 2 separate helper functions to determine where the problem is or (easier) changing the response to "Either the username or password is incorrect". Thirdly, you are passing the password as plaintext, which leads me to assume you're saving the password as plain text - bad idea. Rather use a hash to encrypt the password before processing or saving. Take a look at this[^]
Have you tried putting a .ToString() at the end of the DataSet?
for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"].Rows[i]["User_Name"].ToString()) && (txtPassword.Text == ds.Tables["CreateAccount"].Rows[i]["Password"].ToString())) { Response.Write("You have succesfully logged in"); } else { Response.Write("User Name Does Not Exist"); } }
Steve Welborn Software Engineer BitWise Solutions -
Have you tried putting a .ToString() at the end of the DataSet?
for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"].Rows[i]["User_Name"].ToString()) && (txtPassword.Text == ds.Tables["CreateAccount"].Rows[i]["Password"].ToString())) { Response.Write("You have succesfully logged in"); } else { Response.Write("User Name Does Not Exist"); } }
Steve Welborn Software Engineer BitWise Solutions -
Hi plz help This code is not working ,i m using asp.net 2.0 with C# for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"][i]["User_Name"]) && (txtPassword.Text == ds.Tables["CreateAccount"][i]["Password"])) Response.Write("You have succesfully logged in"); else Response.Write("User Name Does Not Exist"); } Thanks CARE CAREER
ismailiiuc is right. This will loop through the whole DataSet. what you should do is take it out of a loop all together. Its not needed. There should only be one username and password in there. after you fill your DataSet just do this:
if (txtUser.Text == ds.Tables["CreateAccount"][0]["User_Name"].ToString() && txtPassword.Text == ds.Tables["CreateAccount"][0]["Password"].ToString()) { Response.Write("You have succesfully logged in"); } else { Response.Write("User Name Does Not Exist"); }
Steve Welborn Software Engineer BitWise Solutions
-
Hi plz help This code is not working ,i m using asp.net 2.0 with C# for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"][i]["User_Name"]) && (txtPassword.Text == ds.Tables["CreateAccount"][i]["Password"])) Response.Write("You have succesfully logged in"); else Response.Write("User Name Does Not Exist"); } Thanks CARE CAREER
use this to find data from table ds.Tables["CreateAccount"].rows[i]["User_Name"].tostring(); or ds.Tables[0].rows[i]["User_Name"].tostring();
Sujit
-
Have you tried putting a .ToString() at the end of the DataSet?
for (int i = 0; i < ds.Tables[0].Rows.Count; i++) { if ((txtUser.Text == ds.Tables["CreateAccount"].Rows[i]["User_Name"].ToString()) && (txtPassword.Text == ds.Tables["CreateAccount"].Rows[i]["Password"].ToString())) { Response.Write("You have succesfully logged in"); } else { Response.Write("User Name Does Not Exist"); } }
Steve Welborn Software Engineer BitWise SolutionsI did it "off-the-cuff" & missed that one (oops! & thanks), what did you think of the rest or the advice?