2 Replies - 4141 Views - Last Post: 24 January 2013 - 01:28 PM Rate Topic: -----

#1 mrudulbose   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 16-February 11

i am not able to retrive data from database using datareader

Posted 24 January 2013 - 01:14 PM

I am not able to retrive data from database using datareader. I am using visual studio 2010 and access 2007. please help me...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace Accounts
    public partial class Form3 : Form
        public TextBox TextBox1
                return textBox1;

        public Form3()

        private void Form3_Load(object sender, EventArgs e)
            this.Text = "Welome, " + textBox1.Text;

        private void button2_Click(object sender, EventArgs e)
            DateTime MyDateTime;
            MyDateTime = DateTime.Today;
            String MyString;
            MyString = MyDateTime.ToString("dd-MM-yyyy");
                if (textBox2.Text == "")
                    textBox2.Text ="0";
                else if (textBox3.Text == "")
                    textBox3.Text = " ";
                else if (textBox4.Text == "")
                    textBox4.Text = "0";
                else if (textBox5.Text == "")
                    textBox5.Text = " ";
            OleDbConnection con = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0; Data Source=D:\\accounts.accdb");
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = con;
            cmd = con.CreateCommand();
            OleDbCommand cmd1 = new OleDbCommand();
            cmd1.Connection = con;
            cmd1 = con.CreateCommand();
            OleDbCommand cmd2 = new OleDbCommand();
            cmd2.Connection = con;
            cmd2 = con.CreateCommand();
            OleDbCommand cmd3 = new OleDbCommand();
            cmd3.Connection = con;
            cmd3 = con.CreateCommand();
            OleDbCommand cmd4 = new OleDbCommand();
            cmd4.Connection = con;
            cmd4 = con.CreateCommand();
            OleDbCommand cmd5 = new OleDbCommand();
            cmd5.Connection = con;
            cmd5 = con.CreateCommand();
            OleDbCommand cmd6 = new OleDbCommand();
            cmd6.Connection = con;
            cmd6 = con.CreateCommand();
            OleDbCommand cmd7 = new OleDbCommand();
            cmd7.Connection = con;
            cmd7 = con.CreateCommand();
            OleDbCommand cmd8 = new OleDbCommand();
            cmd8.Connection = con;
            cmd8 = con.CreateCommand();
            cmd.CommandText = "INSERT INTO " + textBox1.Text + " ([Date], [Income], [IncomeDescription], [Expense], [Exp description] ) VALUES('"+MyString+"','" + textBox2.Text + "', '" + textBox3.Text + "', '" + textBox4.Text + "','" + textBox5.Text + "')";
            cmd1.CommandText = "select * from "+textBox1.Text+"";
            OleDbDataReader dr3 = cmd1.ExecuteReader();
            while (dr3.Read())
                cmd2.CommandText = "select COUNT(*) from "+textBox1.Text+"";
                int dd = (int)cmd2.ExecuteScalar();
                if (dd==1)
                    cmd3.CommandText = "update " + textBox1.Text + " set Total_income=0, Total_expense=0, Balance=0";
                    cmd6.CommandText = "select MAX(Total_income), MAX(Total_expense), MAX(Balance)from " + textBox1.Text + "";
                    OleDbDataReader dr1 = cmd6.ExecuteReader();
                [color="#FFA500"]    cmd7.CommandText = "select Income, Expense from " + textBox1.Text + "";
                    OleDbDataReader dr2 = cmd7.ExecuteReader();
                    while (dr1.Read())
                        while (dr2.Read())
                            int a = Convert.ToInt32(dr1["Total_income"]);
                            int b = Convert.ToInt32(dr1["Total_expense"]);
                            int c = Convert.ToInt32(dr1["Balance"]);
                            int d = Convert.ToInt32(dr1["Income"]);
                            int n = Convert.ToInt32(dr1["Expense"]);
                            int l = d + a;
                            int k = n + b;
                            int z = l - k;
                            cmd8.CommandText = "update " + textBox1.Text + " set Total_income=" + l + ", Total_expense=" + k + ", Balance=" + z + "";//"insert into "+textBox1.Text+" ([Total_income], [Total_expense]) values("l","k")";
            MessageBox.Show("your data has been entered...");

        private void groupBox1_Enter(object sender, EventArgs e)


This post has been edited by tlhIn`toq: 24 January 2013 - 01:19 PM
Reason for edit:: Fixed the fraked up quote and code tags

Is This A Good Question/Topic? 0
  • +

Replies To: i am not able to retrive data from database using datareader

#2 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: i am not able to retrive data from database using datareader

Posted 24 January 2013 - 01:20 PM

FAQ 31: Databases
FAQ 2: How to debug

What this shows us is that you aren't familiar with breakpoints and how to debug your own code.

tlhIn`toq's FAQ list

Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.

TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2

FAQ (Frequently Asked Questions - Updated Jan 2013

Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6537
  • View blog
  • Posts: 14,450
  • Joined: 02-June 10

Re: i am not able to retrive data from database using datareader

Posted 24 January 2013 - 01:28 PM

After that, you seriously need to clean up your code to make it easier on yourself.

Form3, Textbox1, button1 etc
chains of if...else
Using textbox4.text as if it were a variable instead of parameters
Single letter variable names

All in all - its a hot mess that looks like a first week student wrote it.

If possible, skip doing this in WinForms. Just go straight to WPF since it is all equally new to you. That way a year from now when you have some idea of what you are doing you don't have to start all over learning WPF for current generation of Windows (Win8).

Some of my common tips (some may apply more than others to your specific style):
  • Take the extra 3 seconds to rename your controls each time you drag them onto a form. The default names of button1, button2... button54 aren't very helpful. If you rename them right away to something like btnOk, btnCancel, btnSend etc. it helps tremendously when you make the methods for them because they are named after the button by the designer.btnSend_Click(object sender, eventargs e) is a lot easier to maintain than button1_click(object sender, eventargs e)

  • You aren't paying for variable names by the byte. So instead of variables names of a, b, c go ahead and use meaningful names like index, timeOut, row, column and so on. You should avoid 'T' for the timer. Amongst other things 'T' is commonly used throughout C# for Type and this will lead to problems. There are naming guidelines you should follow so your code confirms to industry standards. It makes life much easier on everyone around you, including those of us here to help. If you start using the standards from the beginning you don't have to retrain yourself later.
    You might want to look at some of the naming guidelines. Its a lot easier to start with good habits than to break bad habits later and re-learn.

  • Don't use your GUI objects as your variable. In other words don't keep referencing TextBox4.Text everyplace. TextBox4.Text is not a variable or property. The GUI is on its own thread so as soon as you start doing multi-threading you're screwed because your worker thread can't access the GUI elements. Use properties.

  • Try to avoid having work actually take place in GUI control event handlers. It is better to have the GUI handler call other methods so those methods can be reused and make the code more readable. This is also how you can send parameters rather than use excessive global variables. Get in this habit even if you are using WinForms because WPF works a lot under the idea of "commands" and this will get you working towards that. Think of each gester, control click, menu option etc. as a command to do something such as a command to SAVE. It doesn't matter where the command comes from, all sources should point at the same target to do the actual saving.

  • Don't replace lines of code that don't work. Instead comment them out and put your new attempts below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot


    If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.

  • You have to program as if everything breaks, nothing works, the cyberworld is not perfect, the attached hardware is flakey, the network is slow and unreliable, the harddrive is about to fail, every method will return an error and every user will do their best to break your software. Confirm everything. Range check every value. Make no assumptions or presumptions.

  • I strongly suggest installing VMware or some other virtualization technology on your development PC so you can create a couple virtual computers for testing. This would allow you to debug and test inside: WinXP32, XP64, Vista, Win7x32, Win7x64... etc. without having to actually have 5 physical PC's. Visual Studio will let you send the debug directly into one of these virtual machines so you can watch it operate, check its variables, see the crashes and so on just as if it were debugging on your real machine.

  • This can't be stressed enough in today's world of cell phone messaging:
    Don't use txt/sms/leet/T9 speak like: u no, u r, dnt, wut i m do-n, coz, al gud, b4, ny1, sum1, please and so on like this guy:


Was This Post Helpful? 1
  • +
  • -

Page 1 of 1