Click here to Skip to main content
15,887,331 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
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.
Posted
Comments
RavonX 24-Oct-12 16:09pm    
wouldn't you just need to create a new sub to handle starfields and send it a couple parameters from each datarow?

for instance:

foreach (DataRow row in table.Rows)
{
CreateStar(row.item(0), row.item(1), ...)
}

~~~~~~~~~~ then a new sub ~~~~~~~~~~
private void CreateStar(byval myinfo1 as object, byval myinfo2 as object, ...)
{
''' add code snippet 2 here and use parameters passed from the table rows
}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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