How to convert Gridview to word document

Abstract


Here we will discuss about how to convert Gridview to Word Document using asp.net C#.
Drag and drop the Grid view control to your web page and call the LoadData() on page load event.


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
LoadData();
}
}

private void LoadData()
{
try
{
SqlConnection con = new SqlConnection();
con = new SqlConnection("server=servername;uid=userid;password=password;database=databasename;");
con.Open();
string sql = ("select userid,username from Tablename");
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataSet resultData = new DataSet();
da.Fill(resultData);
GridView1.DataSource = resultData;
GridView1.DataBind();
con.Close();
if (resultData.Tables[0].Rows.Count > 0)
{
Convert(resultData, Response);
}

}
catch (Exception ex)
{
// Response.Write(ex.ToString());
}


}


public void Convert(DataSet ds, HttpResponse response)
{

//*********Export to Excel/Doc**************
//Let's clean up the response.object

response.Clear();
response.Charset = "";
response.ContentType = "application/msword"; // doc export
//create a string writer
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
//create an htmltextwriter which uses the stringwriter
HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
//instantiate a datagrid
DataGrid dg = new DataGrid();
//set the datagrid datasource to the dataset passed in
dg.DataSource = ds.Tables[0];
//bind the datagrid
dg.DataBind();
//tell the datagrid to render itself to our htmltextwriter
dg.RenderControl(htmlWrite);
//all that's left is to output the html
response.Write(stringWrite.ToString());
response.End();

}

}

What is the AppSetting Section in Web.config file?

What is the AppSetting Section in Web.config file ?



Web.Config file defines configuration for a web project.AppSetting is used to set the user defined values.

For Example:-
You can store the connectionstring.its used to through out the project for database connection.


<>
< key="ConnectionString" value="Provider=Providername;server=servername;uid=userid;pwd=password;database=databasename;">
< /appSettings >



In code behind you get the connection string like this

C#:-

string strConnection = ConfigurationSettings.AppSettings["ConnectionString"];

VB.Net:-

Dim strconn As String = ConfigurationManager.AppSettings("ConnectionString")

How to sort Datatable in C#

Abstract


Just Drag and drop the Grid view control to web page.
[code]
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataTable table = new DataTable();
table.Columns.Add("Id", typeof(string));
table.Columns.Add("name", typeof(string));
table.Columns.Add("Offer", typeof(string));
table.Columns.Add("City", typeof(string));

DataRow row = null;
for (int i = 0; i < 5; i++)
{
row = table.NewRow();
row["Id"] = i;
row["name"] = "Ram";
row["Offer"] = "Offer";
row["City"] = "City Ram";

table.Rows.Add(row);
}
DataView dataView = new DataView(table);
dataView.Sort = "Id DESC";
//dataView.Sort = "Id DESC";
grvgrid.DataSource = dataView;
grvgrid.DataBind();

}
}



[/code]

How to open a notepad with maximized size using asp.net

Abstract


Here we will discuss about how to open Note Pad with maximized size.You can use below code both windows and web application.You must import the System.Diagnostics name space in both windows and web application.

[code]
System.Diagnostics.ProcessStartInfo myProc = new System.Diagnostics.ProcessStartInfo();
myProc.FileName = "Notepad.exe";
myProc.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
System.Diagnostics.Process.Start(myProc);
[/code]

In Web application



[code]
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Diagnostics;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
System.Diagnostics.ProcessStartInfo myProc = new System.Diagnostics.ProcessStartInfo();
myProc.FileName = "Notepad.exe";
myProc.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
System.Diagnostics.Process.Start(myProc);

}

}



[/code]

In Windows Application



[code]
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
namespace WindowsFormsApplication3
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void Form1_Load(object sender, EventArgs e)
{

System.Diagnostics.ProcessStartInfo myProc = new System.Diagnostics.ProcessStartInfo();
myProc.FileName = "Notepad.exe";
myProc.WindowStyle = System.Diagnostics.ProcessWindowStyle.Maximized;
System.Diagnostics.Process.Start(myProc);

}
}
}

[/code]

How to get the ASCII value in C#

Abstract


Just Drag and drop the Text box and button control to your web page.In run time
give the input to Text box then click the button control the result will be display in your page.


[code]
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
int i = 0;
string strInput ="";
string strTemp ="";
strInput = TextBox1.Text.ToString().Trim();
for (i = 0; i <= strInput.Length - 1; i++)
{
int charValue = Convert.ToInt32(strInput[i]); // converting in ascii
if (strTemp == "")
{
Response.Write(charValue);
Response.Write("
");
}
else
{
Response.Write(charValue);
Response.Write("
");
}
}
}
}

[/code]