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]

How to Maintain Dataset in ViewState

Abstract


Here we will discuss about how to maintain Data set in view state using vb.net.Just drag and drop the one grid view and button control to your web page.

In Aspx.vb



[code]
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Dim dtrow As DataRow
Dim dstDataSet As DataSet
Public Shared Function GetFieldType(ByVal FieldType As String) As System.Type
Select Case FieldType
Case "int"
Return Type.GetType("System.Int32")
Case "varchar"
Return Type.GetType("System.String")
Case "datetime"
Return Type.GetType("System.DateTime")
Case "float"
Return Type.GetType("System.Double")
End Select
Return Type.GetType("System.Object")
End Function

Public Shared Function DefineDataset(ByVal Field() As String, ByVal FieldType() As String, ByVal PrimaryKey As String) As DataSet
Dim dstDataset As New DataSet
Dim dtTable As New DataTable
Dim i As Integer
For i = 0 To FieldType.Length - 1
dtTable.Columns.Add(New DataColumn(Field(i), GetFieldType(FieldType(i))))
If Field(i) = PrimaryKey Then
dtTable.Constraints.Add("c" & i, dtTable.Columns(i), True)
End If
Next
dstDataset.Tables.Add(dtTable)
Return (dstDataset)
End Function
Public Property GridBindDataSet() As DataSet
Get
If Not (ViewState("GridDataset") Is Nothing) Then
dstDataSet = CType(ViewState("GridDataset"), DataSet)
End If
If dstDataSet Is Nothing Then
dstDataSet = DefineDataset(New String() {"EmpID", "EmpName"}, New String() {"varchar", "varchar"}, "")
ViewState.Add("GridDataset", dstDataSet)
End If
Return dstDataSet
End Get

Set(ByVal value As DataSet)
dstDataSet = value
If Not (ViewState("GridDataset") Is Nothing) Then
ViewState("GridDataset") = dstDataSet
Else
ViewState.Add("GridDataset", dstDataSet)
End If
End Set
End Property
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
Dim drwRow As DataRow = GridBindDataSet.Tables(0).NewRow()
drwRow("EmpID") = ""
drwRow("EmpName") = ""
GridBindDataSet.Tables(0).Rows.Add(drwRow)
GridView1.DataSource = GridBindDataSet
GridView1.DataBind()
End If
End Sub

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim drwRow As DataRow = GridBindDataSet.Tables(0).NewRow()
drwRow("EmpID") = ""
drwRow("EmpName") = ""
GridBindDataSet.Tables(0).Rows.Add(drwRow)
GridView1.DataSource = GridBindDataSet
GridView1.DataBind()
End Sub
End Class

[/code]

How to get total number of folder counts in a drive

Description :


This code shows how to get total number of folder counts in a drive

[CODE]
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
MsgBox(GetFileCount("c:\"))

End If
End Sub
Public Function GetFileCount(ByVal strPath As String) As Integer
Try
Return System.IO.Directory.GetFiles(strPath).Length()
Catch ex As Exception
Throw New Exception("Error From GetFileCount Function" & ex.Message, ex)
End Try
End Function
End Class
[/CODE]

How to create Web Browser in vb.net

Abstract


Create new windows application by selecting New project.Drag and drop the one text box and button control to form.Add the Browser control to form then double click on the button control to bring up button's click event then put the below code
[code]
WebBrowser1.Navigate(TextBox1.Text)
[/code]

Sample Source Code



[code]
Public Class Form1

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
WebBrowser1.Navigate(TextBox1.Text)
End Sub
End Class

[/code]

How to show openfile dialogue box while clicking textbox of fileupload control of asp.net

Just assign like this in your page load event
FileUpload1.Attributes.Add("onmousedown", "document.getElementById('" + FileUpload1.ClientID + "').click()");

How to open word file with in iframe?

Just drag and drop the iframe in your page or else just paste the below code


In page load event just
myPDF.Attributes.Add("src", filepath);