2 Replies - 1162 Views - Last Post: 17 October 2012 - 01:01 AM Rate Topic: -----

#1 rbflowers  Icon User is offline

  • New D.I.C Head

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

write to excel problem

Posted 17 October 2012 - 12:52 AM

Hi, I've added a comment portion to my website. I originally built it using a SQL database to store the comments, but I was having all kinds of IIS problems, so I figured I'd just read in and write to an excel csv file.

The read in part is working fine, I'm having problems with the writing. Here's the comment page:

@{//title of tab
    ViewBag.Title = "Comments for Availability (ATB)";
}


@functions {             
        string returnNeededString(int which, string larger){
            //1 = time, 2 = name, 3 = email, 4 = comment
            var neededString = ""; int num = 0;
            for (int i = 0; i < larger.Length; i++){
                if (num == which && larger[i] != ','){
                    if(larger[i] == '`') { //if someone's comment is hi, how's it going. it gets stored as hi` how's it going
                        neededString += ',';
                    }
                    else {
                        neededString += larger[i];
                    }
                }
                else if (num > which){
                    return neededString;
                }
                if(larger[i] == ','){
                    num++;
                }
            }
            return "";
        }
             
        string returnTime(string time)
        {
            return returnNeededString(1,time);
        }
        string returnName(string name)
        {
            return returnNeededString(2, name);
        }
        string returnEmail(string email)
        {
            return returnNeededString(3, email);
        }
        string returnComment(string comment)
        {
            return returnNeededString(4, comment);
        }
    }

@{
    var name = ""; var email = ""; var comment = ""; DateTime time = DateTime.Now; var metric = ""; int likes = 0;

    if(IsPost){
        name = Request.Form["name"];
        if (name == ""){
            name = "Anonymous";
        }
        email = Request.Form["email"];
        comment = Request.Form["comment"];
        time = DateTime.Now;
        metric = "Availability (ATB)";



        FileInfo t = new FileInfo("~/App_Data/Comments.csv");
        StreamWriter Tex = t.CreateText();
        Tex.WriteLine(metric + "," + time + "," + name + "," + email + "," + comment + "," + likes);
        Tex.Close();
        
        Response.Redirect("/Home/CommentsATB");
    }
}

<div class="left">
<h1>Add a comment</h1>
    <form action ="" method="post">      
            <label for="name">Name:</label>
             <input type="text" name="name" value="@Request.Form["name"]" />

          <label for="email">Email:</label>
             <input type="text" name="email" value="@Request.Form["email"]" />

             <br />
          <label for="comment">Comment:</label><br />
            <textarea rows="3" cols="59" name = "comment" value="@Request.Form["comment"]"></textarea>
          <br />
          <center>
          <input type="submit" name="buttonsubmit" value="Add Comment" /><input id="reset" type="reset" name="reset" value="Start Over" />
          </center>
    </form>
</div>

<div class="right">
<h1>Recent Comments about Availability (ATB)</h1>
    @{
        var dataFile = Server.MapPath("~/App_Data/Comments.csv");
        Array userData = File.ReadAllLines(dataFile);
        List<string> ATB = new List<string>();
        foreach (string dataLine in userData) 
        {
            foreach (string dataItem in dataLine.Split(','))
            {
                if (dataItem == "Availability (ATB)")
                {
                    ATB.Add(dataLine);
                }
            }
        }
        
        var commentsLength = ATB.Count;

        <p style="margin-left:250px;">There are <span style="font-size:18px; font-weight:bold;">@commentsLength</span> total comments</p>

        foreach (var i in ATB)
        {<br />
            <div class="comment">
                <div class="commentUpper">
                    <div class="commentUpperLeft"><a href="mailto:@returnEmail(i)?subject=Response to your comment about Availability&body=This is in response to your comment: '@returnComment(i)' about Availability">@returnName(i)</a></div>
                    <div class="commentUpperMid"><a style="color:red;" href="mailto:rflowers@gmail.com?subject=Report Comment about Availability&body=I find this comment: '@returnComment(i)' inappropriate! Thank you for reporting">Report</a></div><!--should put time here-->
                    <div class="commentUpperRight">@returnTime(i)</div>
                </div>
                <hr />
                <p class="commentText">@returnComment(i)</p> 
            </div>
        }
    }

</div>




When I try to submit a comment, I get the following error:

Quote

Server Error in '/' Application.

Could not find a part of the path 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\~\App_Data\Comments.csv'.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IO.DirectoryNotFoundException: Could not find a part of the path 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\10.0\~\App_Data\Comments.csv'.

Source Error:


Line 61:
Line 62: FileInfo t = new FileInfo("~/App_Data/Comments.csv");
Line 63: StreamWriter Tex = t.CreateText();
Line 64: Tex.WriteLine(metric + "," + time + "," + name + "," + email + "," + comment + "," + likes);
Line 65: Tex.Close();

Source File: c:\Users\rflowers\Documents\Visual St...


Any idea what's going on?

Is This A Good Question/Topic? 0
  • +

Replies To: write to excel problem

#2 Mina-no-Hime  Icon User is offline

  • D.I.C Head

Reputation: 98
  • View blog
  • Posts: 176
  • Joined: 23-August 12

Re: write to excel problem

Posted 17 October 2012 - 12:56 AM

Your error tells you exactly what the problem is - it cannot find a file called "~/App_Data/Comments.csv" when you create your FileInfo variable. This is probably because there is no ~/App_Data folder. What have you tried thus far in order to resolve your issue?
Was This Post Helpful? 0
  • +
  • -

#3 rbflowers  Icon User is offline

  • New D.I.C Head

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

Re: write to excel problem

Posted 17 October 2012 - 01:01 AM

There is an App_Data folder. It reads in my self written comments from the file, calling the exact same file path, so the path isn't incorrect...

I tried all different variations of ~/App_data/Comments.csv (../App_data/Comments.csv, /../App_data/Comments.csv, /Comments.csv , etc) but none worked
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1