PDA

View Full Version : C#: creating csv from a database


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>

raf
04-24-2003, 12:36 PM
I'm absolutely no C# expert, but with fso, the close method fluches the cache and closes the file. It could be the same here, so i'd try it without the flush line.

afranka
04-24-2003, 12:40 PM
Thanks Raf,

I've tried both flush and close on their own but get the same error for each!

Happy to use either or both or none - just want to get it right!
Could it be the syntax?

Thanks again!

a.

afranka
04-24-2003, 06:36 PM
Hi,

Managed to fix it:

The Flush and Close bits were outside of the page_load function, moved them inside and it works a treat!

Still would like to know about the other bits if anyone has a mo!!

Thanks,
a.