Reputation: 271
I am new to C# and ASP.Net, and I am running into this error
Compiler Error Message: CS0246: The type or namespace name 'StreamWriter' could not be found (are you missing a using directive or an assembly reference?)
Here is my code Currently:
<% @Page Language="C#" %>
<!-- code section -->
<script runat="server">
private void WriteUpper(object sender, EventArgs e)
{
string str = mytext.Value;
using (StreamWriter outputFile = new StreamWriter(@"~\AppData\data.txt", true)) {
outputFile.WriteLine(str);
}
}
</script>
<!-- Layout -->
<html>
<head>
<title> Untitled </title>
</head>
<body>
<form id="Form1" runat="server">
<input runat="server" id="mytext" type="text" />
<input runat="server" id="button1" type="submit" value="Enter..." OnServerClick="WriteUpper"/>
<hr />
</form>
</body>
</html>
Upvotes: 1
Views: 1325
Reputation: 1092
StreamWriter is in the System.IO namespace. Try adding a
#using System.IO;
to your page.
Upvotes: 1
Reputation: 5943
I have never added namespaces in cshtml(razor) code but you could just use the entire namespace when initializing like so:
using (System.IO.StreamWriter outputFile = new System.IO.StreamWriter(@"~\AppData\data.txt", true))
let me know if that helps.
Upvotes: 1