Click here to Skip to main content
15,921,643 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My code as follows

C#
int count = 0;
string connectionstring = "Server=(local);initial catalog=Test;Trusted_Connection=True";
SqlConnection sqlConnection = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
DataSet ds = new DataSet();
cmd.CommandText = "select * from Empdetails";
cmd.CommandText += " where shifttype = @par ";
cmd.Parameters.Add("@par", SqlDbType.Int).Value = j;
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
if (reader.HasRows)
{
	string filePath = @"C:\Users\God\Desktop\DataDump\" + j + "Excel.xls";
	System.IO.StreamWriter sw_In = new System.IO.StreamWriter(filePath);
	while (reader.Read())
	{
		if (count == 0)
		{
			for (int i = 0; i < reader.FieldCount; i++)
			{
				sw_In.AutoFlush = true;
				sw_In.Write(reader.GetName(i) + "\t");
			}
			sw_In.Write("\n");
			count = 1;
		}
		for (int i = 0; i < reader.FieldCount; i++)
		{
			sw_In.AutoFlush = true;
			sw_In.Write(reader[i].ToString() + "\t");
		}
		sw_In.Write("\n");
	}
}
reader.Close();
sqlConnection.Close();


when i run the above code two excel file is downloaded in desktop under C Folder as follows

1Excel
2Excel

i want the above Excel file name to retrieved and displayed.

for that how can i do to retireve the excel file name and to be displayed
Posted
Updated 28-Aug-16 9:08am
v3
Comments
Maciej Los 28-Aug-16 11:26am    
What?
Richard MacCutchan 28-Aug-16 11:42am    
Why do you keep doing this? How many times do you need this explained to you - you are not creating Excel files.

You use the System.IO.tory.GetFiles("path\\to\\files"). That return a string array to file names.
 
Share this answer
 
I must be missing something big or you don't understand the code you supposed to have written.

I don't understand why you have a problem retrieving the file names, because this code is creating the files, and to create the files, the code gives the file names.
Here, you build the full file name before creating the file:
C#
string filePath = @"C:\Users\God\Desktop\DataDump\" + j + "Excel.xls";

The path is C:\Users\God\Desktop\DataDump\
The file name is j + "Excel"
The extension is ".xls"

Nota: your code is complicated and can be simplified to:
C#
for (int i = 0; i < reader.FieldCount; i++)
{
    sw_In.AutoFlush = true;
    sw_In.Write(reader.GetName(i) + "\t");
}
sw_In.Write("\n");
while (reader.Read())
{
    for (int i = 0; i < reader.FieldCount; i++)
    {
        sw_In.AutoFlush = true;
        sw_In.Write(reader[i].ToString() + "\t");
    }
    sw_In.Write("\n");
}
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900