Click here to Skip to main content
15,116,253 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying this code i found on google.
and i'm trying to fetch value from database and display it on a textbox. based it on a time

OleDbConnection cn = new OleDbConnection();
            cn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\`Hp\Desktop\PROGRAM1\notificationSystem\notificationSystem\Database.accdb";
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = cn;


            string cmdText = "SELECT * FROM news " +
                         "WHERE npublished BETWEEN ? AND ?";

            DateTime dt = this.metroDateTime1.Value.Date;
            DateTime dt2 = this.metroDateTime1.Value.Date.AddMinutes(1440);
            cmd.CommandText = cmdText;
            cmd.Parameters.AddWithValue("@p1", dt);
            cmd.Parameters.AddWithValue("@p2", dt2);
            cmd.Parameters.AddWithValue("@article", metroTextBox2.Text);
            OleDbDataAdapter adapter = new OleDbDataAdapter(cmdText, cn);
            DataSet ds = new DataSet();
            Form5 f5 = new Form5();
            f5.newsGrid.DataSource = ds.Tables[0];

What should i do? do i miss something?
Any help would be gladly appreciated.

What I have tried:

Tried the code above but it won't work.
Updated 24-Feb-16 19:09pm
Richard MacCutchan 25-Feb-16 4:23am
What happens when you run the code? Do you get any items returned from the database? if not, why?

1 solution


you are missing parameter Names on your Query. You are sending 3 parameters in your cmd (command object) check and correct your query.

Ashish Nigam
Member 12347808 25-Feb-16 0:22am
What do you mean? Please be specific.
I'm kinda newbie to what should i do? do i add something in the code?
Nigam,Ashish 25-Feb-16 0:26am
What is the use of @article parameter?
Member 12347808 25-Feb-16 0:27am
I wanted to fetch the value from article and display the value on textbox2.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900