Anyways I managed to figure out the solution. I executed the query and saved it in a Dataset. Read all the results from dataset and saved the result comma separated in a string. When the user clicks the button the query will be executed and they will be prompted to save the output.
String str; // this has the results of query in csv
string file_name; // name of the file,
Response.Clear();
Response.ContentType = "text/csv";
Response.AddHeader("Content-disposition", "attachment; filename=\"" + file_name + "\"");
Response.Write(str);
Response.Flush();
Response.End();
solved How to run a sql query in C# and asp.net and show the result in csv file locally [duplicate]