Hi,

I am coding the loginpage, i want know how to code

1.) To check whether the User entered username,pwd matches with any of the rows of database data, if it is so it should navigate to next page like inbox.aspx.


As i am new to Asp.net, please help me to know this.


Thanks.

You can do it in following way :

SqlDataAdapter objAdpt;
SqlConnection objCon = new SqlConnectin("your database connection string");
DataTable objDt;

now  in the Click event of Button say "OK" button click event..

objAdpt = new SqlDataAdapter("Select * from yourtablename WHERE UserID=' " + textBox1.Text.Trim() + " ' , Password = ' " + textBox2.Text.Trim() + " ' ", objCon);
objDt = new DataTable();
objAdpt.Fill(objDt);

if(objDt.Rows.Count > 0)
{
  // valid user. allow user to use system..
}
else
{
  // put message that invalid user..
}

Hi,

I am coding the loginpage, i want know how to code

1.) To check whether the User entered username,pwd matches with any of the rows of database data, if it is so it should navigate to next page like inbox.aspx.


As i am new to Asp.net, please help me to know this.


Thanks.

Hi,

I am coding the loginpage, i want know how to code

1.) To check whether the User entered username,pwd matches with any of the rows of database data, if it is so it should navigate to next page like inbox.aspx.


As i am new to Asp.net, please help me to know this.


Thanks.

Hi,

I agree with the solution given by Rohan.

However, as you are new to ASP.NET I would suggest that once you get this working. Go ahead and try implementing Forms Based Authentication and then move on to Role Bases Authentication Authorization.

Read on Forms Authentication here

You can do it in following way :

SqlDataAdapter objAdpt;
SqlConnection objCon = new SqlConnectin("your database connection string");
DataTable objDt;

now  in the Click event of Button say "OK" button click event..

objAdpt = new SqlDataAdapter("Select * from yourtablename WHERE UserID=' " + textBox1.Text.Trim() + " ' , Password = ' " + textBox2.Text.Trim() + " ' ", objCon);
objDt = new DataTable();
objAdpt.Fill(objDt);

if(objDt.Rows.Count > 0)
{
  // valid user. allow user to use system..
}
else
{
  // put message that invalid user..
}

Hi........thanks for replying me soon and i haven't replied soon,sorry for that.I have tried using the code it worked fine......Thank you very much.I have one doubt for this....u have used datatable...can't we use dataset for storing the row.

Hi
harika.k
the following code will useful it is login code
table structure in database is also give at end

private void loginbtnbtn_Click(object sender, EventArgs e)
        {
                               
            
                SqlConnection con = new SqlConnection();
                string str = "Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=cardb;Data Source=SAIDATTA-051EA8\\SQLEXPRESS";
                con.ConnectionString = str;
                con.Open();
                string query = "select  * from carlogin where empcode='" + textBox1.Text + "'";//textBox1=employeid
                SqlCommand cmd = new SqlCommand(query, con);

                SqlDataReader dr;

                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                    string pwd = Convert.ToString(dr[1]);//textBox2=password
                                      

                    if ((textBox2.Text == pwd) )
                    {
                        MessageBox.Show("YOU ARE LOGGED IN AS ADMIN");
                        MDIParent2 m1 = new MDIParent2();// MDIParent2 is form for parent
                        m1.Show();



                    }
                    else if ((textBox2.Text == pwd) )
                    {
                        MessageBox.Show("YOU ARE LOGGED IN AS AS STAFF");
                        MDIParent1 m = new MDIParent1();// MDIParent1 is form for staff
                        m.Show();


                    }
                    else
                    {
                        MessageBox.Show(" invalid  username or password");
                        
                    }
                }
            }
table are in following formate
 

create table carlogin ( username varchar(40) canstraint primary key(username),password varchar(30) )
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.