Error: The type 'System.Data.OleDb.OleDbDataReader' has no constructors defined

前端 未结 3 1235
梦毁少年i
梦毁少年i 2021-01-22 12:28

I am trying to change password option with ms access database....

please help me folks....

here the code: default.aspx.cs

protected void Button1         


        
相关标签:
3条回答
  • 2021-01-22 13:15

    Problem: You try to make new instance of OleDbDataReader by calling new OleDbDataReader() instead you should create a reader using OleDbCommand.ExecuteReader().

    In the following code notice use of using statement (this should ensure connection closing or reader closing for the case of OleDbDataReader).

    protected void Button1_Click(object sender, EventArgs e)
    {
        try
        {
            string sConnString = ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString;
            using(OleDbConnection myCon = new OleDbConnection(sConnString))
            {
                 myCon.Open();
                 string userid = txtuserid.Text;
                 string oldpass = txtoldpass.Text;
                 string newPass = txtnewpass.Text;
                 string conPass = txtconfirmpass.Text;
    
                 string q = "select user_id,passwd from register where user_id = @userid and passwd = @oldpass";
    
                 OleDbCommand cmd = new OleDbCommand(q, myCon);
                 cmd.Parameters.AddWithValue("@userid", txtuserid.Text);
                 cmd.Parameters.AddWithValue("@oldpass", txtoldpass.Text);
    
                 string sUserId = string.Empty;
                 string sPass = string.Empty;
    
                 using(OleDbDataReader reader = cmd.ExecuteReader())
                 {
                    if(reader.Read()) //assumption: one record returned
                    {           
                       sUserId = reader["user_id"].ToString();
                       sPass = reader["passwd"].ToString();
    
                    }
                 }
    
                 if (sUserId != string.Empty && sPass != string.Empty)
                 {
                   if (newPass.Trim() != conPass.Trim())                   
                       lblmsg.Text = "New Password and old password does not match";
                   else
                   {
                        q = "UPDATE register SET passwd = @newPass WHERE user_id =@userid";
                        cmd = new OleDbCommand(q, myCon);
                        cmd.Parameters.AddWithValue("@newPass", txtnewpass.Text);
                        cmd.Parameters.AddWithValue("@userid", txtuserid.Text);                       
    
                        int count = cmd.ExecuteNonQuery();
    
                        if (count > 0)                
                           lblmsg.Text = "Password changed successfully";                
                        else                
                           lblmsg.Text = "password not changed";
    
                   }
                }
            }
        }
        catch (Exception ex)
          {
            throw ex;
          }
    }
    
    0 讨论(0)
  • 2021-01-22 13:18

    As error message says; OleDbDataReader has no constructor.

    From documentation of OleDbDataReader;

    To create an OleDbDataReader, you must call the ExecuteReader method of the OleDbCommand object, instead of directly using a constructor.

    You can use ExecuteReader method that returns OleDbDataReader

    OleDbDataReader dr = cmd.ExecuteReader();
    

    And you need add your parameter values before you call ExecuteReader method.

    Also use using statement to dispose your OleDbConnection, OleDbCommand and OleDbDataReader like;

    using(OleDbConnection myCon = new OleDbConnection(conString))
    using(OleDbCommand cmd = myCon.CreateCommand())
    {
        //Define your sql query and add your parameter values.
    
        using(OleDbDataReader dr = cmd.ExecuteReader())
        {
           //
        }  
    }
    

    And as Steve mentioned, OleDbDataReader.Read method returns boolean value (true of false) and it reads your OleDbDataReader results row by row. You might need to consider to use the result of this method like in a while statement. For example;

    while(reader.Read())
    {
        //Reads your results until the last row..
    }
    

    As a final words, I strongly suspect you store your passwords as plain text. Don't do that! Use SHA-512 hash.

    0 讨论(0)
  • 2021-01-22 13:35

    As MSDN clearly states, To create an OleDbDataReader, you must call the ExecuteReader method of the OleDbCommand object, instead of directly using a constructor.

    You cannot instantiate it using new, which is what you are doing and which is why you get the error. Remove the offending line and change it to this to get rid of the error:

    OleDbDataReader reader = cmd.ExecuteReader();
    

    Also, remember to use using blocks to ensure resources get properly disposed.

    using(OleDbConnection myCon = new OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString))
    {
     OleDbCommand cmd = new OleDbCommand(q, myCon);
    
     //Add parameters etc
    
    OleDbDataReader reader = cmd.ExecuteReader();
    
    //Rest of the processing
    }
    
    0 讨论(0)
提交回复
热议问题