0

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!

0

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting a different error now towards the bottom of the code on one of the brackets. It says " COM object that has been separated from its underlying RCW cannot be used."
what line on the codes do you find the error... i already try that code and its working or you can try to change the Microsoft.ACE.OLEDB.12.0 to Microsoft.Jet.OLEDB.4.0 and save your db to mdb extention(save as 2003)
I tried changing the file formats and the connection strings however I'm still getting the same error. The error is occurring at line 25, the middle closing bracket after "throw".
I found the problem, the error only occurs when you use the double using quotes. I don't know whether if it's because I used another using command without closing the first one but instead I just removed the second using statement on the OleDBCommand object and it works fine, thanks for your help!
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.