Click here to Skip to main content
15,913,055 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hello,

I am using this code but its getting error:- system.io.streamwriter' does not contain a constructor that takes 0 arguments.
C#
public void ExportDetails()
{
 SqlConnClass connClass = new SqlConnClass();
        DataTable dt = connClass.GetQrCodeDetails();
        GridView gv = new GridView();
        gv.AutoGenerateColumns = false;

        BoundField ID = new BoundField();
        ID.HeaderText = "ID";
        ID.DataField = "ID";
        gv.Columns.Add(ID);

        BoundField QRIMAGE = new BoundField();
        IMAGE.HeaderText = "IMAGE";
        IMAGE.DataField = "IMAGE";
        gv.Columns.Add(IMAGE);

        StreamWriter sw = new StreamWriter(); //Error is showing in this line.
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        gv.RenderControl(htw);
        Response.Write(htw);
ExportTableData(sw.ToString());
}

public void ExportTableData(string data)
    {
        string attach = "attachment;filename=CodeDetails.xls";
        Response.ClearContent();
        Response.AddHeader("content-disposition", attach);
        Response.ContentType = "application/ms-excel";

        Response.Write(data);
        Response.End();
    }


please help me.
How to resolve this error.

Thanks in Advance.

Ankit Agarwal
Software Engineer
Posted
Updated 25-Sep-15 20:11pm
v3
Comments
Mario Z 26-Sep-15 3:05am    
When initializing StreamWriter you need to provide at least a stream or a file's path to which you want to write:
StreamWriter Constructors[^]

However looking at your code I don't see a reason why you want to use it, your ExportTableData method does the desired writing.

Also just as an FYI, note that you are actually creating a file with an XLS extension but it has a HTML content. You see this trick works because MS Excel is able to figure out that it's a HTML file instead and it will open it, but it will also promp a warning saying that a file format and extension don't match.

1 solution

Yes the message is quite clear.

To know about all the constructors - StreamWriter Class[^].
 
Share this answer
 

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