Click here to Skip to main content
15,896,201 members

Fetching values from a DataSet.table?

Frank R. Haugen asked:

Open original thread
So this is a noobish question but I'm failing to get the correct results.

I have a table (Data Table 1) in a DB that I have successfully connected to and extracted to a DataSet. Now I used the MSDN-website provided example code to test the information, and the code (Code Snippet 1) prints out every piece of data flawlessly by going trough every row and and every column on the row and adding them to a string, (used only for testing purposes).

What I want is for a set of variables to be "edited" which is used in a function (Code Snippet 2) to ultimately add a new XAML element, which at some point will be a dynamic starmap.

I basically want to have a loop which populates a canvas with ellipses based on the information in the database's table's rows.

The code below is messy as hell, and just plain stupid in some places but It works, giving me a message box with the DB-table's contents in one long string. The data-table below is just for reference and are only a few columns of a whole lot.

thanks for any and all help!

Code snippet 1:
C#
private void TestDBconnection()
{

    //Connecting to database
    string connetionString = null;
    OleDbConnection DbConn;
    connetionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Frank R. Haugen\Documents\Visual Studio 2010\Projects\StarApp\WpfApplication1\10_closest_stars.accdb;"; //TODO: make path relative
    DbConn = new OleDbConnection(connetionString);

    //SQL query string
    string SqlQueryString = "SELECT * FROM [10 closest stars]";

    //open connection to database
    DbConn.Open();

    //creat a dataset to hold all values
    DataSet myDataSet = new DataSet();

    //Setting up and starting connection to database-file
    OleDbConnection objConnection = new OleDbConnection(connetionString);
    OleDbCommand objCommand = new OleDbCommand(SqlQueryString, objConnection);
    objConnection.Open();

    //adapts the data from the database-file to a DataSet
    OleDbDataAdapter myDataAdapter = new OleDbDataAdapter(objCommand);
    myDataAdapter.Fill(myDataSet, "[10 closest stars]");

    string Response = "";

    //testing
    foreach (DataTable table in myDataSet.Tables)
    {
        foreach (DataRow row in table.Rows)
        {
            foreach (DataColumn column in table.Columns)
            {
                Response += row[column] + ";";
            }

        //TODO: add function to add a new ellipse/star here

        }
    }

    MessageBox.Show(Response);

    objConnection.Close();

    //close connection to database
    DbConn.Close();
}


Data Table 1:
ID	HYG	GLIESE	SERV	
32263	32349	Gl 244  A	
53879	54035	Gl 411	NULL	
70667	70890	Gl 551	NULL	
71454	71681	Gl 559  B	
71457	71683	Gl 559  A	
87666	87937	Gl 699	NULL	
92116	92403	Gl 729	NULL	
118081	0	Gl  65  B	


Code Snippet 2:
C#
var tool = new StackPanel();
tool.Children.Add(new TextBlock() { Text = "Proxima Centauri" });
tool.Children.Add(new TextBlock() { Text = "-1,538676906;-1,178494413;-3,752088504" });

var star = new Ellipse
{
    Height = 5,
    Width = 5,
    Fill = Brushes.Blue,
    RenderTransform = new TranslateTransform(-2, -2),
    ToolTip = tool,
    Cursor = Cursors.Hand
};
star.SetValue(Canvas.LeftProperty, 60.0);   //left-right distance from center in pixles
star.SetValue(Canvas.TopProperty, 20.0);    //top-bottom distance from center in pixles
star.SetValue(Canvas.ZIndexProperty, -500); //

starfield.Children.Add(star);

This code is not yet been made compatible with the above so it has absolute values instead of referring to variables.
Tags: C#, Visual Studio (Visual Studio 2010), Desktop Programming, OleDb

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900