Click here to Skip to main content
15,894,050 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
how to check userid and password from database.i have to open a page of teacher,admin,parent through switch case

my code is here:................

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con;
    SqlDataReader dr;
    SqlDataReader dr1,dr2;
    SqlDataAdapter da;
    SqlCommand sqlcomm;
    string username;
    string password;
    protected void Page_Load(object sender, EventArgs e)
    {
        string str = (string)ConfigurationManager.AppSettings["connectionstring"];
        con = new SqlConnection(str);
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        con.Open();
        string s=selected(DropDownList1.SelectedValue);
        string p="select username,password from login where type="+ s +"";
        sqlcomm = new SqlCommand(p,con);
        dr=sqlcomm.ExecuteReader();
        if (dr.Read())
        {
            username = dr1.GetString(0);
            password = dr2.GetString(1);
            if ((TextBox0.Text == username)&&(TextBox1.Text==password))
            {
                Server.Transfer("Admin.aspx");
                switch ()
                {
                    case "Admin":
                        return "Admin.aspx";
                    case "Parent":
                        return "Parent.aspx";
                    case "Teacher":
                        return "Teacher.aspx";
                    default:
                        return "zero";

                }
            }
        \
        }
       
    }
    public string selected(string i)
    {

        switch (i)
        {
            case "Admin":
                return "Admin";
            case "Parent":
                return "Parent";
            case "Teacher":
                return "Teacher";
            default :
                return "zero";
          
        }
    }
}
Posted
Comments
codegeekalpha 3-Jan-12 0:52am    
This is not the right way to do it.. You should better try to use sessions ora cookies..

try as
C#
if ((TextBox0.Text == username)&&(TextBox1.Text==password))
            {
               
                switch (s)
                {
                    case "Admin":
                         Server.Transfer("Admin.aspx");
                    case "Parent":
                         Server.Transfer("Parent.aspx");
                    case "Teacher":
                        Server.Transfer("Teacher.aspx");
                    default:
                        return "zero";
 
                }
            }
 
Share this answer
 
con.Open();
string s=selected(DropDownList1.SelectedValue);
string p="select username,password from login where type='"+ s +"'";
sqlcomm = new SqlCommand(p,con);
dr=sqlcomm.ExecuteReader();
if (dr.Read())
{
username = dr.GetString(0);
password = dr.GetString(1);
if ((TextBox0.Text == username) && (TextBox1.Text == password))
{
switch (s)
{
case "Admin":
Server.Transfer("Admin.aspx");
break;
case "Parent":
Server.Transfer("Parent.aspx");
break;
case "Teacher":
Server.Transfer("Teacher.aspx");
break;


}

}
else
{
Label1.Text = "Invalid login";
Label1.Visible = true;
}

}
 
Share this answer
 
you can code like

select type from table where user='user' and pwd='pwd';

and then

if(type=="Admin"
Server.Transfer("Admin.aspx");
vice versa
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900