Click here to Skip to main content
15,903,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
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;

           //reg();

           cn.Open();
           cmd.CommandText = "select article from news where npublished = '" + metroDateTime1.Text + "' AND ntime = '" + label4.Text + "'";
           //AND ntime = '" + label4.Text + "'

           //Scalar executes the fetching of data
           var a = cmd.ExecuteScalar();

           //this statement gets the process of getting the data from the database to label
           if (a != null)
           {
               label1.Text = "" + a.ToString();

               //while in this statement is displaying the data inside the textbox
               metroTextBox2.Text = label1.Text;
           }

           else if (metroTextBox2.Text != null)
           {

               //SEND SMS PROCESS
           }
           else if (metroTextBox2.Text == null)
           {
               //REPEAT FETCH
           }
           cn.Close();


C#
So the code above works but i use a button, and i have to press the button everytime to fetch information. Does anyone have any advice on what should i do to automatically fetch information from database using datagridview and display it on textbox.

I want to put the code on form load, so everytime i will run the program it will automatically fetch and do its job. But i don't know how to do it and i tried but it won't work.

Any help is gradly appreciated.


What I have tried:

Tried using a timer so every 1 sec it will fire an action.
Posted
Updated 8-Mar-16 4:56am
Comments
Mehdi Gholam 7-Mar-16 4:33am    
and what happened to the timer?
Member 12347808 7-Mar-16 6:11am    
Did not work..i put the timer on the form load and it did not work.
Mehdi Gholam 7-Mar-16 6:14am    
Enable the timer, and set AutoReset = true
Richard Deeming 7-Mar-16 5:54am    
Your code is vulnerable to SQL Injection[^].

NEVER use string concatenation to build a SQL query. ALWAYS use a parameterized query.
Member 12347808 7-Mar-16 6:11am    
I don't care if its vulnerable..this project wont go public or what.
School project.

1 solution

 
Share this answer
 
Comments
Member 12347808 9-Mar-16 0:53am    
I don't find this helpful..
Maciej Los 9-Mar-16 1:51am    
Sorry, but seems you're lazy to read linked articles. Are you expecting that i'll prepare for you ready-to-use solution?
As i mentioned in my previous answer, you should call procedure/method which will connect your programme with database OnLoad event or directly in Form-constructor. That'a all! If you can't do it, i'd suggest to read carefullly above articles.
Member 12347808 9-Mar-16 2:23am    
I don't find it useful as i have code already and yes im too lazy to read loads of article and if you r not interested in helping then pls do not bother to comment.

I already have tried but it did not work at all....
Maciej Los 9-Mar-16 2:36am    
"I already have tried but it did not work at all..." - Don't you think that such of comment is not informative at all? First: i have no idea what have you tried, second: i have no idea why this is not working. And you have the audacity to accuse me i'm bother you?!?
Member 12347808 9-Mar-16 2:41am    
You have no idea what you talkin about? What i tried is what you tried to give me a false solution. Then if you have no idea why it is not working then stop commenting..Yes you are bothering me

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