0 Replies - 2148 Views - Last Post: 05 September 2012 - 10:33 AM Rate Topic: -----

#1 rbflowers  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 48
  • Joined: 10-February 12

array/database pull help

Posted 05 September 2012 - 10:33 AM

Hey guys,
I'm working in an asp.net mvc3 project on visual studio. The web app is a display of 20 metrics. Currently, the data for the metrics is collected by email and I have to go in and edit them individually.

I've set up a database where users can update their own information, and so I'd like to be able to link the information from the database straight to my .cshtml pages.

So, instead of doing something like this and having to update manually:
<div class="extras">
            <h1>Operational Definition:</h1>
                <p>long definition here </p>
            <h1>Objective:</h1><p>Objective text goes here</p>
            <h1>Strategy:</h1><p>Strategy text goes here.</p>
        </div>
        <div class="extras">
            <h1>Key Improvements:</h1><ol><li>Improvement 1</li><li>Improvement 2</li><li>Improvement 3</li></ol>
            <h1>Key issues:</h1><ol><li>Issue 1</li><li>Issue 2</li><li>Issue 3</li></ol>
        </div>
        <div class="extras">
            <div class="takeaways"><h1>Key Take-aways</h1>
                <ol><li>Takeaway1 </li><li>Takeaway2 </li><li>Takeaway3 </li></ol>
            </div>


I can simply call whatever the user put into the database.


Here's what I have so far.
I created a .sdf database in my visual studio project following this example:
http://www.asp.net/m...to-aspnet-mvc-3

This created the database and created a link where users can go and edit their information.

I'm pretty unfamiliar with databases and asp.net in general, but this is what I have so far:

@using WebMatrix.Data;

@{//title of tab
    ViewBag.Title = "All Metrics";
    var DB = Database.Open("Metrics") ;
}

<div>
@{
    string[] metrics;
    foreach (var row in DB.Query("SELECT * FROM Metrics"))
    {
        metrics = @row.MetricName;
    }

    for (var i = 0; i < metrics.Length; i++)
    {
        <p>metric = @metrics[i]</p>
    }
}
    
</div>


The thought is that if I can pull into an array, then I can simply call whichever information I need like this:

<div id="title"> @metrics[0] </div>
<div> all other information about the first metric here</div>


the above code gave me this error:

Quote

Compiler Error Message: CS0165: Use of unassigned local variable 'metrics'

Source Error:


Line 17: }
Line 18:
Line 19: for (var i = 0; i < metrics.Length; i++)
Line 20: {
Line 21: <p>metric = @metrics[i]</p>


I'm assuming the call I have here:
foreach (var row in DB.Query("SELECT * FROM Metrics"))
{
metrics = @row.MetricName;
}
Is not adding anything to the metrics array, but haven't found another way to add elements to arrays in razor.

Is there an easier way to do this? Is it possible to just do something like this:

<div id="title">databaseMetricName[0]</div>


Thanks

This post has been edited by rbflowers: 05 September 2012 - 10:41 AM


Is This A Good Question/Topic? 0
  • +

Page 1 of 1