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
A
回答
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); –
相關問題
- 1. 當我用Symfony2上的同一用戶登錄時,斷開用戶連接
- 2. 當我啓用@EnableResourceServer和@EnableAuthorizationServer時,我遇到了登錄頁面的問題
- 3. 我遇到用戶輸入問題
- 4. 用新創建的用戶登錄遇到問題
- 5. 我剛剛安裝了Heroku,並且在初始登錄時遇到了問題
- 6. 服務器登錄和數據庫用戶遇到的問題
- 7. 在php中連接帳戶時遇到的問題
- 8. 客戶端在使用套接字連接到服務器時遇到問題?
- 9. 我在登錄後得到不同的用戶數據
- 10. 我的Django應用程序在線登錄的用戶問題
- 11. 我遇到了$ _POST問題。
- 12. 我遇到了問題compareTo
- 13. 用戶登錄失敗'NT AUTHORITY \ ANONYMOUS登錄從不同的SQL Server連接時
- 14. 我遇到了我的form_for問題
- 15. 我遇到了我的NSPredicate問題
- 16. 我遇到了我的邊欄問題
- 17. 根據登錄的用戶使用不同的連接名稱
- 18. asp.net用戶登錄問題
- 19. 我在使用NSSize時遇到問題
- 20. 我在使用API時遇到問題
- 21. 在嘗試重定向用戶時遇到問題(如果他們已登錄)
- 22. 我在複製目錄時遇到了問題
- 23. 我遇到了一個java登錄菜單應用程序的問題
- 24. 我可以在使用openID時登錄用戶登錄
- 25. 用電子郵件發送表單遇到問題我的用戶填寫了
- 26. 會話登錄問題,如果我編輯用戶名,而我登錄
- 27. 當有人連接到我的WiFi網絡時,要求用戶註冊/登錄
- 28. 實施hook_user_insert不登錄我的用戶
- 29. 我正在使用ListFragment,但我在使用LoaderManager時遇到了一些問題
- 30. 我在使用simplexml解析Facebook feed時遇到了問題
this.Session [ 「用戶名」] = DV [0] .Row [ 「用戶名」]的ToString();你確定你不是指用戶名而是用戶名嗎? – MStp
在這條線上有什麼可能的值?請確保套管是正確的。 this.Session [「UserType」] = dv [0] .Row [「UserType」]。ToString(); – MStp
錯誤消息是「ConnectionString屬性尚未初始化」 –