Nitin Dhiman. Powered by Blogger.

Google Search in asp.net page.

No comments
asp:content id="Content1" contentplaceholderid="ContentPlaceHolder1" runat="Server"
//put the following line in script:

putScriptHere src="http://www.google.com/jsapi?key=ABQIAAAAchOdSzCPe0fgAm8Ow24YYhQZMjxVy-0fy3B1JroJth17tbEUrRQwDlqM1PUlwiawvBO4RMVpYVIfLA"
type="text/javascript"

//put following also in script tag

google.load("search", "0");

function OnLoad() {
// Create a search control
var searchControl = new google.search.SearchControl();

// Add in a full set of searchers
var localSearch = new google.search.LocalSearch();
searchControl.addSearcher(localSearch);
searchControl.addSearcher(new google.search.WebSearch());
searchControl.addSearcher(new google.search.VideoSearch());
searchControl.addSearcher(new google.search.BlogSearch());

// Set the Local Search center point
localSearch.setCenterPoint("New Delhi, INDIA");

// Tell the searcher to draw itself and tell it where to attach
searchControl.draw(document.getElementById("searchcontrol"));

// Execute an inital search
searchControl.execute("");
}
google.setOnLoadCallback(OnLoad);


divSTART id="searchcontrol"
Loading.../divEND
/asp:content

javascript to find day difference.

No comments
HTML START
HEAD START HERE
SCRIPT START HERE language="javascript" type="text/javascript"
today = new Date();
todayEpoch = today.getTime();

target = new Date("1 Jan, 2009");
targetEpoch = target.getTime();

daysLeft = Math.floor(((targetEpoch - todayEpoch) / (60*60*24)) / 1000);
SCRIPT END HERE

HEAD END
BODY START HERE
FROM START method="get" action="http://www.google.com/search"

DoD inspection in

SCRIPT START HERE document.write(daysLeft); SCRIPT TAG END HERE

days. Are you ready?

FORM END
BODY END
HTML END

Create bound control column in a grid.

No comments
public void createBoundCols()
{
DataTable dt = ds.Tables[0];

int countDt = 0;
foreach (DataColumn col in dt.Columns)
{
if (countDt < 12)
{
BoundField bfield = new BoundField();
bfield.DataField = col.ColumnName;
bfield.HeaderText = col.ColumnName;
GrdDynamic.Columns.Add(bfield);
countDt = countDt + 1;
}

}

CommandField cf = new CommandField();
cf.HeaderText = "print";

cf.ShowSelectButton = true;
cf.SelectText = "Print";
cf.Visible = true;


GrdDynamic.Columns.Add(cf);

}



// and on grid row command so that we can fire the event associated with the created bound column.



protected void GrdDynamic_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Select")
{
int index = Convert.ToInt32(e.CommandArgument);

// Retrieve the row that contains the button clicked
// by the user from the Rows collection.
GridViewRow row = GrdDynamic.Rows[index];
int rowid = Convert.ToInt32(row.Cells[0].Text.ToString());
Response.Redirect("RecordDetails.aspx?rID=" + rowid);

}
}

print all data on a page.

No comments
Hi,

Here is the javascript code to print all the data on the page:

scriptSTART language="javascript" type="text/javascript"

function printdoc()
{
var div=document.getElementById("DivPrint");

//to hide the undesired DIV from print page.
div.style.display='none';
window.print();
return true;
}

/scriptEND


asp:button id="btnPrint" runat="server" width="90px" text="Print" onclientclick="printdoc();"

Export to excel, word in .net

No comments
Hi,

Here is the code to export the grid data to excel or word:

// create an enum
public enum ExportType
{

EXCEL,

PDF,

WORD
}

//source can be DataSet, DataTable and etc
public void ExportDataGrid(object source, ExportType type)
{
try
{
// setting temporarily datagrid
DataGrid dg = new DataGrid();
dg.ID = "GridView1";
dg.DataSource = source;
dg.AutoGenerateColumns = true;
dg.AllowPaging = false;
dg.AllowSorting = false;
dg.DataBind();
// export selected format
Response.Clear();
Response.Buffer = true;
switch ((type))
{
case ExportType.EXCEL:
Response.ContentType = "application/ms-excel";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.xls", "ExportedGridData"));
break;
case ExportType.PDF:
break;
// TODO
case ExportType.WORD:
Response.ContentType = "application/ms-word";
Response.AddHeader("content-disposition", string.Format("attachment;filename={0}.doc", "ExportedGridData"));
break;
}
Response.Charset = "";
System.IO.StringWriter stringWriter = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWriter = new HtmlTextWriter(stringWriter);
dg.RenderControl(htmlWriter);
Response.Write(stringWriter.ToString());
Response.End();
}
catch (Exception ex)
{
if ((ex.Message != "Thread was being aborted."))
{
Response.Write(("Error occur while exporting excel. For more information:" + ex.Message));
}
}
}
protected void btnExportToExcel_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = (DataSet)Session["ds"];

if (ds.Tables[0].Rows.Count > 0)
{
ExportDataGrid(ds, ExportType.EXCEL);
}
}
protected void btnExportToWord_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = (DataSet)Session["ds"];

if (ds.Tables[0].Rows.Count > 0)
{
ExportDataGrid(ds, ExportType.WORD);
}
}

protected void btnExportToPDF_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds = (DataSet)Session["ds"];

if (ds.Tables[0].Rows.Count > 0)
{
ExportDataGrid(ds, ExportType.PDF);
}
}

Get 1st day of week using C#.

No comments
Hi,

Here is the code to get date on 1st day of week, we just need to call this function with date:


private static DateTime firstDayOfWeek(DateTime day, DayOfWeek weekStarts)
{
DateTime d = day;
while (d.DayOfWeek != weekStarts)
{
d = d.AddDays(-1);
}

return d;
}


and to call this function:


DateTime myDate = firstDayOfWeek(DateTime.Now, DayOfWeek.Sunday);

from Excel file to Sql table.

No comments
Hello again,

Here is the code to fill sql table from data in excel file:

protected void button_click(object sender, EventArgs e)
{
string sqlTblName = "nitinSongs";
string excelFileName = "WT_codes.xls";
string workBook = "[nitin$]";
string exlConStr = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Data Source=" + Server.MapPath("WT_codes.xls") + ";" + "Extended Properties=Excel 8.0;";
string sqlConStr = "Data Source=SERVER NAME;Database=DATABASE NAME;Persist Security Info=True;User ID=LOGINID;Password=PASSWORD";

SqlConnection sqlCon = new SqlConnection(sqlConStr);
SqlCommand sqlCom = new SqlCommand("DELETE FROM " + sqlTblName, sqlCon);
sqlCon.Open();
sqlCom.ExecuteNonQuery();
sqlCon.Close();

OleDbConnection oleCon = new OleDbConnection(exlConStr);
OleDbCommand oleCom = new OleDbCommand("select * from " + workBook, oleCon);
oleCon.Open();
OleDbDataReader dr = oleCom.ExecuteReader();
SqlBulkCopy bCopy = new SqlBulkCopy(sqlConStr);
bCopy.DestinationTableName = sqlTblName;
bCopy.WriteToServer(dr);
oleCon.Close();
}