prasath
prasath

Reputation: 19

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

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

please help me folks....

here the code: default.aspx.cs

protected void Button1_Click(object sender, EventArgs e)
{
    try
    {

        OleDbConnection myCon = new OleDbConnection(ConfigurationManager.ConnectionStrings["vhgroupconnection"].ConnectionString);
        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);

        OleDbDataReader reader = new OleDbDataReader();



        cmd.Parameters.AddWithValue("@userid", txtuserid.Text);

        cmd.Parameters.AddWithValue("@oldpass", txtoldpass.Text);



        reader = cmd.ExecuteReader();
        reader.Read();

        if (reader["user_id"].ToString() != String.Empty && reader["passwd"].ToString() != 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("@newPasss", txtnewpass.Text);
                cmd.Parameters.AddWithValue("@userod", txtuserid.Text);
                cmd.Parameters.AddWithValue("@passwd", txtoldpass.Text);

                int count = cmd.ExecuteNonQuery();

                if (count > 0)
                {
                    lblmsg.Text = "Password changed successfully";
                }
                else
                {
                    lblmsg.Text = "password not changed";
                }
            }
        }
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

also check pls.....

Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

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

Source Error:

Line 36:             OleDbCommand cmd = new OleDbCommand(q, myCon);
Line 37: 
Line 38:             OleDbDataReader reader = new OleDbDataReader();
Line 39:             
Line 40:    

Upvotes: 2

Views: 3517

Answers (3)

Hassan
Hassan

Reputation: 5430

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;
      }
}

Upvotes: 0

Soner Gönül
Soner Gönül

Reputation: 98750

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.

Upvotes: 1

shree.pat18
shree.pat18

Reputation: 21757

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
}

Upvotes: 0

Related Questions