none
data table help pls! RRS feed

  • Question

  •  

    hello, i'm having a little problem using datatables, i have never use one before, so i really don't have a idea in what im doing wrong, i'll really apreciate some help.

    so this is what i need to do:

    i need to make a query to a mysql server in c# im using mysqlconector, and i'm fine wiht the conections and with the queries, but now that im trying to put the info into a data table i get an error that says, the the reference for the object it's not established as a instance of the object.

    here is the code:

     

    Code Snippet

    public Form1()

    {

    gridCarga();

    InitializeComponent();

    }

    private void gridCarga()

    {

    try

    {

    MySqlConnection con = new MySqlConnection("Database=labuena;Data Source=localhost;User Id=root;Password=****");

    con.Open();

    string selection = "select * from dnincide";

    MySqlCommand cmd = con.CreateCommand();

    cmd.CommandText = selection;

    MySqlDataAdapter da = new MySqlDataAdapter();

    da.SelectCommand = cmd;

    DataSet ds = new DataSet();

    da.Fill(ds, "dnincide");

    DataTable dt = ds.Tables["dnincide"];

    label1.Text = dt.Rows[1].ToString();

    con.Close();

     

    }

    catch (MySqlException ex)

    {

    MessageBox.Show("Error en la tabla de incidencias" + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    catch (NullReferenceException e)

    {

    MessageBox.Show("No hay datos en la tabla DNINCIDE : "+e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

     

     

    i have also try something more simple like only:

     

    Code Snippet

     

    try

    {

    MySqlConnection con = new MySqlConnection("Database=labuena;Data Source=localhost;User Id=root;Password=****");

    con.Open();

    MySqlCommand cmd = new MySqlCommand("SELECT * FROM dnincide", con);

    MySqlDataAdapter da = new MySqlDataAdapter(cmd);

    DataSet ds = new DataSet();

    da.Fill(ds);

    con.Close();

    }

    catch (MySqlException ex)

    {

    MessageBox.Show("Error en la tabla de incidencias" + ex, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

    catch (NullReferenceException e)

    {

    MessageBox.Show("No hay datos en la tabla DNINCIDE : " + e.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);

    }

     

     

    but i keep getting nullreferenceExceptions each time i run my aplication, where is mi wrong?, 'cause i have read a lot and every where says the same way to do it, and its not working for me Tongue Tied, i'll really apreciate any help, or a source from where i can read how to do it or something to keep goin.

    tnks Big Smile

    Farewell...

    Monday, September 24, 2007 9:33 PM

All replies

  • Which line does it throw the exception on? Run it in the debugger and check each of the objects to see if they are null. I suspect the problem will exist on the da.fill() .


    Monday, September 24, 2007 9:44 PM
  • does SELECT * FROM dnincide return any rows?

     

    dt.Rows[1].ToString() -- This assumes that the datatable has at least 2 rows in it, you can be getting the null acception because there isnt any data at the second postion in the row set (remember C# deals with arrays with a 0 index, so Rows[1] is really the second item in the array)

     

    If you still have issues, I suggest using DataSets and TableAdapters and having Visual Studio (hopefully 2005) generate the classes for you.  This way you will have a data object as well as a collection of objects.  From there you can jsut do

     

    label1.text = MyObject.Name;

     

    here is a link that will explain TableAdapters

     

    http://msdn2.microsoft.com/en-us/library/bz9tthwx(VS.80).aspx

     

     

    Monday, September 24, 2007 9:45 PM