afranka
04-24-2003, 10:55 AM
Hi,
I am trying to create a webpage that allows a user to download data from a database in csv format.
I have created the following by nicking bits from books but am getting an error when I try to close or flush the stream which I think I need to do:
CS1519: Invalid token '(' in class, struct, or interface member declaration.
Please can anyone have a look through this code and tell me what I am doing wrong?
I'd also be happy if anyone can help me improve this or point out anything that I am doing wrong!!! I did originally have the text writing to scren to check what was going on, so that is probably still lurking in there!
I'd ideally also like to name the file based on the user's login id.
Should I use original asp's request.servervariables to do this and if so how do I code it so it doesn't annoy c#? Is there a .net alternative I should use?
Thanks very much!
al.
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script Language="c#" runat="server">
void Page_Load()
{
string fileOutName = "c:\\bulkfile2.txt";
FileStream FileOut = new FileStream(fileOutName, FileMode.OpenOrCreate, FileAccess.Write);
BufferedStream bufStream = new BufferedStream(FileOut);
StreamWriter sWriter = new StreamWriter(bufStream);
string strSQL = "SELECT Name, Surname, office_name_short FROM WMDatabase;";
string strConnection = "user id=test;password=testing;initial catalog=people;data source=testdatabase;";
strConnection += "Connect Timeout=30";
DataSet objDataSet = new DataSet();
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
SqlDataAdapter objDataAdapter = new SqlDataAdapter(strSQL, objConnection);
objDataAdapter.Fill(objDataSet, "Employees");
DataTable objTable = objDataSet.Tables["Employees"];
foreach(DataRow objDataRow in objTable.Rows)
{
message.Text+=(objDataRow["name"].ToString() + " ");
message.Text+=(objDataRow["surname"].ToString() + ", ");
message.Text+=(objDataRow["office_name_short"].ToString() + "<BR>");
string line = message.Text;
sWriter.WriteLine(line);
message.Text = "";
}
}
sWriter.Flush();
sWriter.Close();
</script>
<html>
<body>
<asp:label id=message runat="server" />
<a href="/csv/bulkfile4.csv">test</a>
</body>
</html>
I am trying to create a webpage that allows a user to download data from a database in csv format.
I have created the following by nicking bits from books but am getting an error when I try to close or flush the stream which I think I need to do:
CS1519: Invalid token '(' in class, struct, or interface member declaration.
Please can anyone have a look through this code and tell me what I am doing wrong?
I'd also be happy if anyone can help me improve this or point out anything that I am doing wrong!!! I did originally have the text writing to scren to check what was going on, so that is probably still lurking in there!
I'd ideally also like to name the file based on the user's login id.
Should I use original asp's request.servervariables to do this and if so how do I code it so it doesn't annoy c#? Is there a .net alternative I should use?
Thanks very much!
al.
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<script Language="c#" runat="server">
void Page_Load()
{
string fileOutName = "c:\\bulkfile2.txt";
FileStream FileOut = new FileStream(fileOutName, FileMode.OpenOrCreate, FileAccess.Write);
BufferedStream bufStream = new BufferedStream(FileOut);
StreamWriter sWriter = new StreamWriter(bufStream);
string strSQL = "SELECT Name, Surname, office_name_short FROM WMDatabase;";
string strConnection = "user id=test;password=testing;initial catalog=people;data source=testdatabase;";
strConnection += "Connect Timeout=30";
DataSet objDataSet = new DataSet();
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
SqlDataAdapter objDataAdapter = new SqlDataAdapter(strSQL, objConnection);
objDataAdapter.Fill(objDataSet, "Employees");
DataTable objTable = objDataSet.Tables["Employees"];
foreach(DataRow objDataRow in objTable.Rows)
{
message.Text+=(objDataRow["name"].ToString() + " ");
message.Text+=(objDataRow["surname"].ToString() + ", ");
message.Text+=(objDataRow["office_name_short"].ToString() + "<BR>");
string line = message.Text;
sWriter.WriteLine(line);
message.Text = "";
}
}
sWriter.Flush();
sWriter.Close();
</script>
<html>
<body>
<asp:label id=message runat="server" />
<a href="/csv/bulkfile4.csv">test</a>
</body>
</html>