开发者

I am having a problem i to connect different users when user logs in

     string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;" 
             + "data source=" + Page.Server.MapPath("MyConnectionString");
        System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(connectionString);


        // System.Data.OleDb.OleDbCommand cmd = conn.CreateCommand();

        SqlDataSource ads = new SqlDataSource();
        ads.SelectParameters.Add("UserName", this.TextBox1.Text);
        ads.SelectParameters.Add("Password", this.TextBox2.Text);

        ads.SelectCommand = "SELECT * FROM [Users] WHERE [UserName]= @UserName AND [Password] = @Password";

        //retrieve required data
        //conn.Open();
        DataView dv = (DataView)ads.Select(DataSourceSelectArguments.Empty);

        //diplay error message if record is not found
        if (dv.Count == 0)
        {

            this.Label1.ForeColor = System.Drawing.Color.Red;
            this.Label1.Text = "Login failed. The username or password you have entered isn't valid";
            return;
        }

        //create Session variables
        this.Session["Username"] = dv[0].Row["Username"].ToString();
        this.Session["UserType"] = dv[0].Row["UserType"].ToString();

        //Redirect to respective page based on user 

             if (this.Session["UserType"].ToString().Equals("patient"))
                Response.Redirect("Patient.aspx");


            else if (this.Session["UserType"].ToString().Equals("doctors"))
            Response.Redirect(开发者_如何学Go"Doctor.aspx");


            else if (this.Session["UserType"].ToString().Equals("nurse"))
            Response.Redirect("Nurse.aspx");


You are initializing connection and connection string but not assigning to source to use it with the command.


Try,

 SqlDataSource ads = new SqlDataSource();
 ads.ConnectionString=connectionString;  

I dislike the SqlDatasource personally. I suggest the use of provider classes.

Demo:

/***** Please check/verify the path of database file *****/
string connectionString = "provider=Microsoft.Jet.OLEDB.4.0;" 
             + "data source=" + Page.Server.MapPath("MyConnectionString"); 
/***********************************************************/
System.Data.OleDb.OleDbConnection conn = new 
    System.Data.OleDb.OleDbConnection(connectionString);

System.Data.OleDb.OleDbCommand cmd = new System.Data.OleDb.OleDbCommand();
cmd.Connection=conn;
cmd.CommandText="SELECT * FROM [Users] WHERE [UserName]= @UserName AND [Password] = @Password";

cmd.Parameters.Add("@UserName",System.Data.OleDb.OleDbType.VarChar,40).Value=TextBox1.Text;
cmd.Parameters.Add("@Password",System.Data.OleDb.OleDbType.VarChar,40).Value=TextBox2.Text;

System.Data.OleDb.OleDbDataReader dr;
conn.Open();
dr=cmd.ExecuteReader();
bool Found=false;
string username="";
string uesrtype="";
if(dr.Read())
 {
    Found=true;
    username=dr.getString("username");
    usertype=dr.getString("usertype");
  }
conn.Close();

if (!Found)
 {
   Label1.ForeColor = System.Drawing.Color.Red;
   Label1.Text = "Login failed. The username or password you have entered isn't valid";
   return;
 }

Session["Username"] =username;
Session["UserType"] = usertype;

if(usertype.equals("patient"))
  {
  }
else
if(usertype.equals("doctor"))
 {
 }
else
if(usertype.equals("nurse"))
{
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜