John
John

Reputation: 141

C# OLEDB ExecuteReader Error

I'm pretty new to using M-S Access Databases in Visual Studio and therefore I'm not familiar with the OLEDB syntax. I've managed to create this program using various internet sources. My program so far gets the user to log into a login form, it then tests the data against the username and password fields and if they match it then redirects the user to the second form, this then gathers data from an Access database using the name they've logged in with, however I keep getting the error "No value given for one or more required parameters." when it tries to execute the code which gathers data from the database depending on their name. This is my code so far:

private void Form2_Load(object sender, EventArgs e)
    {
        string username = lblName.Text;
        OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Rhys\Documents\Visual Studio 2013\Projects\AssignmentTrackerV2\AssignmentTrackerV2\bin\Debug\ATDatabase.accdb");
        DataTable dt = new DataTable();
        con.Open();
        OleDbDataReader dr = null;
        OleDbCommand cmd = new OleDbCommand("SELECT [Name], [Surname], [Password], [ID] FROM MemberDetails WHERE [Name] = '" + username + "'", con);
//This is where the error is occuring.
        **dr = cmd.ExecuteReader();**
        while (dr.Read())
        {
            lblName.Text = (dr["Name"].ToString() + dr["Surname"].ToString());
            lblCourseTitle.Text = (dr["CourseTitle"].ToString());
            lblID.Text = "ID: " + (dr["MemberID"].ToString());
        }
        con.Close();
    }

Any advice on how to fix this error would be appreciated and as stated before I am fairly new to OLEDB syntax and apologise if there is an easy solution, thanks!

Upvotes: 0

Views: 1592

Answers (2)

kulotskie
kulotskie

Reputation: 331

try to use this code

     string username = lblName.Text;
    using(OleDbConnection con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Rhys\Documents\Visual Studio 2013\Projects\AssignmentTrackerV2\AssignmentTrackerV2\bin\Debug\ATDatabase.accdb"))
    {
         using(OleDbCommand cmd = new OleDbCommand(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Rhys\Documents\Visual Studio 2013\Projects\AssignmentTrackerV2\AssignmentTrackerV2\bin\Debug\ATDatabase.accdb"))
         {
            cmd.Connection = con;
            cmd.CommandText = "SELECT [Name], [Surname], [Password], [ID] FROM MemberDetails WHERE [Name] = '" + username + "'";
            cmd.CommandType = CommandType.Text;
            OleDbDataReader dr = null;
            try 
            {           
                con.Open();
                dr = cmd.ExecuteReader();
                while (dr.Read())
                {
                   lblName.Text = (dr["Name"].ToString() + dr["Surname"].ToString());
                   lblCourseTitle.Text = (dr["CourseTitle"].ToString());
                   lblID.Text = "ID: " + (dr["MemberID"].ToString());
                }
                con.Close();
            }
            catch (Exception)
            {
                throw;
            }}}

i hope that this code will help you

Upvotes: 1

jmcilhinney
jmcilhinney

Reputation: 54417

At a glance, it looks like that should work. Regardless, the way you're doing it is bad because, among other things, it leaves you open to SQL injection attacks. Try replacing this:

OleDbCommand cmd = new OleDbCommand("SELECT [Name], [Surname], [Password], [ID] FROM MemberDetails WHERE [Name] = '" + username + "'", con);

with this:

OleDbCommand cmd = new OleDbCommand("SELECT [Name], [Surname], [Password], [ID] FROM MemberDetails WHERE [Name] = @Name", con);

cmd.Parameters.AddWithValue("@Name", username);

We can look more closely if that still doesn't work.

Upvotes: 0

Related Questions