2015-04-23 36 views
-1
protected void btnLogIn_Click(object sender, EventArgs e) 
    { 
     string ID = Request.Form["txtID"]; 
     String password = Request.Form["txtPassword"]; 
     string strConString = ConfigurationManager.ConnectionStrings["SOConnectionString"].ConnectionString; 
     OleDbConnection conn = new OleDbConnection(strConString); 
     OleDbCommand cmd = new OleDbCommand("SELECT * FROM USERMASTER WHERE USERID =" + ID + ""); 

     try 
     { 

      // ClientScript.RegisterStartupScript(this.GetType(), "yourMessage", "alert('" + ID + " " + password + "');", true); 
      conn.Open(); 
      OleDbDataReader dr; 

      dr = cmd.ExecuteReader(); 
      txtID.Text = "test1"; 
      //txtPassword.Text = dr["UserPwd"].ToString(); 
      //OleDbDataReader dr; 
      //while (dr.Read()){ 
      // ClientScript.RegisterStartupScript(this.GetType(), "yourMessage", "alert('" + ID + " " + password + "');", true); 
      // txtPassword.Text = dr["UserPwd"].ToString(); 
      //} 

     } 
     catch (Exception ex) 
     { 
      txtID.Text = "ex"; 
      lbltest.Text = ex.ToString(); 


     } 
     finally 
     { 
      conn.Close(); 
      conn.Dispose(); 
     } 

例外:System.InvalidOperationException:

System.InvalidOperationException:的ExecuteReader:Connection屬性尚未初始化。 System.Data.OleDb.OleDbCommand.ValidateConnection(String method)at System.Data.OleDb.OleDbCommand.ValidateConnectionAndTransaction(String method)at System.Data.OleDb.OleDbCommand.ExecuteReaderInternal(CommandBehavior behavior,String method)at System.Data在System.Data.OleDb.OleDbCommand.ExecuteReader()位於c:\ Users \ b2patso \ Desktop \ SO Maintainance \ Default.aspx中_Default.btnLogIn_Click(Object sender,EventArgs e)處的.OleDb.OleDbCommand.ExecuteReader(CommandBehavior行爲) cs:line 29

有人知道怎麼回事?我正在使用asp.net C#和visual studio 2013. db是oracle

+2

你把conn傳給cmd了嗎? – uowzd01

+0

string strConString = ConfigurationManager.ConnectionStrings [「SOConnectionString」]。ConnectionString; OleDbConnection conn = new OleDbConnection(strConString); OleDbCommand cmd = new OleDbCommand(「SELECT * FROM USERMASTER」); string ID = Request.Form [「txtID」]; String password = Request.Form [「txtPassword」]; –

+0

嘗試OleDbCommand cmd = new OleDbCommand(「SELECT * FROM USERMASTER」,conn); – uowzd01

回答

0

嘗試OleDbCommand cmd = new OleDbCommand(「SELECT * FROM USERMASTER」,conn);

+0

其工作非常好..謝謝:) –

相關問題