Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

[got a bit further so its updated]

Hello There, i really hope you are able to help me!

Now the first part of my code does work, and I do get my report numbers out in my combobox, and i'm able to write that number to a lbl. now I need to take that number and get the rest of my data from my Access 2003 database, and drop them in a string (my output). ( I dont really want all the data loaded into my mem when i open the program, so i belive only getting the [Rapport nr] until i choose one, where I will load the data into the string and save it there for now) :)

my problem is that this wont work!

output = dbReader.GetString(dbReader.GetOrdinal("Dato")).ToString();

OBS: my error is now that it says i dont have any data in my rows or coloums

my code is as follows:

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.Collections;
using System.Data.OleDb;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private string aktuelRapportNR = "";
        string output = "";

        private string connectionName = "Provider=Microsoft.Jet.OLEDB.4.0;"
            + "Data Source=semcqdjh-access 2007.mdb;"
            + "Jet OLEDB:Database Password=;";




        public Form1()
        {
            InitializeComponent();
            #region Indlæsning af combobox, med rapport numre
            OleDbConnection Myconnection = null;
            OleDbDataReader dbReader = null;

            Myconnection = new OleDbConnection(connectionName);
            Myconnection.Open();

            OleDbCommand cmd = Myconnection.CreateCommand();
            cmd.CommandText = "SELECT [Rapport nr] FROM AVR";
            dbReader = cmd.ExecuteReader();

            int rapportNR;
            while (dbReader.Read())
            {
                rapportNR = (int)dbReader.GetInt32(dbReader.GetOrdinal("Rapport nr"));

                comboBox1.Items.Add(rapportNR);

            }

            dbReader.Close();
            Myconnection.Close();


#endregion
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            aktuelRapportNR = comboBox1.SelectedItem.ToString();
            lblAktuelRapportNR.Text = aktuelRapportNR;

            OleDbConnection Myconnection = null;
            OleDbDataReader dbReader = null;

            Myconnection = new OleDbConnection(connectionName);
            Myconnection.Open();

            OleDbCommand cmd = Myconnection.CreateCommand();
            cmd.CommandText = "SELECT [Dato] FROM AVR WHERE [Rapport nr] =" + aktuelRapportNR;
            dbReader = cmd.ExecuteReader();

            try
            {
                output = dbReader.GetString(dbReader.GetOrdinal("Dato")).ToString();

            }
            catch (Exception)
            {
                output = "fejl eller tom";
            } 
            dbReader.Close();
            Myconnection.Close();

            label1.Text = output;
        }

        private void btnExport_Click(object sender, EventArgs e)
        {

        }


    }
}
share|improve this question
Is your AVR table field [Rapport nr] text data type? If it's numeric, eliminate the single quotes from your SELECT statement. – HansUp Oct 24 '10 at 15:34
just saw it myself so i updated my post and got a bit further :P thanks anyway though :P – Hans-Henrik Oct 24 '10 at 15:38
OBS: my error is now that it says i dont have any data in my rows or coloums – Hans-Henrik Oct 24 '10 at 15:41
OK, I'm lost with .Net, but thought maybe the error message was coming from Access' (Jet) database engine. It was smart of you to identify which line causes the error. But since you also changed the code, are you still getting the same error message or a different message? Suggest you add the current error message back to your question. – HansUp Oct 24 '10 at 15:47
Sorry, I didn't see your comment until now. But you should still include the current error message in the question. – HansUp Oct 24 '10 at 15:54
show 3 more comments

1 Answer

i figured this out :D after a break i went back to this and tried to see if there where enother method i could use and there was :P i figured by the error that it was properly something about the types so insted of taking one out and trying to work around that i took the hole row out and placed it in a object array :P by using GetValues in the dbReader i got it to work, so i can now move on :D for those who might be interested! here is my code :P its not pretty but it works :P i allso took in som try catch just to make sure i check for errors and getting a friendly respons on that :)

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.Collections;
using System.Data.OleDb;


namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private string aktuelRapportNR = "";
        string output;

        private string connectionName = "Provider=Microsoft.Jet.OLEDB.4.0;"
            + "Data Source=semcqdjh-access 2007.mdb;"
            + "Jet OLEDB:Database Password=;";




        public Form1()
        {
            InitializeComponent();
            #region Indlæsning af combobox, med rapport numre
            try
            {

                OleDbConnection Myconnection = null;
                OleDbDataReader dbReader = null;

                Myconnection = new OleDbConnection(connectionName);
                Myconnection.Open();

                OleDbCommand cmd = Myconnection.CreateCommand();
                cmd.CommandText = "SELECT [Rapport nr] FROM AVR";
                dbReader = cmd.ExecuteReader();

                int rapportNR;
                while (dbReader.Read())
                {
                    rapportNR = (int)dbReader.GetInt32(dbReader.GetOrdinal("Rapport nr"));

                    comboBox1.Items.Add(rapportNR);

                }

                dbReader.Close();
                Myconnection.Close();
                txtStatus.Text = "Indlæsning Fuldført! Vælg venligst en rapport";
            }
            catch (Exception)
            {

                txtStatus.Text = "Indlæsning Fejlet!";
            }

#endregion
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            aktuelRapportNR = comboBox1.SelectedItem.ToString();
            lblAktuelRapportNR.Text = aktuelRapportNR;

            txtStatus.Text = "Du har valgt rapport nr.: " + aktuelRapportNR + "! Klik på export";
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            try
            {

                OleDbConnection Myconnection = null;
                OleDbDataReader dbReader = null;

                Myconnection = new OleDbConnection(connectionName);
                Myconnection.Open();

                OleDbCommand cmd = Myconnection.CreateCommand();
                cmd.CommandText = "SELECT * FROM AVR WHERE [Rapport nr] =" + aktuelRapportNR;
                dbReader = cmd.ExecuteReader();

                object[] liste = new object[dbReader.FieldCount];
                if (dbReader.Read() == true)
                {

                    int NumberOfColums = dbReader.GetValues(liste);

                    for (int i = 0; i < NumberOfColums; i++)
                    {
                        output += "|" + liste[i].ToString();
                    }

                }

                dbReader.Close();
                Myconnection.Close();
                txtStatus.Text = "Export Lykkes! Luk programmet!";
            }
            catch (Exception)
            {

                txtStatus.Text = "Export Fejlet!";
            }


        }


    }
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.