Welcome to Dream.In.Code
Getting Help is Easy!

Join 95,475 Programmers for FREE!. Ask your question and get quick answers from Dream.In.Code experts. There are 968 online right now! We're the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert

Register to Make This Box Go Away!


Login Question

 
Reply to this topicStart new topic

Login Question

IceX
post 8 May, 2008 - 09:30 PM
Post #1


New D.I.C Head

*
Joined: 17 Apr, 2008
Posts: 12

I got this problem , whenever i try to login using the following code , my ASP.net page can only log in the last user of my database . i think is because the Username and password keeps on overwriting itself till the last value of the database . Can anyone help mi to figure out how to solve this problem . i want to be able to log in all of the users in my database . tried using arrays to store but cant seems to solve it.

Here is the code :

CODE

using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Login : System.Web.UI.Page
{
    String UserName;
    String Password;
    Boolean Tracker1 = true;
    Boolean Tracker2 = true;
    Boolean Tracker3 = true;
    private void InitializeComponents()
    {
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection myConn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\images\ChineseCheckerz.mdf;Integrated Security=True;User Instance=True");
        try
        {
            myConn.Open();
            String strQuery = "Select UserName , Password FROM UserInfo";
            SqlCommand myCommand = new SqlCommand(strQuery, myConn);
            DataSet myDataSet = new DataSet();
            SqlDataAdapter myAdapter = new SqlDataAdapter();
            myAdapter.SelectCommand = myCommand;
            myAdapter.Fill(myDataSet , "UserInfo");


           // SqlDataReader myReader = myCommand.ExecuteReader();

           /* if (myReader.HasRows)
            {
                MessageBox.Show("Has rows");
            }*/
            //while (myReader.Read())

            for(int i = 0; i<=myDataSet.Tables["UserInfo"].Rows.Count - 1; i++)
            {
                UserName = Convert.ToString(myDataSet.Tables["UserInfo"].Rows[i]["UserName"]);
                Password = Convert.ToString(myDataSet.Tables["UserInfo"].Rows[i]["Password"]);
            }

           //MessageBox.Show("UserName" + UserName);
           // MessageBox.Show("Password" + Password);

             //myReader.Close();


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            if (myConn.State == ConnectionState.Open)
            {
                myConn.Close();
            }
        }
      
    }
  
    
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (TextBox1.Text == "" && TextBox2.Text == "")
        {
            MessageBox.Show("Please enter your username and password");
            Tracker3 = false;
        }

        
        if (TextBox1.Text == ""&& Tracker3==true)
        {
            MessageBox.Show("Please enter your username");
            Tracker1 = false;
        }

        if (TextBox2.Text == "" && Tracker3==true)
        {
            MessageBox.Show("Please Enter your Password");
            Tracker2 = false;
            
        }

         if (Tracker1 == true && Tracker2 == true && Tracker3 == true)
        {
          
                if (TextBox1.Text == UserName && TextBox2.Text == Password)
                {
                    Response.Redirect("./LoginSuccess.aspx");
                }

                else
                {
                    MessageBox.Show("Invalid UserID or password");
                }
            
            
        }
    }
}



User is offlineProfile CardPM

Go to the top of the page


thor78
post 8 May, 2008 - 11:40 PM
Post #2


D.I.C Head

Group Icon
Joined: 6 May, 2008
Posts: 96



Dream Kudos: 50
My Contributions


Well, you just said yourself that it outputs the last value. Based on your code, your for loop WILL iterate through ALL of the rows, so naturally, the values of your username and password variables will contain the last record iteration.

Edit your SQL query to include a WHERE statement to filter the username and password, so that you only get the record needed for verification.




--------------------
Novelty is key to interest.
User is offlineProfile CardPM

Go to the top of the page

IceX
post 8 May, 2008 - 11:59 PM
Post #3


New D.I.C Head

*
Joined: 17 Apr, 2008
Posts: 12

QUOTE(thor78 @ 8 May, 2008 - 11:40 PM) *

Well, you just said yourself that it outputs the last value. Based on your code, your for loop WILL iterate through ALL of the rows, so naturally, the values of your username and password variables will contain the last record iteration.

Edit your SQL query to include a WHERE statement to filter the username and password, so that you only get the record needed for verification.


i mean that this value taken out from the database will be used to compare with the textbox input of the users in the website . for example if my database has got 10 people , and lets say the last person has got a username of abc and password 123 , and the 5th person has got a username of abcd and password 222. how can i allow users from 5th person to log in successfully ? Can SQL query to do this ?
User is offlineProfile CardPM

Go to the top of the page

thor78
post 9 May, 2008 - 12:42 AM
Post #4


D.I.C Head

Group Icon
Joined: 6 May, 2008
Posts: 96



Dream Kudos: 50
My Contributions


I think I already gave you a hint, try WHERE in your SQL query.


--------------------
Novelty is key to interest.
User is offlineProfile CardPM

Go to the top of the page

rgfirefly24
post 9 May, 2008 - 10:50 AM
Post #5


D.I.C Regular

Group Icon
Joined: 7 Apr, 2008
Posts: 283



Thanked 4 times

Dream Kudos: 150
My Contributions


QUOTE(thor78 @ 9 May, 2008 - 12:42 AM) *

I think I already gave you a hint, try WHERE in your SQL query.


try this

CODE



Select UserName , Password FROM UserInfo WHERE UserName = (value of textbox)



you should set a SQLParamiter to get the value from your textbox and replace (value of textbox) with the paramiter name.


--------------------
Please do not PM me asking for help.
User is offlineProfile CardPM

Go to the top of the page

IceX
post 11 May, 2008 - 07:58 PM
Post #6


New D.I.C Head

*
Joined: 17 Apr, 2008
Posts: 12

Tried using this :

String strQuery = "Select UserName , Password FROM UserInfo WHERE UserName = " +TextBox1.Text;

however i got the syntax error :
"Incorrect syntax near "=" this is caught as an exception error.

i also configured the select statement as follow :

SELECT [UserName], [Password] FROM [UserInfo] WHERE ([UserName] = @UserName)
in the WHERE option ,
Columm set as UserName , Operator set as "=" , Source as Control
Control ID as TextBox1 ; Default value as "TextBox1.Text ;




User is offlineProfile CardPM

Go to the top of the page

thor78
post 11 May, 2008 - 11:26 PM
Post #7


D.I.C Head

Group Icon
Joined: 6 May, 2008
Posts: 96



Dream Kudos: 50
My Contributions


String strQuery = "Select UserName , Password FROM UserInfo WHERE UserName =" +TextBox1.Text;

use this instead:
csharp

string SqlQuery = "Select UserName , Password FROM UserInfo WHERE UserName =";
string WhereParameter = TextBox1.Text;
String.Format("{0}{1}",SqlQuery,WhereParameter);


--------------------
Novelty is key to interest.
User is offlineProfile CardPM

Go to the top of the page

IceX
post 11 May, 2008 - 11:46 PM
Post #8


New D.I.C Head

*
Joined: 17 Apr, 2008
Posts: 12

The problem is still there anime2.gif
"incorrect syntax near the "="
is there really a syntax error ?
as far as i can see it got no syntax error .


CODE

protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection myConn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\images\ChineseCheckerz.mdf;Integrated Security=True;User Instance=True");
        try
        {
            myConn.Open();
            string strQuery = "Select UserName , Password FROM UserInfo WHERE UserName =";
            string WhereParameter = TextBox1.Text;
            string.Format("{0}{1}", strQuery, WhereParameter);  

            SqlCommand myCommand = new SqlCommand(strQuery, myConn);
            DataSet myDataSet = new DataSet();
            SqlDataAdapter myAdapter = new SqlDataAdapter();
            myAdapter.SelectCommand = myCommand;
            myAdapter.Fill(myDataSet , "UserInfo");


           // SqlDataReader myReader = myCommand.ExecuteReader();

           /* if (myReader.HasRows)
            {
                MessageBox.Show("Has rows");
            }*/
            //while (myReader.Read())
            for(int i = 0; i<=myDataSet.Tables["UserInfo"].Rows.Count - 1; i++)
            {
                UserName = Convert.ToString(myDataSet.Tables["UserInfo"].Rows[i]["UserName"]);
                Password = Convert.ToString(myDataSet.Tables["UserInfo"].Rows[i]["Password"]);
            }
           //MessageBox.Show("UserName" + UserName);
           // MessageBox.Show("Password" + Password);

             //myReader.Close();


        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
        finally
        {
            if (myConn.State == ConnectionState.Open)
            {
                myConn.Close();
            }
        }


i comment out line by line and discovered that the problem lies with this line :

myAdapter.Fill(myDataSet , "UserInfo");

for the incorrect syntax near the "="

but i still think that its something to do with my strQuery as its being passed inside into the adapter when it runs mycommand.

This post has been edited by IceX: 12 May, 2008 - 12:40 AM
User is offlineProfile CardPM

Go to the top of the page

jayman9
post 12 May, 2008 - 08:32 AM
Post #9


Student of Life

Group Icon
Joined: 26 Dec, 2005
Posts: 5,966



Thanked 10 times

Dream Kudos: 500

Expert In: C#, VB.NET, Java

My Contributions


What is the data type of the UserName column in your database?

If it is a string (varchar), then you need to enclose the value supplied in the Sql statement with single quotes.

CODE

String strQuery = "Select UserName , Password FROM UserInfo WHERE UserName = '" +TextBox1.Text + "'";


--------------------
User is offlineProfile CardPM

Go to the top of the page

rgfirefly24
post 12 May, 2008 - 10:41 AM
Post #10


D.I.C Regular

Group Icon
Joined: 7 Apr, 2008
Posts: 283



Thanked 4 times

Dream Kudos: 150
My Contributions


Jay is probably right on this one. Although i still like using SQLParamiters so that it will catch if the input is different from the DBType.

also i would suggest using Step Over Mode in order to find exactly what values are stored in your variables. It might be different that what you think.

This post has been edited by rgfirefly24: 12 May, 2008 - 10:44 AM


--------------------
Please do not PM me asking for help.
User is offlineProfile CardPM

Go to the top of the page

IceX
post 12 May, 2008 - 07:31 PM
Post #11


New D.I.C Head

*
Joined: 17 Apr, 2008
Posts: 12

QUOTE(jayman9 @ 12 May, 2008 - 08:32 AM) *

What is the data type of the UserName column in your database?

If it is a string (varchar), then you need to enclose the value supplied in the Sql statement with single quotes.

CODE

String strQuery = "Select UserName , Password FROM UserInfo WHERE UserName = '" +TextBox1.Text + "'";




Yea ! thanks man jayman ! i got it all solved smile.gif
thanks a million !
and yea my database is stored as varchar .
User is offlineProfile CardPM

Go to the top of the page

thor78
post 12 May, 2008 - 11:26 PM
Post #12


D.I.C Head

Group Icon
Joined: 6 May, 2008
Posts: 96



Dream Kudos: 50
My Contributions


Great work! But try to stay away from directly concatenating your strings, you can use String.Format() or an instance of Stringbuilder. smile.gif


--------------------
Novelty is key to interest.
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 7/5/08 03:41AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month
-->