Simple database class error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lasse Edsvik

    Simple database class error

    Hello

    Im trying to create a simple testclass that connects to a db on localhost
    and a method that returns a dataset. I get these errors:

    Unhandled Exception: System.InvalidO perationExcepti on: Fill:
    SelectCommand.C onne
    ction property has not been initialized.
    at System.Data.Com mon.DbDataAdapt er.GetConnectio n3(DbDataAdapte r adapter,
    IDb
    Command command, String method)
    at System.Data.Com mon.DbDataAdapt er.FillInternal (DataSet dataset,
    DataTable[]
    datatables, Int32 startRecord, Int32 maxRecords, String srcTable,
    IDbCommand co
    mmand, CommandBehavior behavior)
    at System.Data.Com mon.DbDataAdapt er.Fill(DataSet dataSet, Int32
    startRecord,
    Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior
    behavior)

    at System.Data.Com mon.DbDataAdapt er.Fill(DataSet dataSet)
    at Procent.DataJox .Northwind.GetA llCustomers() in C:\Documents and
    Settings\A
    dministrator\My Documents\Visua l Studio\Projects \DataJox\DataJo x\Class1.cs:
    line 21
    at rock.Program.Ma in(String[] args) in C:\Documents and
    Settings\Admini strato
    r\My Documents\Visua l Studio\Projects \DataJox\rock\P rogram.cs:line 14



    using System;

    using System.Collecti ons.Generic;

    using System.Text;

    using System.Data;

    using System.Data.Sql Client;

    namespace Procent.DataJox

    {

    public class Northwind

    {

    private SqlConnection dbconn;


    public DataSet GetAllCustomers ()

    {

    Open();

    DataSet ds = new DataSet();


    SqlDataAdapter da = new SqlDataAdapter( );

    da.SelectComman d = new SqlCommand("Cus tListAll", dbconn);

    da.Fill(ds);

    return ds;

    }



    private void Open()

    {

    string connstr = "server=localho st;database=nor thwind;UID=sa;P WD=kk";

    try

    {

    SqlConnection dbconn = new SqlConnection(c onnstr);

    dbconn.Open();

    }

    catch

    {

    throw;

    }

    }


    }


    }


  • Chris Dunaway

    #2
    Re: Simple database class error

    Lasse Edsvik wrote:[color=blue]
    > Im trying to create a simple testclass that connects to a db on localhost
    > and a method that returns a dataset. I get these errors:
    >
    > Unhandled Exception: System.InvalidO perationExcepti on: Fill:
    > SelectCommand.C onne
    > ction property has not been initialized.[/color]

    That's because you never open the connection.
    [color=blue]
    >
    > Open();
    >[/color]

    The connection you open in the Open() method is a /different/
    connection that is declared inside the Open method and is only visible
    there. It is not the same as the one you declared at class level.
    [color=blue]
    > SqlConnection dbconn = new SqlConnection(c onnstr);[/color]

    Change this line to:

    dbconn = New SqlConnection(c onnstr)

    Comment

    • Spidey

      #3
      Re: Simple database class error

      The line
      SqlConnection dbconn = new SqlConnection(c onnstr);
      in the Open method is the problem.
      Replace this line with
      dbconn = new SqlConnection(c onnstr);


      What's happenning here is that a connection object is being created,
      assigned to and opened properly in the Open method. However, this is
      being assigned to the local variable dbconn (because of the
      SqlConnection dbconn declaration inside the Open method). This local
      dbconn variable is valid only within the Open method. The dbconn
      variable referred to in the GetAllCustomers method is the private
      variable dbconn which has never been assigned to.

      Regards,
      Sarin.

      Lasse Edsvik wrote:[color=blue]
      > Hello
      >
      > Im trying to create a simple testclass that connects to a db on localhost
      > and a method that returns a dataset. I get these errors:
      >
      > Unhandled Exception: System.InvalidO perationExcepti on: Fill:
      > SelectCommand.C onne
      > ction property has not been initialized.
      > at System.Data.Com mon.DbDataAdapt er.GetConnectio n3(DbDataAdapte r adapter,
      > IDb
      > Command command, String method)
      > at System.Data.Com mon.DbDataAdapt er.FillInternal (DataSet dataset,
      > DataTable[]
      > datatables, Int32 startRecord, Int32 maxRecords, String srcTable,
      > IDbCommand co
      > mmand, CommandBehavior behavior)
      > at System.Data.Com mon.DbDataAdapt er.Fill(DataSet dataSet, Int32
      > startRecord,
      > Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior
      > behavior)
      >
      > at System.Data.Com mon.DbDataAdapt er.Fill(DataSet dataSet)
      > at Procent.DataJox .Northwind.GetA llCustomers() in C:\Documents and
      > Settings\A
      > dministrator\My Documents\Visua l Studio\Projects \DataJox\DataJo x\Class1.cs:
      > line 21
      > at rock.Program.Ma in(String[] args) in C:\Documents and
      > Settings\Admini strato
      > r\My Documents\Visua l Studio\Projects \DataJox\rock\P rogram.cs:line 14
      >
      >
      >
      > using System;
      >
      > using System.Collecti ons.Generic;
      >
      > using System.Text;
      >
      > using System.Data;
      >
      > using System.Data.Sql Client;
      >
      > namespace Procent.DataJox
      >
      > {
      >
      > public class Northwind
      >
      > {
      >
      > private SqlConnection dbconn;
      >
      >
      > public DataSet GetAllCustomers ()
      >
      > {
      >
      > Open();
      >
      > DataSet ds = new DataSet();
      >
      >
      > SqlDataAdapter da = new SqlDataAdapter( );
      >
      > da.SelectComman d = new SqlCommand("Cus tListAll", dbconn);
      >
      > da.Fill(ds);
      >
      > return ds;
      >
      > }
      >
      >
      >
      > private void Open()
      >
      > {
      >
      > string connstr = "server=localho st;database=nor thwind;UID=sa;P WD=kk";
      >
      > try
      >
      > {
      >
      > SqlConnection dbconn = new SqlConnection(c onnstr);
      >
      > dbconn.Open();
      >
      > }
      >
      > catch
      >
      > {
      >
      > throw;
      >
      > }
      >
      > }
      >
      >
      > }
      >
      >
      > }[/color]

      Comment

      • Lasse Edsvik

        #4
        Re: Simple database class error

        Spidey,

        thx, one step further til next error :)

        I still get NullReferenceEx ception, but on this line:

        da.SelectComman d.Connection = dbconn;

        not sure what's wrong here



        using System;

        using System.Collecti ons.Generic;

        using System.Text;

        using System.Data;

        using System.Data.Sql Client;

        namespace Procent.DataJox

        {

        public class Northwind

        {

        private SqlConnection dbconn;


        public DataSet GetAllCustomers ()

        {

        Open();

        DataSet ds = new DataSet();


        SqlDataAdapter da = new SqlDataAdapter( );

        da.SelectComman d.Connection = dbconn;

        da.SelectComman d = new SqlCommand("Cus tListAll");

        da.Fill(ds);

        return ds;

        }



        private void Open()

        {

        string connstr = "server=localho st;database=nor thwind;UID=sa;P WD=kk";


        try

        {

        dbconn = new SqlConnection(c onnstr);

        dbconn.Open();

        }

        catch

        {

        throw;

        }

        }


        }


        }




        "Spidey" <sarin.rajendra n@gmail.com> wrote in message
        news:1131461296 .546007.267620@ g44g2000cwa.goo glegroups.com.. .[color=blue]
        > The line
        > SqlConnection dbconn = new SqlConnection(c onnstr);
        > in the Open method is the problem.
        > Replace this line with
        > dbconn = new SqlConnection(c onnstr);
        >
        >
        > What's happenning here is that a connection object is being created,
        > assigned to and opened properly in the Open method. However, this is
        > being assigned to the local variable dbconn (because of the
        > SqlConnection dbconn declaration inside the Open method). This local
        > dbconn variable is valid only within the Open method. The dbconn
        > variable referred to in the GetAllCustomers method is the private
        > variable dbconn which has never been assigned to.
        >
        > Regards,
        > Sarin.
        >
        > Lasse Edsvik wrote:[color=green]
        > > Hello
        > >
        > > Im trying to create a simple testclass that connects to a db on[/color][/color]
        localhost[color=blue][color=green]
        > > and a method that returns a dataset. I get these errors:
        > >
        > > Unhandled Exception: System.InvalidO perationExcepti on: Fill:
        > > SelectCommand.C onne
        > > ction property has not been initialized.
        > > at System.Data.Com mon.DbDataAdapt er.GetConnectio n3(DbDataAdapte r[/color][/color]
        adapter,[color=blue][color=green]
        > > IDb
        > > Command command, String method)
        > > at System.Data.Com mon.DbDataAdapt er.FillInternal (DataSet dataset,
        > > DataTable[]
        > > datatables, Int32 startRecord, Int32 maxRecords, String srcTable,
        > > IDbCommand co
        > > mmand, CommandBehavior behavior)
        > > at System.Data.Com mon.DbDataAdapt er.Fill(DataSet dataSet, Int32
        > > startRecord,
        > > Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior
        > > behavior)
        > >
        > > at System.Data.Com mon.DbDataAdapt er.Fill(DataSet dataSet)
        > > at Procent.DataJox .Northwind.GetA llCustomers() in C:\Documents and
        > > Settings\A
        > > dministrator\My Documents\Visua l[/color][/color]
        Studio\Projects \DataJox\DataJo x\Class1.cs:[color=blue][color=green]
        > > line 21
        > > at rock.Program.Ma in(String[] args) in C:\Documents and
        > > Settings\Admini strato
        > > r\My Documents\Visua l Studio\Projects \DataJox\rock\P rogram.cs:line 14
        > >
        > >
        > >
        > > using System;
        > >
        > > using System.Collecti ons.Generic;
        > >
        > > using System.Text;
        > >
        > > using System.Data;
        > >
        > > using System.Data.Sql Client;
        > >
        > > namespace Procent.DataJox
        > >
        > > {
        > >
        > > public class Northwind
        > >
        > > {
        > >
        > > private SqlConnection dbconn;
        > >
        > >
        > > public DataSet GetAllCustomers ()
        > >
        > > {
        > >
        > > Open();
        > >
        > > DataSet ds = new DataSet();
        > >
        > >
        > > SqlDataAdapter da = new SqlDataAdapter( );
        > >
        > > da.SelectComman d = new SqlCommand("Cus tListAll", dbconn);
        > >
        > > da.Fill(ds);
        > >
        > > return ds;
        > >
        > > }
        > >
        > >
        > >
        > > private void Open()
        > >
        > > {
        > >
        > > string connstr = "server=localho st;database=nor thwind;UID=sa;P WD=kk";
        > >
        > > try
        > >
        > > {
        > >
        > > SqlConnection dbconn = new SqlConnection(c onnstr);
        > >
        > > dbconn.Open();
        > >
        > > }
        > >
        > > catch
        > >
        > > {
        > >
        > > throw;
        > >
        > > }
        > >
        > > }
        > >
        > >
        > > }
        > >
        > >
        > > }[/color]
        >[/color]


        Comment

        • Bjorn Abelli

          #5
          Re: Simple database class error


          "Lasse Edsvik" wrote...
          [color=blue]
          > thx, one step further til next error :)
          >
          > I still get NullReferenceEx ception, but on this line:
          >
          > da.SelectComman d.Connection = dbconn;
          >
          > not sure what's wrong here
          >[/color]
          [color=blue]
          > SqlDataAdapter da = new SqlDataAdapter( );
          > da.SelectComman d.Connection = dbconn;
          > da.SelectComman d = new SqlCommand("Cus tListAll");[/color]

          Maybe you should shift the order of statements...

          SqlDataAdapter da = new SqlDataAdapter( );
          da.SelectComman d = new SqlCommand("Cus tListAll");
          da.SelectComman d.Connection = dbconn;

          // Bjorn A


          Comment

          • Lasse Edsvik

            #6
            Re: Simple database class error

            Bjorn,

            thx very much....... :)

            class seem fine now though........ Just cant get it to display in a listbox
            or whatever :)

            cant get that DataBind to show up for the object im trying to populate.


            what's more needed to do besides:

            public Form1()

            {

            InitializeCompo nent();

            Northwind n = new Northwind();

            listBox1.DataSo urce = n.GetAllCustome rs();


            }





            "Bjorn Abelli" <bjorn_abelli@D oNotSpam.hotmai l.com> wrote in message
            news:%23fgeHiH5 FHA.3636@TK2MSF TNGP09.phx.gbl. ..[color=blue]
            >
            > "Lasse Edsvik" wrote...
            >[color=green]
            > > thx, one step further til next error :)
            > >
            > > I still get NullReferenceEx ception, but on this line:
            > >
            > > da.SelectComman d.Connection = dbconn;
            > >
            > > not sure what's wrong here
            > >[/color]
            >[color=green]
            > > SqlDataAdapter da = new SqlDataAdapter( );
            > > da.SelectComman d.Connection = dbconn;
            > > da.SelectComman d = new SqlCommand("Cus tListAll");[/color]
            >
            > Maybe you should shift the order of statements...
            >
            > SqlDataAdapter da = new SqlDataAdapter( );
            > da.SelectComman d = new SqlCommand("Cus tListAll");
            > da.SelectComman d.Connection = dbconn;
            >
            > // Bjorn A
            >
            >[/color]


            Comment

            Working...