2011-09-28 34 views
0
 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("Doctor.aspx"); 


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

this.Session [ 「用戶名」] = DV [0] .Row [ 「用戶名」]的ToString();你確定你不是指用戶名而是用戶名嗎? – MStp

+0

在這條線上有什麼可能的值?請確保套管是正確的。 this.Session [「UserType」] = dv [0] .Row [「UserType」]。ToString(); – MStp

+0

錯誤消息是「ConnectionString屬性尚未初始化」 –

回答

0

您正在初始化連接和連接字符串,但未分配給源以與命令一起使用。

+0

我只能得到錯誤試圖從數據庫檢索所需的數據..我如何解決它。 –

0

嘗試,

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

我不喜歡SqlDatasource個人。我建議使用provider類。 。

演示:

/***** 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

仍然錯誤的問題是檢索所需的數據>>> DataView dv =(DataView)ads.Select(DataSourceSelectArguments.Empty); –

相關問題