ASP.NET - to write data in notepad - Asked By anbu n on 17-Sep-11 06:01 AM

here the code i 've used...,

//this.BindGvDetails();
            this.BindQuesAns();

            // Let's output HTML of GridView
            Response.Clear();
            Response.ContentType = "text/csv";


            //Response.AppendHeader("Content-Type", "application/vnd.text");

            Response.AddHeader("content-disposition","attachment;filename=Ques_Answer.txt");
            Response.Charset = "";
            Response.Cache.SetCacheability(HttpCacheability.NoCache);

            StringWriter swriter = new StringWriter();
            HtmlTextWriter hwriter = new HtmlTextWriter(swriter);

            HtmlForm frm = new HtmlForm();

            this.GvQues_Ans_user.Parent.Controls.Add(frm);

            frm.Attributes["runat"] = "server";
            frm.Controls.Add(this.GvQues_Ans_user);
            frm.RenderControl(hwriter);
            

            Response.Write(swriter.ToString());
            Response.End();
-------------------------
the above code generates data from gridview to notepad....

but the problem is ... it looks as below....

<form name="aspnetForm" method="post" action="Ques_Answer.aspx" id="aspnetForm" runat="server">
<div>
<input type="hidden" name="__VIEWSTATE" id="
__VIEWSTATE" value="" />
</div>
<div>
    <table cellspacing="0" rules="all" border="1" id="ctl00_ContentPlaceHolder1_GvQues_Ans_user" style="border-collapse:collapse;">
        <tr>
            <th scope="col" style="display:none;">UserId</th><th scope="col" style="display:none;">QuestionId</th><th scope="col" style="display:none;">CustomerId</th><th scope="col">Conducted by</th><th scope="col">Question</th><th scope="col">Answer</th><th scope="col">Customer</th>
        </tr><tr>
            <td align="center" style="display:none;">3</td><td align="center" style="display:none;">5</td><td align="center" style="display:none;">1</td><td align="center">Senthil</td><td align="center">What is my ip address what is my ip address what is my ip address</td><td align="center">Test Answer 2 Test Answer 2 Test Answer 2 ,  Test Answers Test Answers Test Answers hai</td><td align="center">saravanan</td>
        </tr><tr>
            <td align="center" style="display:none;">3</td><td align="center" style="display:none;">6</td><td align="center" style="display:none;">1</td><td align="center">Senthil</td><td align="center">Question 2</td><td align="center">Answer 1,  Answer2,  Answer3,  Answer4,  Answer5</td><td align="center">saravanan</td>
        </tr>
    </table>
</div></form>
-----------

can anybody give idea how write only data by omitting html tags


dipa ahuja replied to anbu n on 17-Sep-11 06:46 AM
Untitled document
  protected void Page_Load(object sender, EventArgs e)
   {
     HttpBrowserCapabilities browse = Request.Browser;
     /* Get Ip Addresss of User */
     string RemoteIp = Request.UserHostAddress.ToString();
     string DNSName = Request.UserHostName;
     string[] KeyBoardTypes = Request.UserLanguages;
     string OperatingSystem = Request.UserAgent;
 
     Response.Write("</br>Remote Ip:" + RemoteIp);
     Response.Write("</br>DNS Name :" + RemoteIp);
 
     /* Print languages Supported or Keyboard Type */
     foreach (string str in KeyBoardTypes)
     {
       Response.Write("</br>Keyboard Type :" + str);
     }
 
     Response.Write("</br>Operating System :" + OperatingSystem);
     Response.Write("<br>Your Browser name: " + browse.Browser);
     Response.Write("<BR>");
     Response.Write("Your Operating System: " + browse.Platform);
 
 
}
dipa ahuja replied to anbu n on 17-Sep-11 06:47 AM
Untitled document
using System.IO;
using System.Web.UI.HtmlControls;
 
protected void Button2_Click(object sender, EventArgs e)
{
   //Excel
   Response.Clear();
   Response.AddHeader("content-disposition", "attachment;filename=e1.txt");
   Response.Charset = "";
   Response.Cache.SetCacheability(HttpCacheability.NoCache);
   Response.ContentType = "application/vnd.xls";
 
   System.IO.StringWriter stringWrite = new System.IO.StringWriter();
   System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
 
 
   //if you have take linkButton then make it invisible
   GridView1.Columns[3].Visible = false;
 
   StringWriter swriter = new StringWriter();
   HtmlTextWriter hwriter = new HtmlTextWriter(swriter);
 
   HtmlForm frm = new HtmlForm();
   this.GridView1.Parent.Controls.Add(frm);
   frm.Attributes["runat"] = "server";
   frm.Controls.Add(GridView1);
   frm.RenderControl(hwriter);
 
   Response.Write(swriter.ToString());
   Response.End();
   //set linkButton    visible
   GridView1.Columns[3].Visible = true;
 
}
Reena Jain replied to anbu n on 17-Sep-11 06:55 AM
hi,

Like this using StreamWriter you can create notepad file

StreamWriter SW;
SW=File.CreateText("c:\\myfile.txt");
SW.WriteLine("First Line");
SW.WriteLine("Second line");
SW.Close();
Response.Write("File Created SucacessFully");

Hope this will help you